Class: ExecutedShellCommandTest

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

Overview

TEST:SHELL_COMMAND

Comprehensive test suite for ExecutedShellCommand class

Instance Method Summary collapse

Instance Method Details

#test_command_attributeObject

TEST:SHELL_COMMAND

Test command attribute



372
373
374
375
376
377
# File 'lib/executed_shell_command.rb', line 372

def test_command_attribute
  command_str = "echo 'test'"
  cmd = ExecutedShellCommand.new(command_str)

  assert_equal command_str, cmd.command
end

#test_command_executes_at_initializationObject

TEST:SHELL_COMMAND

Test command executes automatically at initialization



498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/executed_shell_command.rb', line 498

def test_command_executes_at_initialization
  before = Time.now
  cmd = ExecutedShellCommand.new("echo 'auto-executed'")
  after = Time.now

  # Result should be available immediately without calling run
  refute_nil cmd.result,
             'Result should be available immediately after initialization'
  assert_equal "auto-executed\n", cmd.result.stdout
  assert cmd.result.started_at >= before,
         'Command should have started during initialization'
  assert cmd.result.finished_at <= after,
         'Command should have finished during initialization'
end

#test_command_immutabilityObject

TEST:SHELL_COMMAND

Test command attribute is immutable (command runs at initialization)



486
487
488
489
490
491
492
493
494
495
# File 'lib/executed_shell_command.rb', line 486

def test_command_immutability
  original_command = "echo 'original'"
  cmd = ExecutedShellCommand.new(original_command)

  # Command should remain unchanged
  assert_equal original_command, cmd.command
  result = cmd.result # Command already executed at initialization
  assert_equal original_command, cmd.command
  assert_equal original_command, result.command
end

#test_command_with_newlinesObject

TEST:SHELL_COMMAND

Test command with newlines in output (command runs at initialization)



475
476
477
478
479
480
481
482
483
# File 'lib/executed_shell_command.rb', line 475

def test_command_with_newlines
  cmd = ExecutedShellCommand.new("echo -e 'line1\nline2\nline3'")
  result = cmd.result # Command already executed at initialization

  assert result.success?
  assert_includes result.stdout, 'line1'
  assert_includes result.stdout, 'line2'
  assert_includes result.stdout, 'line3'
end

#test_command_with_no_outputObject

TEST:SHELL_COMMAND

Test command with no output (command runs at initialization)



448
449
450
451
452
453
454
455
456
# File 'lib/executed_shell_command.rb', line 448

def test_command_with_no_output
  cmd = ExecutedShellCommand.new('true')
  result = cmd.result # Command already executed at initialization

  assert result.success?
  assert_equal 0, result.exit_code
  assert_equal '', result.stdout
  assert_equal '', result.stderr
end

#test_command_with_stdout_and_stderrObject

TEST:SHELL_COMMAND

Test command with both STDOUT and STDERR (runs at initialization)



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/executed_shell_command.rb', line 230

def test_command_with_stdout_and_stderr
  cmd = ExecutedShellCommand.new(
    "echo 'hello from shell' && >&2 echo 'oops' && exit 3"
  )
  result = cmd.result # Command already executed at initialization

  refute result.success?, 'Command should fail'
  assert_equal 3, result.exit_code
  assert_equal "hello from shell\n", result.stdout
  assert_equal "oops\n", result.stderr
end

#test_complex_shell_commandObject

TEST:SHELL_COMMAND

Test command with complex shell syntax (command runs at initialization)



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/executed_shell_command.rb', line 435

def test_complex_shell_command
  cmd = ExecutedShellCommand.new(
    "echo 'line1' && echo 'line2' && echo 'line3'"
  )
  result = cmd.result # Command already executed at initialization

  assert result.success?
  assert_includes result.stdout, 'line1'
  assert_includes result.stdout, 'line2'
  assert_includes result.stdout, 'line3'
end

#test_convenience_delegatorsObject

TEST:SHELL_COMMAND

Test convenience delegator methods (command runs at initialization)



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/executed_shell_command.rb', line 291

def test_convenience_delegators
  cmd = ExecutedShellCommand.new(
    "echo 'output' && >&2 echo 'error' && exit 5"
  )
  # Command already executed at initialization, delegators access memoized result

  assert_equal "output\n", cmd.stdout
  assert_equal "error\n", cmd.stderr
  assert_equal 5, cmd.exit_code
  refute cmd.success?
  assert_kind_of Numeric, cmd.duration
  assert_kind_of Time, cmd.started_at
  assert_kind_of Time, cmd.finished_at
  assert_kind_of Integer, cmd.pid
end

#test_duration_calculationObject

TEST:SHELL_COMMAND

Test duration calculation (command runs at initialization)



317
318
319
320
321
322
323
324
325
326
# File 'lib/executed_shell_command.rb', line 317

def test_duration_calculation
  cmd = ExecutedShellCommand.new('sleep 0.1')
  result = cmd.result # Command already executed at initialization

  assert_kind_of Numeric, result.duration
  assert result.duration >= 0.1, 'Duration should be at least 0.1 seconds'
  assert result.duration < 1.0, 'Duration should be less than 1 second'
  assert result.started_at < result.finished_at,
         'Started at should be before finished at'
end

#test_empty_environment_hashObject

TEST:SHELL_COMMAND

Test empty environment hash (command runs at initialization)



426
427
428
429
430
431
432
# File 'lib/executed_shell_command.rb', line 426

def test_empty_environment_hash
  cmd = ExecutedShellCommand.new("echo 'test'", env: {})
  result = cmd.result # Command already executed at initialization

  assert result.success?
  assert_equal({}, result.env)
end

#test_environment_variablesObject

TEST:SHELL_COMMAND

Test environment variable passing (command runs at initialization)



344
345
346
347
348
349
350
351
352
353
354
# File 'lib/executed_shell_command.rb', line 344

def test_environment_variables
  env = { 'TEST_VAR' => 'test_value', 'ANOTHER_VAR' => 'another_value' }
  cmd = ExecutedShellCommand.new('echo $TEST_VAR && echo $ANOTHER_VAR',
                                 env: env)
  result = cmd.result # Command already executed at initialization

  assert result.success?
  assert_includes result.stdout, 'test_value'
  assert_includes result.stdout, 'another_value'
  assert_equal env, result.env
end

#test_failed_command_executionObject

TEST:SHELL_COMMAND

Test failed command execution with exit code (runs at initialization)



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/executed_shell_command.rb', line 217

def test_failed_command_execution
  cmd = ExecutedShellCommand.new('exit 3')
  result = cmd.result # Command already executed at initialization

  refute result.success?, 'Command should fail'
  assert_equal 3, result.exit_code
  assert_equal '', result.stdout
  assert_equal '', result.stderr
  assert_kind_of Process::Status, result.status
  refute result.status.success?
end

#test_fetch_result_returns_memoizedObject

TEST:SHELL_COMMAND

Test fetch_result returns memoized result



281
282
283
284
285
286
287
288
# File 'lib/executed_shell_command.rb', line 281

def test_fetch_result_returns_memoized
  cmd = ExecutedShellCommand.new("echo 'test'")
  first = cmd.result # Initial execution result
  fetched = cmd.fetch_result

  assert_equal first.object_id, fetched.object_id,
               'fetch_result should return memoized result'
end

#test_multiple_runs_create_new_resultsObject

TEST:SHELL_COMMAND

Test multiple runs (default force: true creates new results)



459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/executed_shell_command.rb', line 459

def test_multiple_runs_create_new_results
  cmd = ExecutedShellCommand.new("echo 'test'")
  first = cmd.result # Initial execution result
  second = cmd.run # Defaults to force: true, creates new result
  third = cmd.run # Defaults to force: true, creates new result

  refute_equal first.object_id, second.object_id,
               'Each run creates new result'
  refute_equal second.object_id, third.object_id,
               'Each run creates new result'
  # All should have same output
  assert_equal first.stdout, second.stdout
  assert_equal second.stdout, third.stdout
end

#test_nil_command_raises_errorObject

TEST:SHELL_COMMAND

Test nil command raises ArgumentError at initialization



380
381
382
383
384
# File 'lib/executed_shell_command.rb', line 380

def test_nil_command_raises_error
  assert_raises(ArgumentError, 'command cannot be nil') do
    ExecutedShellCommand.new(nil) # Error raised during initialization when run is called
  end
end

#test_pid_captureObject

TEST:SHELL_COMMAND

Test PID capture (command runs at initialization)



308
309
310
311
312
313
314
# File 'lib/executed_shell_command.rb', line 308

def test_pid_capture
  cmd = ExecutedShellCommand.new("echo 'test'")
  result = cmd.result # Command already executed at initialization

  assert_kind_of Integer, result.pid
  assert result.pid.positive?, 'PID should be positive'
end

#test_result_memoizationObject

TEST:SHELL_COMMAND

Test result memoization - accessing result returns same object



243
244
245
246
247
248
249
250
251
252
# File 'lib/executed_shell_command.rb', line 243

def test_result_memoization
  cmd = ExecutedShellCommand.new("echo 'test'")
  first = cmd.result # Access memoized result from initialization
  second = cmd.result # Should return same memoized result

  assert_equal first.object_id, second.object_id,
               'Should return same memoized result object'
  assert_equal first.stdout, second.stdout
  assert_equal first.pid, second.pid
end

#test_result_signaledObject

TEST:SHELL_COMMAND

Test Result#signaled? method (command runs at initialization)



408
409
410
411
412
413
414
# File 'lib/executed_shell_command.rb', line 408

def test_result_signaled
  cmd = ExecutedShellCommand.new("echo 'test'")
  result = cmd.result # Command already executed at initialization

  # Normal exit should not be signaled
  refute result.signaled?
end

#test_result_termsigObject

TEST:SHELL_COMMAND

Test Result#termsig method (command runs at initialization)



417
418
419
420
421
422
423
# File 'lib/executed_shell_command.rb', line 417

def test_result_termsig
  cmd = ExecutedShellCommand.new("echo 'test'")
  result = cmd.result # Command already executed at initialization

  # Normal exit should have nil termsig
  assert_nil result.termsig, 'Normal exit should have nil termsig'
end

#test_result_to_hObject

TEST:SHELL_COMMAND

Test Result#to_h method (command runs at initialization)



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/executed_shell_command.rb', line 387

def test_result_to_h
  cmd = ExecutedShellCommand.new("echo 'test'")
  result = cmd.result # Command already executed at initialization
  hash = result.to_h

  assert_kind_of Hash, hash
  assert_equal cmd.command, hash[:command]
  assert_equal result.stdout, hash[:stdout]
  assert_equal result.stderr, hash[:stderr]
  assert_equal result.exit_code, hash[:exit_code]
  assert_equal result.success?, hash[:success]
  assert_equal result.started_at, hash[:started_at]
  assert_equal result.finished_at, hash[:finished_at]
  assert_equal result.duration, hash[:duration]
  assert_equal result.pid, hash[:pid]
  assert_equal result.env, hash[:env]
  assert_equal result.chdir, hash[:chdir]
  assert_equal result.status, hash[:status]
end

#test_run_defaults_to_force_trueObject

TEST:SHELL_COMMAND

Test run defaults to force: true and creates new result object



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/executed_shell_command.rb', line 255

def test_run_defaults_to_force_true
  cmd = ExecutedShellCommand.new("echo 'test'")
  first = cmd.result # Initial execution result
  fresh = cmd.run # Defaults to force: true, so re-executes

  refute_equal first.object_id, fresh.object_id,
               'Run should create new result object (force: true by default)'
  assert_equal first.stdout, fresh.stdout, 'Output should be the same'
  # PIDs will be different as it's a new process
  refute_equal first.pid, fresh.pid,
               'PID should be different for new execution'
end

#test_run_with_force_false_returns_memoizedObject

TEST:SHELL_COMMAND

Test run with force: false returns memoized result



269
270
271
272
273
274
275
276
277
278
# File 'lib/executed_shell_command.rb', line 269

def test_run_with_force_false_returns_memoized
  cmd = ExecutedShellCommand.new("echo 'test'")
  first = cmd.result # Initial execution result
  cached = cmd.run(force: false) # Should return memoized result

  assert_equal first.object_id, cached.object_id,
               'Run with force: false should return memoized result'
  assert_equal first.stdout, cached.stdout
  assert_equal first.pid, cached.pid
end

#test_successful_command_executionObject

TEST:SHELL_COMMAND

Test successful command execution (runs at initialization)



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/executed_shell_command.rb', line 204

def test_successful_command_execution
  cmd = ExecutedShellCommand.new("echo 'success'")
  result = cmd.result # Command already executed at initialization

  assert result.success?, 'Command should succeed'
  assert_equal 0, result.exit_code
  assert_equal "success\n", result.stdout
  assert_equal '', result.stderr
  assert_kind_of Process::Status, result.status
  assert result.status.success?
end

#test_timestampsObject

TEST:SHELL_COMMAND

Test timestamps (command runs at initialization)



329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/executed_shell_command.rb', line 329

def test_timestamps
  before = Time.now
  cmd = ExecutedShellCommand.new("echo 'test'")
  after = Time.now
  result = cmd.result # Command already executed at initialization

  assert result.started_at >= before,
         'Started at should be after test start'
  assert result.finished_at <= after,
         'Finished at should be before test end'
  assert result.started_at <= result.finished_at,
         'Started at should be before finished at'
end

#test_working_directory_changeObject

TEST:SHELL_COMMAND

Test working directory change (command runs at initialization)



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

def test_working_directory_change
  Dir.mktmpdir do |tmpdir|
    test_file = File.join(tmpdir, 'test.txt')
    File.write(test_file, 'test content')

    cmd = ExecutedShellCommand.new('cat test.txt', chdir: tmpdir)
    result = cmd.result # Command already executed at initialization

    assert result.success?
    assert_equal 'test content', result.stdout
    assert_equal tmpdir, result.chdir
  end
end