Skip to content

Commit

Permalink
Namespace method aliases
Browse files Browse the repository at this point in the history
There's a conflict when another gem has a monekypatch method named `original_require`. By adding a namespace we should ensure that there no conflict.
  • Loading branch information
schneems committed Jan 11, 2021
1 parent d8af8b6 commit 1ec0390
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/dead_end/auto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
# Monkey patch kernel to ensure that all `require` calls call the same
# method
module Kernel
alias_method :original_require, :require
alias_method :original_require_relative, :require_relative
alias_method :original_load, :load
alias_method :dead_end_original_require, :require
alias_method :dead_end_original_require_relative, :require_relative
alias_method :dead_end_original_load, :load

def load(file, wrap = false)
original_load(file)
dead_end_original_load(file)
rescue SyntaxError => e
DeadEnd.handle_error(e)
end

def require(file)
original_require(file)
dead_end_original_require(file)
rescue SyntaxError => e
DeadEnd.handle_error(e)
end

def require_relative(file)
if Pathname.new(file).absolute?
original_require file
dead_end_original_require file
else
original_require File.expand_path("../#{file}", caller_locations(1, 1)[0].absolute_path)
dead_end_original_require File.expand_path("../#{file}", caller_locations(1, 1)[0].absolute_path)
end
rescue SyntaxError => e
DeadEnd.handle_error(e)
Expand Down Expand Up @@ -62,7 +62,7 @@ module DeadEnd
# we can attempt to disable this behavior in a production context.
if !DeadEnd::IsProduction.call
class NoMethodError
alias :original_to_s :to_s
alias :dead_end_original_to_s :to_s

def to_s
return super if DeadEnd::IsProduction.call
Expand Down Expand Up @@ -93,7 +93,7 @@ def to_s
message << $/
message
rescue => e
puts "DeadEnd Internal error: #{e.original_to_s}"
puts "DeadEnd Internal error: #{e.dead_end_original_to_s}"
puts "DeadEnd Internal backtrace:"
puts backtrace.map {|l| " " + l }.join($/)
super
Expand Down
25 changes: 25 additions & 0 deletions spec/integration/ruby_command_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@

module DeadEnd
RSpec.describe "Requires with ruby cli" do
it "namespaces all monkeypatched methods" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
@script = @tmpdir.join("script.rb")
@script.write <<~'EOM'
puts Kernel.private_methods
EOM

dead_end_methods_array = `ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`.strip.lines.map(&:strip)
kernel_methods_array = `ruby #{@script} 2>&1`.strip.lines.map(&:strip)
methods = (dead_end_methods_array - kernel_methods_array).sort
expect(methods).to eq(["dead_end_original_load", "dead_end_original_require", "dead_end_original_require_relative", "timeout"])


@script.write <<~'EOM'
puts Kernel.private_methods
EOM

dead_end_methods_array = `ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`.strip.lines.map(&:strip)
kernel_methods_array = `ruby #{@script} 2>&1`.strip.lines.map(&:strip)
methods = (dead_end_methods_array - kernel_methods_array).sort
expect(methods).to eq(["dead_end_original_load", "dead_end_original_require", "dead_end_original_require_relative", "timeout"])
end
end

it "does not get in an infinite loop when NoMethodError is raised internally" do
Dir.mktmpdir do |dir|
@tmpdir = Pathname(dir)
Expand Down

0 comments on commit 1ec0390

Please sign in to comment.