Skip to content

Commit 62b4b96

Browse files
committed
fix(tests): run the gem installation test before the other tests
This resolves issue #105
1 parent 12b98d7 commit 62b4b96

File tree

3 files changed

+86
-78
lines changed

3 files changed

+86
-78
lines changed

Justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
alias t := test
22

33
test:
4+
ruby -Itest test/gem_installation.rb
45
ruby -Itest -e 'Dir["test/test_*.rb"].each { |f| require File.expand_path(f) }'
56

67
bench pattern="*" write_report="console":

test/gem_installation.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'minitest/autorun'
2+
require 'fileutils'
3+
require 'open3'
4+
require 'rbconfig'
5+
require 'tmpdir'
6+
7+
class GemInstallationTest < Minitest::Test
8+
def run_gem_installation_test(gem_bin, gem_module)
9+
Dir.chdir(File.expand_path('..', __dir__)) do
10+
gem_dir = File.join('gems', gem_bin)
11+
12+
if gem_bin == 'codetracer-ruby-recorder'
13+
system('just', 'build-extension', exception: true)
14+
dlext = RbConfig::CONFIG['DLEXT']
15+
ext_path = File.join(gem_dir, 'ext', 'native_tracer', 'target', 'release', "codetracer_ruby_recorder.#{dlext}")
16+
FileUtils.rm_f(ext_path)
17+
end
18+
19+
Dir.mktmpdir('gemhome') do |gem_home|
20+
gemspec = Dir[File.join(gem_dir, '*.gemspec')].first
21+
gem_build = IO.popen(%W[gem -C #{gem_dir} build #{File.basename(gemspec)}], err: [:child, :out]) { |io| io.read }
22+
gem_file = gem_build.lines.grep(/File:/).first.split.last
23+
gem_file = File.expand_path(File.join(gem_dir, gem_file))
24+
25+
env = { 'GEM_HOME' => gem_home, 'GEM_PATH' => gem_home, 'PATH' => "#{gem_home}/bin:#{ENV['PATH']}" }
26+
system(env, 'gem', 'install', '--local', gem_file, exception: true)
27+
28+
out_dir = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}")
29+
FileUtils.rm_rf(out_dir)
30+
stdout, stderr, status = Open3.capture3(env, RbConfig.ruby, '-S', gem_bin, '--out-dir', out_dir, File.join('test', 'programs', 'addition.rb'))
31+
raise "#{gem_bin} failed: #{stderr}" unless status.success?
32+
assert_equal "3\n", stdout
33+
assert File.exist?(File.join(out_dir, 'trace.json'))
34+
35+
out_dir_lib = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}_lib")
36+
FileUtils.rm_rf(out_dir_lib)
37+
38+
recorder_class = if gem_bin == 'codetracer-ruby-recorder'
39+
"CodeTracer::RubyRecorder"
40+
else
41+
"CodeTracer::PureRubyRecorder"
42+
end
43+
44+
script = <<~RUBY
45+
require '#{gem_module}'
46+
recorder = #{recorder_class}.new('#{out_dir_lib}')
47+
puts 'start trace'
48+
recorder.stop
49+
puts 'this will not be traced'
50+
recorder.start
51+
puts 'this will be traced'
52+
recorder.stop
53+
puts 'tracing disabled'
54+
recorder.flush_trace
55+
RUBY
56+
script_path = File.join('test', 'tmp', "use_#{gem_bin.tr('-', '_')}.rb")
57+
File.write(script_path, script)
58+
stdout, stderr, status = Open3.capture3(env, RbConfig.ruby, script_path)
59+
raise "#{gem_module} library failed: #{stderr}" unless status.success?
60+
expected_out = <<~OUT
61+
start trace
62+
this will not be traced
63+
this will be traced
64+
tracing disabled
65+
OUT
66+
assert_equal expected_out, stdout
67+
assert File.exist?(File.join(out_dir_lib, 'trace.json'))
68+
end
69+
end
70+
end
71+
72+
def test_gem_installation
73+
run_gem_installation_test('codetracer-ruby-recorder', 'codetracer_ruby_recorder')
74+
end
75+
76+
def test_pure_gem_installation
77+
# When the pure Ruby recorder traces a script that holds a reference to the
78+
# `PureRubyRecorder` instance in a local variable, the variable inspection code
79+
# would recursively serialise the tracer's internal state. This results in an
80+
# explosive amount of output and may appear as an infinite recursion when running
81+
# `examples/selective_tracing_pure.rb`. For this reason, we skip this test for now.
82+
skip
83+
run_gem_installation_test('codetracer-pure-ruby-recorder', 'codetracer_pure_ruby_recorder')
84+
end
85+
end

test/test_tracer.rb

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -94,84 +94,6 @@ def test_args_sum_with_separator
9494
assert_equal expected, native_out
9595
end
9696

97-
def run_gem_installation_test(gem_bin, gem_module)
98-
Dir.chdir(File.expand_path('..', __dir__)) do
99-
gem_dir = File.join('gems', gem_bin)
100-
101-
if gem_bin == 'codetracer-ruby-recorder'
102-
system('just', 'build-extension', exception: true)
103-
dlext = RbConfig::CONFIG['DLEXT']
104-
ext_path = File.join(gem_dir, 'ext', 'native_tracer', 'target', 'release', "codetracer_ruby_recorder.#{dlext}")
105-
FileUtils.rm_f(ext_path)
106-
end
107-
108-
Dir.mktmpdir('gemhome') do |gem_home|
109-
gemspec = Dir[File.join(gem_dir, '*.gemspec')].first
110-
gem_build = IO.popen(%W[gem -C #{gem_dir} build #{File.basename(gemspec)}], err: [:child, :out]) { |io| io.read }
111-
gem_file = gem_build.lines.grep(/File:/).first.split.last
112-
gem_file = File.expand_path(File.join(gem_dir, gem_file))
113-
114-
env = { 'GEM_HOME' => gem_home, 'GEM_PATH' => gem_home, 'PATH' => "#{gem_home}/bin:#{ENV['PATH']}" }
115-
system(env, 'gem', 'install', '--local', gem_file, exception: true)
116-
117-
out_dir = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}")
118-
FileUtils.rm_rf(out_dir)
119-
stdout, stderr, status = Open3.capture3(env, RbConfig.ruby, '-S', gem_bin, '--out-dir', out_dir, File.join('test', 'programs', 'addition.rb'))
120-
raise "#{gem_bin} failed: #{stderr}" unless status.success?
121-
assert_equal "3\n", stdout
122-
assert File.exist?(File.join(out_dir, 'trace.json'))
123-
124-
out_dir_lib = File.join('test', 'tmp', "gem_install_#{gem_bin.tr('-', '_')}_lib")
125-
FileUtils.rm_rf(out_dir_lib)
126-
127-
recorder_class = if gem_bin == 'codetracer-ruby-recorder'
128-
"CodeTracer::RubyRecorder"
129-
else
130-
"CodeTracer::PureRubyRecorder"
131-
end
132-
133-
script = <<~RUBY
134-
require '#{gem_module}'
135-
recorder = #{recorder_class}.new('#{out_dir_lib}')
136-
puts 'start trace'
137-
recorder.stop
138-
puts 'this will not be traced'
139-
recorder.start
140-
puts 'this will be traced'
141-
recorder.stop
142-
puts 'tracing disabled'
143-
recorder.flush_trace
144-
RUBY
145-
script_path = File.join('test', 'tmp', "use_#{gem_bin.tr('-', '_')}.rb")
146-
File.write(script_path, script)
147-
stdout, stderr, status = Open3.capture3(env, RbConfig.ruby, script_path)
148-
raise "#{gem_module} library failed: #{stderr}" unless status.success?
149-
expected_out = <<~OUT
150-
start trace
151-
this will not be traced
152-
this will be traced
153-
tracing disabled
154-
OUT
155-
assert_equal expected_out, stdout
156-
assert File.exist?(File.join(out_dir_lib, 'trace.json'))
157-
end
158-
end
159-
end
160-
161-
def test_gem_installation
162-
run_gem_installation_test('codetracer-ruby-recorder', 'codetracer_ruby_recorder')
163-
end
164-
165-
def test_pure_gem_installation
166-
# When the pure Ruby recorder traces a script that holds a reference to the
167-
# `PureRubyRecorder` instance in a local variable, the variable inspection code
168-
# would recursively serialise the tracer's internal state. This results in an
169-
# explosive amount of output and may appear as an infinite recursion when running
170-
# `examples/selective_tracing_pure.rb`. For this reason, we skip this test for now.
171-
skip
172-
run_gem_installation_test('codetracer-pure-ruby-recorder', 'codetracer_pure_ruby_recorder')
173-
end
174-
17597
def test_pure_debug_smoke
17698
Dir.chdir(File.expand_path('..', __dir__)) do
17799
env = { 'CODETRACER_RUBY_RECORDER_DEBUG' => '1' }

0 commit comments

Comments
 (0)