Module: GoTestHelper

Defined in:
lib/go-cli-test-support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/go-cli-test-support.rb', line 8

def debug
  @debug
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



7
8
9
# File 'lib/go-cli-test-support.rb', line 7

def exit_code
  @exit_code
end

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/go-cli-test-support.rb', line 7

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



7
8
9
# File 'lib/go-cli-test-support.rb', line 7

def stdout
  @stdout
end

#test_dirObject (readonly)

Returns the value of attribute test_dir.



7
8
9
# File 'lib/go-cli-test-support.rb', line 7

def test_dir
  @test_dir
end

Instance Method Details

#exec(*args, stdin: nil, timeout: 30) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/go-cli-test-support.rb', line 37

def exec(*args, stdin: nil, timeout: 30)
  debug_log "Executing: #{@binary} #{args.join(' ')}"
  debug_log "Working directory: #{@test_dir}"
  debug_log "Timeout: #{timeout}s"
  
  begin
    Timeout.timeout(timeout) do
      @stdout, @stderr, status = Open3.capture3(
        { 'HOME' => @test_dir },
        @binary, *args,
        stdin_data: stdin,
        chdir: @test_dir
      )
      @exit_code = status.exitstatus
    end
  rescue Timeout::Error
    @stdout = ''
    @stderr = "Command timed out after #{timeout} seconds"
    @exit_code = 124
    debug_log "Command timed out!"
  end
  
  debug_log "Exit code: #{@exit_code}"
  debug_log "STDOUT: #{@stdout}" if @debug && !@stdout.empty?
  debug_log "STDERR: #{@stderr}" if @debug && !@stderr.empty?
  
  self
end

#setup_go_test(project_dir, compile_path: nil, build_flags: []) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/go-cli-test-support.rb', line 10

def setup_go_test(project_dir, compile_path: nil, build_flags: [])
  raise ArgumentError, "Project directory does not exist: #{project_dir}" unless Dir.exist?(project_dir)
  
  go_mod = File.join(project_dir, 'go.mod')
  raise ArgumentError, "go.mod not found in #{project_dir}" unless File.exist?(go_mod)
  
  binary_name = detect_binary_name(project_dir)
  @test_dir = Dir.mktmpdir("#{binary_name}_test_")
  
  build_dir = compile_path || detect_build_dir(project_dir)
  @binary = File.join(@test_dir, binary_name)
  
  build_cmd = ['go', 'build', *build_flags, '-o', @binary]
  
  debug_log "Building Go binary: #{build_cmd.join(' ')}"
  debug_log "Build directory: #{build_dir}"
  
  _, stderr, status = Open3.capture3(*build_cmd, chdir: build_dir)
  raise "Compile failed:\n#{stderr}" unless status.success?
  
  debug_log "Binary created: #{@binary}"
end

#teardown_go_testObject



33
34
35
# File 'lib/go-cli-test-support.rb', line 33

def teardown_go_test
  FileUtils.rm_rf(@test_dir) if @test_dir && Dir.exist?(@test_dir)
end