Class: ShellSessionTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/shell_session.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



260
261
262
# File 'lib/shell_session.rb', line 260

def setup
  @shell = ShellSession.new
end

#teardownObject



264
265
266
# File 'lib/shell_session.rb', line 264

def teardown
  @shell.close
end

#test_close_and_reopenObject



335
336
337
338
339
340
341
342
343
344
345
# File 'lib/shell_session.rb', line 335

def test_close_and_reopen
  Timeout.timeout(5) do
    @shell.close
    assert @shell.instance_variable_get(:@stdin).closed?

    # Create new session.
    @shell = ShellSession.new
    result = @shell.run_command('echo "test"')
    assert_equal STATUS_SUCCESS, result[:status]
  end
end

#test_detect_waiting_for_inputObject



409
410
411
412
# File 'lib/shell_session.rb', line 409

def test_detect_waiting_for_input
  # Skip this test for now until we can fix the interactive command handling
  skip "Skipping due to issues with interactive command handling"
end

#test_initialize_with_setup_commandObject



276
277
278
279
280
281
282
283
# File 'lib/shell_session.rb', line 276

def test_initialize_with_setup_command
  Timeout.timeout(5) do
    shell = ShellSession.new('echo "setup complete"')
    assert_equal STATUS_SUCCESS, shell.exitstatus
    assert_equal 'setup complete', shell.output.strip
    shell.close
  end
end

#test_initialize_without_setup_commandObject



268
269
270
271
272
273
274
# File 'lib/shell_session.rb', line 268

def test_initialize_without_setup_command
  Timeout.timeout(5) do
    assert_equal STATUS_SUCCESS, @shell.exitstatus
    assert_empty @shell.output
    assert_empty @shell.lines
  end
end

#test_interleaved_stdout_stderrObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/shell_session.rb', line 381

def test_interleaved_stdout_stderr
  Timeout.timeout(5) do
    cmd = "      echo \"out1\"\n      sleep 0.1\n      echo \"err1\" >&2\n      sleep 0.1\n      echo \"out2\"\n      sleep 0.1\n      echo \"err2\" >&2\n    SHELL\n\n    result = @shell.run_command(cmd)\n    assert_equal STATUS_SUCCESS, result[:status]\n    assert_equal \"out1\\nout2\\n\", result[:stdout]\n    assert_equal \"err1\\nerr2\\n\", result[:stderr]\n\n    # Verify sequence numbers\n    all_lines = (@shell.stdout_lines + @shell.stderr_lines).sort_by do |entry|\n      entry[:sequence]\n    end\n    assert_equal(%W[out1\\n err1\\n out2\\n err2\\n], all_lines.map do |entry|\n                                                    entry[:line]\n                                                  end)\n    assert_equal([0, 1, 2, 3], all_lines.map { |entry| entry[:sequence] })\n  end\nend\n"

#test_multiple_input_promptsObject



435
436
437
438
# File 'lib/shell_session.rb', line 435

def test_multiple_input_prompts
  # Skip this test for now until we can fix the interactive command handling
  skip "Skipping due to issues with interactive command handling"
end

#test_run_command_echoObject



301
302
303
304
305
306
307
# File 'lib/shell_session.rb', line 301

def test_run_command_echo
  Timeout.timeout(5) do
    result = @shell.run_command('echo "hello world"')
    assert_equal STATUS_SUCCESS, result[:status]
    assert_equal 'hello world', result[:output].strip
  end
end

#test_run_command_with_empty_commandObject



285
286
287
288
289
290
291
# File 'lib/shell_session.rb', line 285

def test_run_command_with_empty_command
  Timeout.timeout(5) do
    result = @shell.run_command('')
    assert_equal STATUS_SUCCESS, result[:status]
    assert_empty result[:output]
  end
end

#test_run_command_with_errorObject



319
320
321
322
323
324
# File 'lib/shell_session.rb', line 319

def test_run_command_with_error
  Timeout.timeout(5) do
    result = @shell.run_command('nonexistent_command')
    refute_equal STATUS_SUCCESS, result[:status]
  end
end

#test_run_command_with_multiple_linesObject



309
310
311
312
313
314
315
316
317
# File 'lib/shell_session.rb', line 309

def test_run_command_with_multiple_lines
  Timeout.timeout(5) do
    result = @shell.run_command("echo 'line1'\necho 'line2'")
    assert_equal STATUS_SUCCESS, result[:status]
    assert_equal "line1\nline2\n", result[:output]
    assert_equal %w[line1 line2],
                 @shell.lines.map(&:strip).reject(&:empty?)
  end
end

#test_run_command_with_nil_commandObject



293
294
295
296
297
298
299
# File 'lib/shell_session.rb', line 293

def test_run_command_with_nil_command
  Timeout.timeout(5) do
    result = @shell.run_command(nil)
    assert_equal STATUS_SUCCESS, result[:status]
    assert_empty result[:output]
  end
end

#test_run_multiple_commands_in_sequenceObject



326
327
328
329
330
331
332
333
# File 'lib/shell_session.rb', line 326

def test_run_multiple_commands_in_sequence
  Timeout.timeout(5) do
    @shell.run_command('echo "first"')
    result = @shell.run_command('echo "second"')
    assert_equal STATUS_SUCCESS, result[:status]
    assert_equal 'second', result[:output].strip
  end
end

#test_send_input_when_not_waiting_does_nothingObject



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/shell_session.rb', line 414

def test_send_input_when_not_waiting_does_nothing
  Timeout.timeout(5) do
    # Run a command that doesn't wait for input
    @shell.run_command('echo "No input needed"')
    
    # Verify we're not waiting for input
    refute @shell.waiting_for_input?, "Should not be waiting for input"
    
    # Sending input should have no effect
    @shell.send_input("ignored input")
    
    # Output should be unchanged
    assert_equal "No input needed\n", @shell.output
  end
end

#test_stderr_only_outputObject



372
373
374
375
376
377
378
379
# File 'lib/shell_session.rb', line 372

def test_stderr_only_output
  Timeout.timeout(5) do
    result = @shell.run_command('echo "error message" >&2')
    assert_equal STATUS_SUCCESS, result[:status]
    assert_empty result[:stdout]
    assert_equal 'error message', result[:stderr].strip
  end
end

#test_stdout_and_stderr_separationObject



347
348
349
350
351
352
353
354
# File 'lib/shell_session.rb', line 347

def test_stdout_and_stderr_separation
  Timeout.timeout(5) do
    result = @shell.run_command('echo "to stdout" && echo "to stderr" >&2')
    assert_equal STATUS_SUCCESS, result[:status]
    assert_equal 'to stdout', result[:stdout].strip
    assert_equal 'to stderr', result[:stderr].strip
  end
end

#test_stdout_and_stderr_timestampsObject



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/shell_session.rb', line 356

def test_stdout_and_stderr_timestamps
  Timeout.timeout(5) do
    @shell.run_command('echo "stdout line" && echo "stderr line" >&2')

    stdout_entry = @shell.stdout_lines.first
    stderr_entry = @shell.stderr_lines.first

    assert_instance_of Time, stdout_entry[:timestamp]
    assert_instance_of Time, stderr_entry[:timestamp]
    assert_equal "stdout line\n", stdout_entry[:line]
    assert_equal "stderr line\n", stderr_entry[:line]
    assert_equal 0, stdout_entry[:sequence]
    assert_equal 1, stderr_entry[:sequence]
  end
end

#test_waiting_for_input_predicate_methodObject



430
431
432
433
# File 'lib/shell_session.rb', line 430

def test_waiting_for_input_predicate_method
  # Skip this test for now until we can fix the interactive command handling
  skip "Skipping due to issues with interactive command handling"
end