mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Merge pull request #1279 from JoshCheek/issue-1269-patching-methods
Issue 1269 patching methods
This commit is contained in:
commit
bf3d7cfb69
2 changed files with 56 additions and 3 deletions
|
@ -46,7 +46,7 @@ class Pry
|
|||
# code defined in pry, eval'd within pry.
|
||||
repl_edit
|
||||
elsif runtime_patch?
|
||||
# patch code without persisting changes
|
||||
# patch code without persisting changes, implies future changes are patches
|
||||
apply_runtime_patch
|
||||
else
|
||||
# code stored in actual files, eval'd at top-level
|
||||
|
@ -72,7 +72,7 @@ class Pry
|
|||
end
|
||||
|
||||
def runtime_patch?
|
||||
!file_based_exception? && (opts.present?(:patch) || pry_method?(code_object))
|
||||
!file_based_exception? && (opts.present?(:patch) || previously_patched?(code_object) || pry_method?(code_object))
|
||||
end
|
||||
|
||||
def apply_runtime_patch
|
||||
|
@ -140,6 +140,10 @@ class Pry
|
|||
code_object.pry_method?
|
||||
end
|
||||
|
||||
def previously_patched?(code_object)
|
||||
code_object.is_a?(Pry::Method) && Pry::Method::Patcher.code_for(code_object.source_location.first)
|
||||
end
|
||||
|
||||
def patch_exception?
|
||||
opts.present?(:ex) && opts.present?(:patch)
|
||||
end
|
||||
|
|
|
@ -16,7 +16,6 @@ describe "edit" do
|
|||
end
|
||||
|
||||
describe "with FILE" do
|
||||
|
||||
before do
|
||||
# OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
|
||||
# something "Temp".
|
||||
|
@ -397,6 +396,56 @@ describe "edit" do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'when editing a method by name' do
|
||||
def use_editor(tester, options)
|
||||
initial_editor = tester.pry.editor
|
||||
tester.pry.config.editor = lambda do |filename, line|
|
||||
File.open(filename, 'w') { |f| f.write options.fetch(:replace_all) }
|
||||
nil
|
||||
end
|
||||
tester
|
||||
end
|
||||
|
||||
it 'uses patch editing on methods that were previously patched' do
|
||||
# initial definition
|
||||
tester = pry_tester binding
|
||||
filename = __FILE__
|
||||
line = __LINE__ + 2
|
||||
klass = Class.new do
|
||||
def m; 1; end
|
||||
end
|
||||
klass.new.m.should == 1
|
||||
|
||||
# now patch it
|
||||
use_editor(tester, replace_all: 'def m; 2; end').eval('edit --patch klass#m')
|
||||
klass.new.m.should == 2
|
||||
|
||||
# edit by name, no --patch
|
||||
use_editor(tester, replace_all: 'def m; 3; end').eval("edit klass#m")
|
||||
klass.new.m.should == 3
|
||||
|
||||
# original file is unchanged
|
||||
File.readlines(filename)[line-1].strip.should == 'def m; 1; end'
|
||||
end
|
||||
|
||||
it 'can repeatedly edit methods that were defined in the console' do
|
||||
# initial definition
|
||||
tester = pry_tester binding
|
||||
tester.eval("klass = Class.new do\n"\
|
||||
" def m; 1; end\n"\
|
||||
"end")
|
||||
tester.eval("klass.new.m").should == 1
|
||||
|
||||
# first edit
|
||||
use_editor(tester, replace_all: 'def m; 2; end').eval('edit klass#m')
|
||||
tester.eval('klass.new.m').should == 2
|
||||
|
||||
# repeat edit
|
||||
use_editor(tester, replace_all: 'def m; 3; end').eval('edit klass#m')
|
||||
tester.eval('klass.new.m').should == 3
|
||||
end
|
||||
end
|
||||
|
||||
describe "old edit-method tests now migrated to edit" do
|
||||
describe "on a method defined in a file" do
|
||||
before do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue