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
Fix whereami to work after a chdir in a main script #2312
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'method_source' | ||
require 'open3' | ||
|
||
describe "whereami" do | ||
it 'should work with methods that have been undefined' do | ||
|
@@ -277,4 +278,15 @@ def blimey! | |
it "should work inside an object" do | ||
expect(pry_eval(Object.new, 'whereami')).to match(/Inside #<Object/) | ||
end | ||
|
||
it 'should work after chdir from main script' do | ||
script_dir = File.expand_path(File.join(__dir__, '..', 'fixtures')) | ||
script = 'whereami_main_chdir.rb' | ||
|
||
output, status = Open3.capture2(RbConfig.ruby, script, chdir: script_dir) | ||
|
||
expect(status.success?).to be true | ||
expect(output.strip).to eq File.join(script_dir, script) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are specs failing for Ruby >3.0. link There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Looks like |
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
if $PROGRAM_NAME != __FILE__ | ||
fail "#{__FILE__} should only be executed as a top level program" | ||
end | ||
|
||
$LOAD_PATH.unshift File.expand_path(File.join(__dir__, '..', '..', 'lib')) | ||
|
||
require 'pry' | ||
require 'pry/testable' | ||
|
||
include Pry::Testable::Evalable | ||
|
||
class Cor | ||
def blimey! | ||
Dir.chdir '..' do | ||
pry_eval(binding, 'whereami', '_file_') | ||
end | ||
end | ||
end | ||
|
||
puts Cor.new.blimey! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As File.absolute_path? was added on Ruby 2.7. And we are still supporting older versions, is there any other way to make it compatible between versions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's disappointing. I guess the logic from
absolute_path?
could be back-ported inside of pry/whereami for earlier versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did my best to port the logic of
File.absolute_path?
to plain ruby inside ofwhereami
. Can the tests for versions less than 2.7 be re-run with the latest code?Also, since I copied 3 lines verbatim from a file from the ruby source repo to implement it, what's the best way to deal with the copyright issues? Ruby appears to be available under 2-clause BSD and pry looks to be under MIT, so I think they should be compatible, but I think we'd have to include the copyright info from ruby:
somewhere.