Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace method aliases #51

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## HEAD (unreleased)

## 1.1.2

- Namespace Kernel method aliases (https://github.com/zombocom/dead_end/pull/51)

## 1.1.1

- Safer NoMethodError annotation (https://github.com/zombocom/dead_end/pull/48)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dead_end (1.1.1)
dead_end (1.1.2)

GEM
remote: https://rubygems.org/
Expand Down
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
2 changes: 1 addition & 1 deletion lib/dead_end/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DeadEnd
VERSION = "1.1.1"
VERSION = "1.1.2"
end
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