Skip to content

Commit bab78e0

Browse files
authored
refactor: simplify Kernel patch restoration (#72)
1 parent ffb3f9a commit bab78e0

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

codetracer/kernel_patches.rb

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,38 @@
33
module Codetracer
44
module KernelPatches
55
@@tracers = []
6-
@@original_methods = {}
76

87
def self.install(tracer)
98
@@tracers << tracer
109

11-
if @@original_methods.empty?
12-
@@original_methods[:p] = Kernel.instance_method(:p)
13-
@@original_methods[:puts] = Kernel.instance_method(:puts)
14-
@@original_methods[:print] = Kernel.instance_method(:print)
15-
10+
if @@tracers.length == 1
1611
Kernel.module_eval do
17-
alias_method :old_p, :p unless method_defined?(:old_p)
18-
alias_method :old_puts, :puts unless method_defined?(:old_puts)
19-
alias_method :old_print, :print unless method_defined?(:old_print)
12+
alias_method :codetracer_original_p, :p unless method_defined?(:codetracer_original_p)
13+
alias_method :codetracer_original_puts, :puts unless method_defined?(:codetracer_original_puts)
14+
alias_method :codetracer_original_print, :print unless method_defined?(:codetracer_original_print)
2015

2116
define_method(:p) do |*args|
22-
loc = caller_locations(1,1).first
17+
loc = caller_locations(1, 1).first
2318
@@tracers.each do |t|
2419
t.record_event(loc.path, loc.lineno, args.map(&:inspect).join("\n"))
2520
end
26-
@@original_methods[:p].bind(self).call(*args)
21+
codetracer_original_p(*args)
2722
end
2823

2924
define_method(:puts) do |*args|
30-
loc = caller_locations(1,1).first
25+
loc = caller_locations(1, 1).first
3126
@@tracers.each do |t|
3227
t.record_event(loc.path, loc.lineno, args.join("\n"))
3328
end
34-
@@original_methods[:puts].bind(self).call(*args)
29+
codetracer_original_puts(*args)
3530
end
3631

3732
define_method(:print) do |*args|
38-
loc = caller_locations(1,1).first
33+
loc = caller_locations(1, 1).first
3934
@@tracers.each do |t|
4035
t.record_event(loc.path, loc.lineno, args.join)
4136
end
42-
@@original_methods[:print].bind(self).call(*args)
37+
codetracer_original_print(*args)
4338
end
4439
end
4540
end
@@ -48,13 +43,16 @@ def self.install(tracer)
4843
def self.uninstall(tracer)
4944
@@tracers.delete(tracer)
5045

51-
if @@tracers.empty? && !@@original_methods.empty?
46+
if @@tracers.empty? && Kernel.private_method_defined?(:codetracer_original_p)
5247
Kernel.module_eval do
53-
define_method(:p, @@original_methods[:p])
54-
define_method(:puts, @@original_methods[:puts])
55-
define_method(:print, @@original_methods[:print])
48+
alias_method :p, :codetracer_original_p
49+
alias_method :puts, :codetracer_original_puts
50+
alias_method :print, :codetracer_original_print
51+
52+
remove_method :codetracer_original_p
53+
remove_method :codetracer_original_puts
54+
remove_method :codetracer_original_print
5655
end
57-
@@original_methods.clear
5856
end
5957
end
6058
end

test/test_kernel_patches.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def teardown
4040
end
4141
# Verify that original methods are restored if no tracers are left
4242
assert_empty Codetracer::KernelPatches.class_variable_get(:@@tracers), "Tracers should be empty after teardown"
43-
assert_empty Codetracer::KernelPatches.class_variable_get(:@@original_methods), "Original methods should be cleared if no tracers are left"
43+
refute Kernel.private_method_defined?(:codetracer_original_p), "Original method aliases should be removed"
44+
refute Kernel.private_method_defined?(:codetracer_original_puts), "Original method aliases should be removed"
45+
refute Kernel.private_method_defined?(:codetracer_original_print), "Original method aliases should be removed"
4446
end
4547

4648
def test_patching_and_basic_event_recording
@@ -115,6 +117,9 @@ def test_restoration_of_original_methods
115117
p 'original restored' # This line's output will go to actual stdout
116118

117119
assert_empty @tracer1.events, "Tracer should not record events after being uninstalled and patches removed"
120+
refute Kernel.private_method_defined?(:codetracer_original_p), "Original method aliases should be removed"
121+
refute Kernel.private_method_defined?(:codetracer_original_puts), "Original method aliases should be removed"
122+
refute Kernel.private_method_defined?(:codetracer_original_print), "Original method aliases should be removed"
118123
end
119124

120125
def test_correct_event_arguments

0 commit comments

Comments
 (0)