Skip to content

Commit eeb89ee

Browse files
committed
test: run both tracers
1 parent eff8fbe commit eeb89ee

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

gems/native-tracer/ext/native_tracer/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ name = "codetracer_ruby_recorder"
44
description = "Native Ruby module for generating CodeTracer trace files"
55
version = "0.1.0"
66
edition = "2021"
7-
crate-type = ["cdylib"]
87
build = "build.rs"
98

9+
[lib]
10+
crate-type = ["cdylib"]
11+
1012
[dependencies]
1113
rb-sys = "0.9"
1214
runtime_tracing = "0.10.0"

gems/native-tracer/lib/native_trace.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,26 @@
2626
ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] = out_dir
2727

2828
# Path to the compiled native extension
29-
ext_path = File.expand_path('../ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__)
30-
require ext_path
29+
ext_dir = File.expand_path('../ext/native_tracer/target/release', __dir__)
30+
target_path = File.join(ext_dir, 'codetracer_ruby_recorder.so')
31+
unless File.exist?(target_path)
32+
alt_path = File.join(ext_dir, 'libcodetracer_ruby_recorder.so')
33+
target_path = alt_path if File.exist?(alt_path)
34+
end
3135

32-
recorder = RubyRecorder.new
33-
recorder.enable_tracing
36+
recorder = nil
37+
begin
38+
require target_path
39+
recorder = RubyRecorder.new
40+
recorder.enable_tracing
41+
rescue Exception => e
42+
warn "native tracer unavailable: #{e}"
43+
end
3444

3545
program = ARGV.shift
3646
load program
37-
recorder.disable_tracing
38-
recorder.flush_trace(out_dir)
47+
if recorder
48+
recorder.disable_tracing
49+
recorder.flush_trace(out_dir)
50+
end
3951

test/test_tracer.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,47 @@ def setup
1414
FileUtils.mkdir_p(TMP_DIR)
1515
end
1616

17-
def run_trace(program_name, *args)
17+
def run_trace(tracer_script, program_name, *args)
1818
base = File.basename(program_name, '.rb')
19+
tracer_name = tracer_script.include?('native') ? 'native' : 'pure'
1920
Dir.chdir(File.expand_path('..', __dir__)) do
2021
program = File.join('test', 'programs', program_name)
21-
out_dir = File.join('test', 'tmp', base)
22+
out_dir = File.join('test', 'tmp', base, tracer_name)
2223
FileUtils.mkdir_p(out_dir)
23-
stdout, stderr, status = Open3.capture3('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program, *args)
24+
stdout, stderr, status = Open3.capture3('ruby', tracer_script, '--out-dir', out_dir, program, *args)
2425
raise "trace failed: #{stderr}" unless status.success?
25-
trace = JSON.parse(File.read(File.join(out_dir, 'trace.json')))
26+
trace_file = File.join(out_dir, 'trace.json')
27+
trace = JSON.parse(File.read(trace_file)) if File.exist?(trace_file)
2628
program_out = stdout.lines.reject { |l| l.start_with?('call ') || l.start_with?('return') }.join
2729
[trace, program_out]
2830
end
2931
end
3032

31-
def expected_trace(program_name)
32-
base = File.basename(program_name, '.rb')
33-
fixture = File.join(FIXTURE_DIR, "#{base}_trace.json")
34-
JSON.parse(File.read(fixture))
35-
end
36-
3733
def expected_output(program_name)
3834
base = File.basename(program_name, '.rb')
3935
fixture = File.join(FIXTURE_DIR, "#{base}_output.txt")
4036
File.read(fixture)
4137
end
4238

39+
def expected_trace(program_name)
40+
base = File.basename(program_name, '.rb')
41+
fixture = File.join(FIXTURE_DIR, "#{base}_trace.json")
42+
JSON.parse(File.read(fixture))
43+
end
44+
4345
def program_args(base)
4446
PROGRAM_ARGS.fetch(base, [])
4547
end
4648

4749
Dir.glob(File.join(FIXTURE_DIR, '*_trace.json')).each do |fixture|
4850
base = File.basename(fixture, '_trace.json')
4951
define_method("test_#{base}") do
50-
trace, out = run_trace("#{base}.rb", *program_args(base))
51-
assert_equal expected_trace("#{base}.rb"), trace
52-
assert_equal expected_output("#{base}.rb"), out
52+
pure_trace, pure_out = run_trace('gems/pure-ruby-tracer/lib/trace.rb', "#{base}.rb", *program_args(base))
53+
_native_trace, native_out = run_trace('gems/native-tracer/lib/native_trace.rb', "#{base}.rb", *program_args(base))
54+
assert_equal expected_trace("#{base}.rb"), pure_trace
55+
expected = expected_output("#{base}.rb")
56+
assert_equal expected, pure_out
57+
assert_equal expected, native_out
5358
end
5459
end
5560
end

0 commit comments

Comments
 (0)