Class: Ace::TestRunner::Molecules::TestExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_runner/molecules/test_executor.rb

Overview

Executes test commands and captures output

Instance Method Summary collapse

Constructor Details

#initialize(command_builder: nil, timeout: nil) ⇒ TestExecutor

Returns a new instance of TestExecutor.



12
13
14
15
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 12

def initialize(command_builder: nil, timeout: nil)
  @command_builder = command_builder || Atoms::CommandBuilder.new
  @timeout = timeout  # In seconds, nil = no timeout
end

Instance Method Details

#execute_command(command) ⇒ Object



29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 29

def execute_command(command)
  start_time = Time.now
  stdout = ""
  stderr = ""
  status = nil

  # Set environment to prevent Minitest autorun at_exit hook
  # Also strip assignment context vars to prevent tests from resolving to wrong assignments
  # Inherit parent environment and override specific vars (nil unsets at exec time)
  env = ENV.to_h.merge({
    "MT_NO_AUTORUN" => "1",
    "ACE_ASSIGN_ID" => nil,
    "ACE_ASSIGN_FORK_ROOT" => nil
  })

  # Remove MT_NO_AUTORUN=1 from command if it's there
  command = command.sub(/^MT_NO_AUTORUN=1\s+/, "")

  begin
    if @timeout
      Timeout.timeout(@timeout) do
        stdout, stderr, status = Open3.capture3(env, command)
      end
    else
      stdout, stderr, status = Open3.capture3(env, command)
    end
  rescue Timeout::Error
    stderr = "Test execution timed out after #{@timeout} seconds"
    status = OpenStruct.new(success?: false, exitstatus: 124)
  end

  end_time = Time.now

  {
    stdout: stdout,
    stderr: stderr,
    status: status,
    command: command,
    start_time: start_time,
    end_time: end_time,
    duration: end_time - start_time,
    success: status.success?
  }
end

#execute_per_file_with_progress(files, options = {}, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 98

def execute_per_file_with_progress(files, options = {}, &block)
  results = []

  files.each do |file|
    yield({type: :start, file: file}) if block_given?

    result = execute_single_file(file, options)
    results << result

    if block_given?
      yield({
        type: :complete,
        file: file,
        success: result[:success],
        duration: result[:duration]
      })
    end

    # Stop on first failure if fail_fast is set
    break if options[:fail_fast] && !result[:success]
  end

  merge_results(results)
end

#execute_single_file(file, options = {}) ⇒ Object



24
25
26
27
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 24

def execute_single_file(file, options = {})
  command = @command_builder.build_single_file_command(file, options)
  execute_command(command)
end

#execute_tests(files, options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 17

def execute_tests(files, options = {})
  return empty_result if files.empty?

  command = @command_builder.build_test_command(files, options)
  execute_command(command)
end

#execute_with_progress(files, options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ace/test_runner/molecules/test_executor.rb', line 74

def execute_with_progress(files, options = {}, &block)
  # Fail-fast requires per-file execution to stop on first failure
  # For performance, execute all files together unless explicitly disabled or fail-fast enabled
  if options[:per_file] == true || options[:fail_fast]
    execute_per_file_with_progress(files, options, &block)
  else
    # Execute all files in a single Ruby process for performance
    result = execute_tests(files, options)

    # Send stdout event for per-test progress parsing
    if block_given? && result[:stdout]
      yield({type: :stdout, content: result[:stdout]})
    end

    # Simulate progress callbacks for compatibility
    if block_given?
      files.each { |file| yield({type: :start, file: file}) }
      files.each { |file| yield({type: :complete, file: file, success: result[:success], duration: result[:duration] / files.size}) }
    end

    result
  end
end