Skip to content

Commit 3afecd4

Browse files
committed
test: share gem install logic and exercise library usage
1 parent 8a2fe2a commit 3afecd4

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'fileutils'
2+
require 'rbconfig'
3+
4+
ext_dir = File.expand_path('../ext/native_tracer/target/release', __dir__)
5+
dlext = RbConfig::CONFIG['DLEXT']
6+
lib = File.join(ext_dir, "codetracer_ruby_recorder.#{dlext}")
7+
unless File.exist?(lib)
8+
alt = %w[so bundle dylib dll]
9+
.map { |ext| File.join(ext_dir, "libcodetracer_ruby_recorder.#{ext}") }
10+
.find { |path| File.exist?(path) }
11+
if alt
12+
begin
13+
File.symlink(alt, lib)
14+
rescue StandardError
15+
FileUtils.cp(alt, lib)
16+
end
17+
end
18+
end
19+
require lib

test/test_tracer.rb

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,75 @@ def program_args(base)
6262
end
6363
end
6464

65-
def test_gem_installation
65+
def run_gem_installation_test(gem_bin, gem_module)
6666
Dir.chdir(File.expand_path('..', __dir__)) do
67-
system('just', 'build-extension', exception: true)
68-
69-
dlext = RbConfig::CONFIG['DLEXT']
70-
ext_path = File.join('gems', 'native-tracer', 'ext', 'native_tracer', 'target', 'release', "codetracer_ruby_recorder.#{dlext}")
71-
FileUtils.rm_f(ext_path)
67+
gem_dir = case gem_bin
68+
when 'codetracer-ruby-recorder'
69+
File.join('gems', 'native-tracer')
70+
when 'codetracer-pure-ruby-recorder'
71+
File.join('gems', 'pure-ruby-tracer')
72+
else
73+
raise ArgumentError, "unknown gem #{gem_bin}"
74+
end
75+
76+
if gem_bin == 'codetracer-ruby-recorder'
77+
system('just', 'build-extension', exception: true)
78+
dlext = RbConfig::CONFIG['DLEXT']
79+
ext_path = File.join(gem_dir, 'ext', 'native_tracer', 'target', 'release', "codetracer_ruby_recorder.#{dlext}")
80+
FileUtils.rm_f(ext_path)
81+
end
7282

7383
Dir.mktmpdir('gemhome') do |gem_home|
74-
gem_build = IO.popen(%w[gem -C gems/native-tracer build codetracer-ruby-recorder.gemspec], err: [:child, :out]) { |io| io.read }
84+
gemspec = Dir[File.join(gem_dir, '*.gemspec')].first
85+
gem_build = IO.popen(%W[gem -C #{gem_dir} build #{File.basename(gemspec)}], err: [:child, :out]) { |io| io.read }
7586
gem_file = gem_build.lines.grep(/File:/).first.split.last
76-
gem_file = File.expand_path(File.join('gems/native-tracer', gem_file))
87+
gem_file = File.expand_path(File.join(gem_dir, gem_file))
7788

7889
env = { 'GEM_HOME' => gem_home, 'GEM_PATH' => gem_home, 'PATH' => "#{gem_home}/bin:#{ENV['PATH']}" }
7990
system(env, 'gem', 'install', '--local', gem_file, exception: true)
8091

81-
out_dir = File.join('test', 'tmp', 'gem_install')
92+
out_dir = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}")
8293
FileUtils.rm_rf(out_dir)
83-
stdout, stderr, status = Open3.capture3(env, 'ruby', '-S', 'codetracer-ruby-recorder', '--out-dir', out_dir, File.join('test', 'programs', 'addition.rb'))
84-
raise "native_trace failed: #{stderr}" unless status.success?
94+
stdout, stderr, status = Open3.capture3(env, 'ruby', '-S', gem_bin, '--out-dir', out_dir, File.join('test', 'programs', 'addition.rb'))
95+
raise "#{gem_bin} failed: #{stderr}" unless status.success?
8596
assert_equal "3\n", stdout
8697
assert File.exist?(File.join(out_dir, 'trace.json'))
98+
99+
out_dir_lib = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}_lib")
100+
FileUtils.rm_rf(out_dir_lib)
101+
script = <<~RUBY
102+
require '#{gem_module}'
103+
recorder = RubyRecorder.new
104+
puts 'start trace'
105+
recorder.disable_tracing
106+
puts 'this will not be traced'
107+
recorder.enable_tracing
108+
puts 'this will be traced'
109+
recorder.disable_tracing
110+
puts 'tracing disabled'
111+
recorder.flush_trace('#{out_dir_lib}')
112+
RUBY
113+
script_path = File.join('test', 'tmp', "use_#{gem_bin.tr('-', '_')}.rb")
114+
File.write(script_path, script)
115+
stdout, stderr, status = Open3.capture3(env, 'ruby', script_path)
116+
raise "#{gem_module} library failed: #{stderr}" unless status.success?
117+
expected_out = <<~OUT
118+
start trace
119+
this will not be traced
120+
this will be traced
121+
tracing disabled
122+
OUT
123+
assert_equal expected_out, stdout
124+
assert File.exist?(File.join(out_dir_lib, 'trace.json'))
87125
end
88126
end
89127
end
90128

91-
def test_pure_gem_installation
92-
Dir.chdir(File.expand_path('..', __dir__)) do
93-
Dir.mktmpdir('gemhome') do |gem_home|
94-
gem_build = IO.popen(%w[gem -C gems/pure-ruby-tracer build codetracer_pure_ruby_recorder.gemspec], err: [:child, :out]) { |io| io.read }
95-
gem_file = gem_build.lines.grep(/File:/).first.split.last
96-
gem_file = File.expand_path(File.join('gems/pure-ruby-tracer', gem_file))
97-
98-
env = { 'GEM_HOME' => gem_home, 'GEM_PATH' => gem_home, 'PATH' => "#{gem_home}/bin:#{ENV['PATH']}" }
99-
system(env, 'gem', 'install', '--local', gem_file, exception: true)
129+
def test_gem_installation
130+
run_gem_installation_test('codetracer-ruby-recorder', 'codetracer_ruby_recorder')
131+
end
100132

101-
out_dir = File.join('test', 'tmp', 'gem_install_pure')
102-
FileUtils.rm_rf(out_dir)
103-
stdout, stderr, status = Open3.capture3(env, 'ruby', '-S', 'codetracer-pure-ruby-recorder', '--out-dir', out_dir, File.join('test', 'programs', 'addition.rb'))
104-
raise "pure_trace failed: #{stderr}" unless status.success?
105-
assert_equal "3\n", stdout
106-
assert File.exist?(File.join(out_dir, 'trace.json'))
107-
end
108-
end
133+
def test_pure_gem_installation
134+
run_gem_installation_test('codetracer-pure-ruby-recorder', 'codetracer_pure_ruby_recorder')
109135
end
110136
end

0 commit comments

Comments
 (0)