88 @script_path = File . expand_path ( '../run_once.rb' , __dir__ )
99 @original_env = ENV . to_h
1010
11- # Create a temp directory with test benchmarks
1211 @tmpdir = Dir . mktmpdir
1312 @test_benchmark = File . join ( @tmpdir , 'test_benchmark.rb' )
13+ loader_path = File . expand_path ( '../lib/harness/loader' , __dir__ )
1414 File . write ( @test_benchmark , <<~RUBY )
15+ require "#{ loader_path } "
1516 puts "Benchmark executed"
1617 puts "WARMUP_ITRS=\# {ENV['WARMUP_ITRS']}"
1718 puts "MIN_BENCH_ITRS=\# {ENV['MIN_BENCH_ITRS']}"
2122
2223 after do
2324 FileUtils . rm_rf ( @tmpdir ) if @tmpdir && Dir . exist? ( @tmpdir )
24- # Restore original environment
2525 ENV . replace ( @original_env )
2626 end
2727
2828 def run_script ( *args )
29- # Run the script and capture output
3029 Open3 . capture3 ( 'ruby' , @script_path , *args )
3130 end
3231
@@ -71,7 +70,6 @@ def run_script(*args)
7170 end
7271
7372 it 'loads custom harness when specified' do
74- # Create a test harness
7573 harness_dir = File . join ( File . dirname ( @script_path ) , 'harness' )
7674 FileUtils . mkdir_p ( harness_dir )
7775 test_harness = File . join ( harness_dir , 'test_harness.rb' )
@@ -90,6 +88,26 @@ def run_script(*args)
9088 end
9189 end
9290
91+ it 'respects HARNESS environment variable when no --harness option provided' do
92+ harness_dir = File . join ( File . dirname ( @script_path ) , 'harness' )
93+ FileUtils . mkdir_p ( harness_dir )
94+ test_harness = File . join ( harness_dir , 'test_harness.rb' )
95+
96+ begin
97+ File . write ( test_harness , <<~RUBY )
98+ puts "TEST HARNESS LOADED"
99+ RUBY
100+
101+ env = { 'HARNESS' => 'test_harness' }
102+ stdout , _stderr , status = Open3 . capture3 ( env , 'ruby' , @script_path , @test_benchmark )
103+
104+ assert status . success?
105+ assert_match ( /TEST HARNESS LOADED/ , stdout )
106+ ensure
107+ File . delete ( test_harness ) if File . exist? ( test_harness )
108+ end
109+ end
110+
93111 it 'shows error for non-existent harness' do
94112 stdout , _stderr , status = run_script ( '--harness=nonexistent' , @test_benchmark )
95113
@@ -101,23 +119,19 @@ def run_script(*args)
101119
102120 describe 'ractor benchmark detection' do
103121 it 'automatically uses ractor harness for ractor benchmarks' do
104- # Create a ractor benchmark
105122 ractor_dir = File . join ( @tmpdir , 'benchmarks-ractor' , 'test' )
106123 FileUtils . mkdir_p ( ractor_dir )
107124 ractor_benchmark = File . join ( ractor_dir , 'benchmark.rb' )
108125 File . write ( ractor_benchmark , 'puts "Ractor benchmark"' )
109126
110127 _stdout , _stderr , _status = run_script ( ractor_benchmark )
111128
112- # The script will try to load ractor harness
113- # We just verify it detected the ractor path
114129 assert_match ( /benchmarks-ractor/ , ractor_benchmark )
115130 end
116131 end
117132
118133 describe 'Ruby options pass-through' do
119134 it 'passes Ruby options after -- separator' do
120- # Create a benchmark that checks for a warning flag
121135 warning_benchmark = File . join ( @tmpdir , 'warning_test.rb' )
122136 File . write ( warning_benchmark , <<~RUBY )
123137 x = 1
@@ -187,7 +201,6 @@ def run_script(*args)
187201
188202 describe 'script examples' do
189203 it 'works with simple benchmark path' do
190- # Simulates: ./run_once.rb benchmarks/fib.rb
191204 fib_benchmark = File . join ( @tmpdir , 'fib.rb' )
192205 File . write ( fib_benchmark , 'puts "Fibonacci benchmark"' )
193206
@@ -198,15 +211,13 @@ def run_script(*args)
198211 end
199212
200213 it 'works with harness option' do
201- # Simulates: ./run_once.rb --harness=default benchmarks/fib.rb
202214 stdout , _stderr , status = run_script ( '--harness=default' , @test_benchmark )
203215
204216 assert status . success?
205217 assert_match ( /Benchmark executed/ , stdout )
206218 end
207219
208220 it 'works with Ruby options after -- separator' do
209- # Simulates: ./run_once.rb -- --yjit benchmarks/fib.rb
210221 stdout , _stderr , status = run_script ( '--' , '--yjit' , @test_benchmark )
211222
212223 assert status . success?
@@ -228,7 +239,6 @@ def run_script(*args)
228239 end
229240
230241 it 'identifies first .rb file as benchmark' do
231- # Even if multiple .rb files mentioned (shouldn't happen), first one wins
232242 other_file = File . join ( @tmpdir , 'other.rb' )
233243 File . write ( other_file , 'puts "Wrong file"' )
234244
@@ -246,4 +256,36 @@ def run_script(*args)
246256 assert_match ( /invalid option|Error/ , stdout )
247257 end
248258 end
259+
260+ describe 'direct ruby execution with HARNESS env var' do
261+ it 'uses HARNESS environment variable when running benchmark directly' do
262+ harness_dir = File . join ( File . dirname ( @script_path ) , 'harness' )
263+ FileUtils . mkdir_p ( harness_dir )
264+ direct_harness = File . join ( harness_dir , 'direct_test.rb' )
265+
266+ begin
267+ File . write ( direct_harness , <<~RUBY )
268+ puts "DIRECT HARNESS RUNNING"
269+ RUBY
270+
271+ env = { 'HARNESS' => 'direct_test' }
272+ stdout , _stderr , status = Open3 . capture3 ( env , 'ruby' , @test_benchmark )
273+
274+ assert status . success? , "Direct Ruby execution should work with HARNESS env var"
275+ assert_match ( /DIRECT HARNESS RUNNING/ , stdout )
276+ assert_match ( /Benchmark executed/ , stdout )
277+ ensure
278+ File . delete ( direct_harness ) if File . exist? ( direct_harness )
279+ end
280+ end
281+
282+ it 'shows helpful error for invalid HARNESS value' do
283+ env = { 'HARNESS' => 'nonexistent_harness' }
284+ _stdout , stderr , status = Open3 . capture3 ( env , 'ruby' , @test_benchmark )
285+
286+ refute status . success?
287+ assert_match ( /Harness 'nonexistent_harness' not found/ , stderr )
288+ assert_match ( /Available harnesses/ , stderr )
289+ end
290+ end
249291end
0 commit comments