Class: MarkdownExec::TestUxBlockEvalForExport

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

Overview

Comprehensive tests for ux_block_eval_for_export function

Instance Method Summary collapse

Instance Method Details

#setupObject



7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
# File 'lib/hash_delegator.rb', line 7346

def setup
  @hd = HashDelegator.new
  @bash_script_lines = ['#!/bin/bash', 'set -e']
  @export = OpenStruct.new(
    name: 'TEST_VAR', required: ['TEST_VAR'], init: 'default'
  )
  @mock_result = OpenStruct.new(
    command_result: OpenStruct.new(
      exit_status: 0,
      stdout: 'test_output',
      stderr: '',
      warning: nil
    )
  )
end

#teardownObject



7362
7363
7364
7365
7366
7367
# File 'lib/hash_delegator.rb', line 7362

def teardown
  # Clean up environment variables set during tests
  ENV.delete('TEST_VAR')
  ENV.delete('VAR1')
  ENV.delete('VAR2')
end

#test_boolean_false_inputObject



7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
# File 'lib/hash_delegator.rb', line 7443

def test_boolean_false_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: false,
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
end

#test_boolean_true_inputObject

Test boolean inputs



7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
# File 'lib/hash_delegator.rb', line 7426

def test_boolean_true_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: true,
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
end

#test_empty_hash_inputObject

Edge case: empty hash



7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
# File 'lib/hash_delegator.rb', line 7630

def test_empty_hash_input
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: {},
    force: false,
    silent: false
  )

  assert_nil command_result # No iterations, so command_result remains nil
end

#test_empty_string_inputObject

Edge case: empty string



7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
# File 'lib/hash_delegator.rb', line 7594

def test_empty_string_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: '',
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
end

#test_environment_variable_settingObject

Test environment variable setting



7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
# File 'lib/hash_delegator.rb', line 7694

def test_environment_variable_setting
  @hd.stubs(:variable_is_exportable).returns(true)

  # Mock EnvInterface.set to verify it's called
  EnvInterface.expects(:set).with('TEST_VAR', "test\n")

  @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'echo "test"',
    force: false,
    silent: false
  )
end

#test_exit_status_required_empty_silentObject

Test EXIT_STATUS_REQUIRED_EMPTY handling with silent flag



7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
# File 'lib/hash_delegator.rb', line 7562

def test_exit_status_required_empty_silent
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'echo ""',
    force: false,
    silent: true
  )

  assert_empty command_result.warning # No warning when silent
end

#test_exit_status_required_empty_with_warningObject

Test EXIT_STATUS_REQUIRED_EMPTY handling



7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
# File 'lib/hash_delegator.rb', line 7539

def test_exit_status_required_empty_with_warning
  failed_result = CommandResult.new(
    exit_status: CommandResult::EXIT_STATUS_REQUIRED_EMPTY,
    exportable: false,
    stdout: '',
    stderr: 'Required variable empty',
    warning: nil
  )
  HashDelegator.stubs(:execute_bash_script_lines).returns(failed_result)
  @hd.stubs(:warning_required_empty).returns('Warning: required empty')

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'echo ""',
    force: false,
    silent: false
  )

  assert_equal failed_result, command_result
  assert_equal 'Warning: required empty', command_result.warning
end

#test_float_inputObject

Test float input



7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
# File 'lib/hash_delegator.rb', line 7461

def test_float_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 3.14,
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
end

#test_hash_input_first_onlyObject

Test hash input with first_only flag



7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
# File 'lib/hash_delegator.rb', line 7502

def test_hash_input_first_only
  hash_data = { 'VAR1' => 'value1', 'VAR2' => 'value2' }

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: hash_data,
    first_only: true,
    force: false,
    printf_expand: true,
    silent: false
  )

  assert_equal 1, command_result.new_lines.length
  assert_equal 'VAR1', command_result.new_lines.first[:name]
end

#test_hash_input_non_exportable_variablesObject

Test hash with non-exportable variables



7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
# File 'lib/hash_delegator.rb', line 7519

def test_hash_input_non_exportable_variables
  hash_data = { 'LOCAL_VAR' => 'value1' }
  @mock_result.exportable = true
  @mock_result.new_lines = []
  HashDelegator.stubs(:execute_bash_script_lines).returns(@mock_result)
  @hd.stubs(:variable_is_exportable).returns(false)
  @hd.stubs(:code_line_to_assign_a_variable).returns('LOCAL_VAR=value1')

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: hash_data,
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
  assert_equal 0, command_result.new_lines.length # No exportable variables
end

#test_hash_input_successObject

Test hash input - typical case



7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
# File 'lib/hash_delegator.rb', line 7479

def test_hash_input_success
  hash_data = { 'VAR1' => 'value1', 'VAR2' => 'value2' }
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)
  @hd.stubs(:code_line_to_assign_a_variable).returns('VAR1=value1')

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: hash_data,
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
  assert_equal 2, command_result.new_lines.length
  assert_equal 'VAR1', command_result.new_lines.first[:name]
  assert_equal 'VAR2', command_result.new_lines.last[:name]
end

#test_hash_with_nil_valuesObject

Edge case: hash with nil values



7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
# File 'lib/hash_delegator.rb', line 7642

def test_hash_with_nil_values
  hash_data = { 'VAR1' => nil, 'VAR2' => 'value2' }
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: hash_data,
    force: false,
    printf_expand: true,
    silent: false
  )

  assert_equal 2, command_result.new_lines.length
end

#test_integer_inputObject

Test integer input



7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
# File 'lib/hash_delegator.rb', line 7407

def test_integer_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 42,
    force: true,
    silent: false
  )

  assert_equal @mock_result, command_result
  assert_equal 1, command_result.new_lines.length
end

#test_join_array_of_arrays_calledObject

Test join_array_of_arrays is called correctly



7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
# File 'lib/hash_delegator.rb', line 7709

def test_join_array_of_arrays_called
  # Mock join_array_of_arrays to verify it's called with correct parameters
  @hd.expects(:join_array_of_arrays).with(
    @bash_script_lines,
    ['test_data']
  ).returns(['combined_script_lines'])

  @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'test_data',
    force: false,
    printf_expand: false,
    silent: false
  )
end

#test_nil_data_inputObject

Edge case: nil data (uses string.nil? check)



7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
# File 'lib/hash_delegator.rb', line 7612

def test_nil_data_input
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'd1',
    force: false,
    silent: false
  )

  assert_equal @mock_result, command_result
end

#test_standard_error_rescueObject

Test error handling - StandardError rescue



7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
# File 'lib/hash_delegator.rb', line 7681

def test_standard_error_rescue
  # Should not raise, but return nil due to rescue block
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'test_data',
    force: false,
    silent: false
  )

  assert_equal CommandResult::EXIT_STATUS_FAIL, command_result.exit_status
end

#test_string_input_successObject

Test string input - typical case



7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
# File 'lib/hash_delegator.rb', line 7370

def test_string_input_success
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines,
    @export,
    data: 'echo "hello"',
    force: false,
    silent: false
  )

  # assert_equal @mock_result, command_result
  assert_equal 1, command_result.new_lines.length
  assert_equal 'TEST_VAR', command_result.new_lines.first[:name]
  assert_equal "hello\n", command_result.stdout
end

#test_string_input_with_printf_expandObject

Test string input with printf_expand



7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
# File 'lib/hash_delegator.rb', line 7388

def test_string_input_with_printf_expand
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'test_value',
    force: false,
    printf_expand: true,
    silent: false
  )

  # Verify that join_array_of_arrays was called with printf wrapped expression
  # This tests that the expander lambda is applied correctly
end

#test_string_parameter_overrideObject

Test string parameter override



7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
# File 'lib/hash_delegator.rb', line 7574

def test_string_parameter_override
  @mock_result.exportable = true
  HashDelegator.stubs(:execute_bash_script_lines).returns(
    @mock_result
  )
  @hd.stubs(:variable_is_exportable).returns(true)

  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: 'original_data',
    string: 'override_string',
    force: false,
    silent: false
  )

  # The function should use string parameter instead of data
  assert_equal @mock_result, command_result
end

#test_unsupported_input_type_arrayObject

Edge case: unsupported input type (Array)



7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
# File 'lib/hash_delegator.rb', line 7656

def test_unsupported_input_type_array
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: %w[item1 item2],
    force: false,
    silent: false
  )

  assert_nil command_result # No processing for unsupported types
end

#test_unsupported_input_type_objectObject

Edge case: unsupported input type (custom object)



7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
# File 'lib/hash_delegator.rb', line 7668

def test_unsupported_input_type_object
  custom_object = Object.new
  command_result = @hd.ux_block_eval_for_export(
    @bash_script_lines, @export,
    data: custom_object,
    force: false,
    silent: false
  )

  assert_nil command_result # No processing for unsupported types
end