be brave, fully remove the 'edit-method' command

All functionality has been successfully moved to 'edit' command, with all tests passing.
This commit is contained in:
John Mair 2013-01-01 01:25:43 +01:00
parent 03d3d99211
commit 68758e49ab
4 changed files with 310 additions and 474 deletions

View File

@ -18,7 +18,7 @@ class Pry
if method_object.alias?
with_method_transaction do
_pry_.evaluate_ruby source
Pry.binding_for(method_object.owner).eval("alias #{method_object.name} #{original_name}")
Pry.binding_for(method_object.owner).eval("alias #{method_object.name} #{method_object.original_name}")
end
else
_pry_.evaluate_ruby source
@ -45,7 +45,7 @@ class Pry
# @param [String] meth_name The method name before aliasing
# @param [Module] target The owner of the method
def with_method_transaction
target = Pry.binding_for(target)
target = Pry.binding_for(method_object.owner)
temp_name = "__pry_#{method_object.original_name}__"
target.eval("alias #{temp_name} #{method_object.original_name}")

View File

@ -1,171 +0,0 @@
class Pry
class Command::EditMethod < Pry::ClassCommand
match 'edit-method'
group 'Editing'
description 'Edit the source code for a method.'
banner <<-BANNER
Usage: edit-method [OPTIONS] [METH]
Edit the method METH in an editor.
Ensure Pry.config.editor is set to your editor of choice.
e.g: `edit-method hello_method`
e.g: `edit-method Pry#rep`
e.g: `edit-method`
https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_method
BANNER
command_options :shellwords => false
def options(opt)
method_options(opt)
opt.on :n, "no-reload", "Do not automatically reload the method's file after editing."
opt.on "no-jump", "Do not fast forward editor to first line of method."
opt.on :p, :patch, "Instead of editing the method's file, try to edit in a tempfile and apply as a monkey patch."
end
def process
begin
@method = method_object
rescue MethodNotFound => err
end
if opts.present?(:patch) || (@method && @method.dynamically_defined?)
if err
raise err # can't patch a non-method
end
process_patch
else
if err && !File.exist?(target.eval('__FILE__'))
raise err # can't edit a non-file
end
process_file
end
end
def process_patch
lines = @method.source.lines.to_a
lines[0] = definition_line_for_owner(lines[0])
source = wrap_for_nesting(wrap_for_owner(Pry::Editor.edit_tempfile_with_content(lines)))
if @method.alias?
with_method_transaction(original_name, @method.owner) do
_pry_.evaluate_ruby source
Pry.binding_for(@method.owner).eval("alias #{@method.name} #{original_name}")
end
else
_pry_.evaluate_ruby source
end
end
def process_file
file, line = extract_file_and_line
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
Pry::Editor.invoke_editor(file, opts["no-jump"] ? 0 : line, reload)
silence_warnings do
load file if reload
end
end
protected
def extract_file_and_line
if @method
if @method.source_type == :c
raise CommandError, "Can't edit a C method."
else
[@method.source_file, @method.source_line]
end
else
[target.eval('__FILE__'), target.eval('__LINE__')]
end
end
# Run some code ensuring that at the end target#meth_name will not have changed.
#
# When we're redefining aliased methods we will overwrite the method at the
# unaliased name (so that super continues to work). By wrapping that code in a
# transation we make that not happen, which means that alias_method_chains, etc.
# continue to work.
#
# @param [String] meth_name The method name before aliasing
# @param [Module] target The owner of the method
def with_method_transaction(meth_name, target)
target = Pry.binding_for(target)
temp_name = "__pry_#{meth_name}__"
target.eval("alias #{temp_name} #{meth_name}")
yield
target.eval("alias #{meth_name} #{temp_name}")
ensure
target.eval("undef #{temp_name}") rescue nil
end
# The original name of the method, if it's not present raise an error telling
# the user why we don't work.
#
def original_name
@method.original_name or raise CommandError, "Pry can only patch methods created with the `def` keyword."
end
# Update the definition line so that it can be eval'd directly on the Method's
# owner instead of from the original context.
#
# In particular this takes `def self.foo` and turns it into `def foo` so that we
# don't end up creating the method on the singleton class of the singleton class
# by accident.
#
# This is necessarily done by String manipulation because we can't find out what
# syntax is needed for the argument list by ruby-level introspection.
#
# @param String The original definition line. e.g. def self.foo(bar, baz=1)
# @return String The new definition line. e.g. def foo(bar, baz=1)
def definition_line_for_owner(line)
if line =~ /^def (?:.*?\.)?#{Regexp.escape(original_name)}(?=[\(\s;]|$)/
"def #{original_name}#{$'}"
else
raise CommandError, "Could not find original `def #{original_name}` line to patch."
end
end
# Update the source code so that when it has the right owner when eval'd.
#
# This (combined with definition_line_for_owner) is backup for the case that
# wrap_for_nesting fails, to ensure that the method will stil be defined in
# the correct place.
#
# @param [String] source The source to wrap
# @return [String]
def wrap_for_owner(source)
Thread.current[:__pry_owner__] = @method.owner
source = "Thread.current[:__pry_owner__].class_eval do\n#{source}\nend"
end
# Update the new source code to have the correct Module.nesting.
#
# This method uses syntactic analysis of the original source file to determine
# the new nesting, so that we can tell the difference between:
#
# class A; def self.b; end; end
# class << A; def b; end; end
#
# The resulting code should be evaluated in the TOPLEVEL_BINDING.
#
# @param [String] source The source to wrap.
# @return [String]
def wrap_for_nesting(source)
nesting = Pry::Code.from_file(@method.source_file).nesting_at(@method.source_line)
(nesting + [source] + nesting.map{ "end" } + [""]).join("\n")
rescue Pry::Indent::UnparseableNestingError => e
source
end
end
Pry::Commands.add_command(Pry::Command::EditMethod)
end

View File

@ -1,299 +0,0 @@
require 'helper'
describe "edit-method" do
describe "on a method defined in a file" do
before do
@tempfile = Tempfile.new(['pry', '*.rb'])
@tempfile.puts <<-EOS
module A
def a
:yup
end
def b
:kinda
end
end
class X
include A
def self.x
:double_yup
end
def x
:nope
end
def b
super
end
alias c b
def y?
:because
end
class B
G = :nawt
def foo
:possibly
G
end
end
end
EOS
@tempfile.flush
load @tempfile.path
end
after do
@tempfile.close(true)
end
describe 'without -p' do
before do
@old_editor = Pry.config.editor
@file = @line = @contents = nil
Pry.config.editor = lambda do |file, line|
@file = file; @line = line
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should correctly find a class method" do
pry_eval 'edit-method X.x'
@file.should == @tempfile.path
@line.should == 14
end
it "should correctly find an instance method" do
pry_eval 'edit-method X#x'
@file.should == @tempfile.path
@line.should == 18
end
it "should correctly find a method on an instance" do
pry_eval 'x = X.new', 'edit-method x.x'
@file.should == @tempfile.path
@line.should == 18
end
it "should correctly find a method from a module" do
pry_eval 'edit-method X#a'
@file.should == @tempfile.path
@line.should == 2
end
it "should correctly find an aliased method" do
pry_eval 'edit-method X#c'
@file.should == @tempfile.path
@line.should == 22
end
end
describe 'with -p' do
before do
@old_editor = Pry.config.editor
Pry.config.editor = lambda do |file, line|
lines = File.read(file).lines.to_a
lines[1] = ":maybe\n"
File.open(file, 'w') do |f|
f.write(lines.join)
end
@patched_def = String(lines[1]).chomp
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should successfully replace a class method" do
pry_eval 'edit-method -p X.x'
class << X
X.method(:x).owner.should == self
end
X.method(:x).receiver.should == X
X.x.should == :maybe
end
it "should successfully replace an instance method" do
pry_eval 'edit-method -p X#x'
X.instance_method(:x).owner.should == X
X.new.x.should == :maybe
end
it "should successfully replace a method on an instance" do
pry_eval 'instance = X.new', 'edit-method -p instance.x'
instance = X.new
instance.method(:x).owner.should == X
instance.x.should == :maybe
end
it "should successfully replace a method from a module" do
pry_eval 'edit-method -p X#a'
X.instance_method(:a).owner.should == A
X.new.a.should == :maybe
end
it "should successfully replace a method with a question mark" do
pry_eval 'edit-method -p X#y?'
X.instance_method(:y?).owner.should == X
X.new.y?.should == :maybe
end
it "should preserve module nesting" do
pry_eval 'edit-method -p X::B#foo'
X::B.instance_method(:foo).owner.should == X::B
X::B.new.foo.should == :nawt
end
describe "monkey-patching" do
before do
@edit = 'edit-method --patch ' # A shortcut.
end
# @param [Integer] lineno
# @return [String] the stripped line from the tempfile at +lineno+
def stripped_line_at(lineno)
@tempfile.rewind
@tempfile.lines.to_a[lineno].strip
end
# Applies the monkey patch for +method+ with help of evaluation of
# +eval_strs+. The idea is to capture the initial line number (before
# the monkey patch), because it gets overwritten by the line number from
# the monkey patch. And our goal is to check that the original
# definition hasn't changed.
# @param [UnboundMethod] method
# @param [Array<String>] eval_strs
# @return [Array<String] the lines with definitions of the same line
# before monkey patching and after (normally, they should be equal)
def apply_monkey_patch(method, *eval_strs)
_, lineno = method.source_location
definition_before = stripped_line_at(lineno)
pry_eval(*eval_strs)
definition_after = stripped_line_at(lineno)
[definition_before, definition_after]
end
it "should work for a class method" do
def_before, def_after =
apply_monkey_patch(X.method(:x), "#@edit X.x")
def_before.should == ':double_yup'
def_after.should == ':double_yup'
@patched_def.should == ':maybe'
end
it "should work for an instance method" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:x), "#@edit X#x")
def_before.should == ':nope'
def_after.should == ':nope'
@patched_def.should == ':maybe'
end
it "should work for a method on an instance" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:x), 'instance = X.new', "#@edit instance.x")
def_before.should == ':nope'
def_after.should == ':nope'
@patched_def.should == ':maybe'
end
it "should work for a method from a module" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:a), "#@edit X#a")
def_before.should == ':yup'
def_after.should == ':yup'
@patched_def.should == ':maybe'
end
it "should work for a method with a question mark" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:y?), "#@edit X#y?")
def_before.should == ':because'
def_after.should == ':because'
@patched_def.should == ':maybe'
end
it "should work with nesting" do
def_before, def_after =
apply_monkey_patch(X::B.instance_method(:foo), "#@edit X::B#foo")
def_before.should == ':possibly'
def_after.should == ':possibly'
@patched_def.should == ':maybe'
end
end
end
describe 'on an aliased method' do
before do
@old_editor = Pry.config.editor
Pry.config.editor = lambda do |file, line|
lines = File.read(file).lines.to_a
lines[1] = '"#{super}aa".to_sym' + "\n"
File.open(file, 'w') do |f|
f.write(lines.join)
end
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should change the alias, but not the original, without breaking super" do
pry_eval 'edit-method -p X#c'
Pry::Method.from_str("X#c").alias?.should == true
X.new.b.should == :kinda
X.new.c.should == :kindaaa
end
end
describe 'with three-arg editor' do
before do
@old_editor = Pry.config.editor
@file = @line = @reloading = nil
Pry.config.editor = lambda do |file, line, reloading|
@file = file; @line = line; @reloading = reloading
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should pass the editor a reloading arg" do
pry_eval 'edit-method X.x'
@reloading.should == true
pry_eval 'edit-method -n X.x'
@reloading.should == false
end
end
end
end

View File

@ -307,14 +307,320 @@ describe "edit" do
proc {
pry_eval 'edit ruby.rb -i'
}.should.raise(Pry::CommandError).
message.should =~ /Only one of --ex, --temp, --in and FILE/
message.should =~ /Only one of --ex, --temp, --in and FILE/
end
it "should not work with nonsense" do
proc {
pry_eval 'edit --in three'
}.should.raise(Pry::CommandError).
message.should =~ /Not a valid range: three/
message.should =~ /Not a valid range: three/
end
end
describe "old edit-method tests now migrated to edit" do
describe "on a method defined in a file" do
before do
@tempfile = (Tempfile.new(['pry', '*.rb']))
@tempfile.puts <<-EOS
module A
def a
:yup
end
def b
:kinda
end
end
class X
include A
def self.x
:double_yup
end
def x
:nope
end
def b
super
end
alias c b
def y?
:because
end
class B
G = :nawt
def foo
:possibly
G
end
end
end
EOS
@tempfile.flush
load @tempfile.path
@tempfile_path = Shellwords.escape(@tempfile.path)
end
after do
@tempfile.close(true)
end
describe 'without -p' do
before do
@old_editor = Pry.config.editor
@file = @line = @contents = nil
Pry.config.editor = lambda do |file, line|
@file = file; @line = line
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should correctly find a class method" do
pry_eval 'edit X.x'
@file.should == @tempfile_path
@line.should == 14
end
it "should correctly find an instance method" do
pry_eval 'edit X#x'
@file.should == @tempfile_path
@line.should == 18
end
it "should correctly find a method on an instance" do
pry_eval 'x = X.new', 'edit x.x'
@file.should == @tempfile_path
@line.should == 18
end
it "should correctly find a method from a module" do
pry_eval 'edit X#a'
@file.should == @tempfile_path
@line.should == 2
end
it "should correctly find an aliased method" do
pry_eval 'edit X#c'
@file.should == @tempfile_path
@line.should == 22
end
end
describe 'with -p' do
before do
@old_editor = Pry.config.editor
Pry.config.editor = lambda do |file, line|
lines = File.read(file).lines.to_a
lines[1] = ":maybe\n"
File.open(file, 'w') do |f|
f.write(lines.join)
end
@patched_def = String(lines[1]).chomp
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should successfully replace a class method" do
pry_eval 'edit -p X.x'
class << X
X.method(:x).owner.should == self
end
X.method(:x).receiver.should == X
X.x.should == :maybe
end
it "should successfully replace an instance method" do
pry_eval 'edit -p X#x'
X.instance_method(:x).owner.should == X
X.new.x.should == :maybe
end
it "should successfully replace a method on an instance" do
pry_eval 'instance = X.new', 'edit -p instance.x'
instance = X.new
instance.method(:x).owner.should == X
instance.x.should == :maybe
end
it "should successfully replace a method from a module" do
pry_eval 'edit -p X#a'
X.instance_method(:a).owner.should == A
X.new.a.should == :maybe
end
it "should successfully replace a method with a question mark" do
pry_eval 'edit -p X#y?'
X.instance_method(:y?).owner.should == X
X.new.y?.should == :maybe
end
it "should preserve module nesting" do
pry_eval 'edit -p X::B#foo'
X::B.instance_method(:foo).owner.should == X::B
X::B.new.foo.should == :nawt
end
describe "monkey-patching" do
before do
@edit = 'edit --patch ' # A shortcut.
end
# @param [Integer] lineno
# @return [String] the stripped line from the tempfile at +lineno+
def stripped_line_at(lineno)
@tempfile.rewind
@tempfile.lines.to_a[lineno].strip
end
# Applies the monkey patch for +method+ with help of evaluation of
# +eval_strs+. The idea is to capture the initial line number (before
# the monkey patch), because it gets overwritten by the line number from
# the monkey patch. And our goal is to check that the original
# definition hasn't changed.
# @param [UnboundMethod] method
# @param [Array<String>] eval_strs
# @return [Array<String] the lines with definitions of the same line
# before monkey patching and after (normally, they should be equal)
def apply_monkey_patch(method, *eval_strs)
_, lineno = method.source_location
definition_before = stripped_line_at(lineno)
pry_eval(*eval_strs)
definition_after = stripped_line_at(lineno)
[definition_before, definition_after]
end
it "should work for a class method" do
def_before, def_after =
apply_monkey_patch(X.method(:x), "#@edit X.x")
def_before.should == ':double_yup'
def_after.should == ':double_yup'
@patched_def.should == ':maybe'
end
it "should work for an instance method" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:x), "#@edit X#x")
def_before.should == ':nope'
def_after.should == ':nope'
@patched_def.should == ':maybe'
end
it "should work for a method on an instance" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:x), 'instance = X.new', "#@edit instance.x")
def_before.should == ':nope'
def_after.should == ':nope'
@patched_def.should == ':maybe'
end
it "should work for a method from a module" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:a), "#@edit X#a")
def_before.should == ':yup'
def_after.should == ':yup'
@patched_def.should == ':maybe'
end
it "should work for a method with a question mark" do
def_before, def_after =
apply_monkey_patch(X.instance_method(:y?), "#@edit X#y?")
def_before.should == ':because'
def_after.should == ':because'
@patched_def.should == ':maybe'
end
it "should work with nesting" do
def_before, def_after =
apply_monkey_patch(X::B.instance_method(:foo), "#@edit X::B#foo")
def_before.should == ':possibly'
def_after.should == ':possibly'
@patched_def.should == ':maybe'
end
end
end
describe 'on an aliased method' do
before do
@old_editor = Pry.config.editor
Pry.config.editor = lambda do |file, line|
lines = File.read(file).lines.to_a
lines[1] = '"#{super}aa".to_sym' + "\n"
File.open(file, 'w') do |f|
f.write(lines.join)
end
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should change the alias, but not the original, without breaking super" do
$x = :bebe
pry_eval 'edit -p X#c'
Pry::Method.from_str("X#c").alias?.should == true
X.new.b.should == :kinda
X.new.c.should == :kindaaa
$x = nil
end
end
describe 'with three-arg editor' do
before do
@old_editor = Pry.config.editor
@file = @line = @reloading = nil
Pry.config.editor = lambda do |file, line, reloading|
@file = file; @line = line; @reloading = reloading
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should pass the editor a reloading arg" do
pry_eval 'edit X.x'
@reloading.should == true
pry_eval 'edit -n X.x'
@reloading.should == false
end
end
end
end
end