1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Also files relative to load path dirs.

Oops, fix typo in comments.

Switch to  backticks instead of plusses.

Set and unset  in a context.

Remove unnecessary #basename.

Remove early File#basename so relative paths actually work.
This commit is contained in:
Shannon Skipper 2013-11-13 20:25:11 -08:00
parent 46c5e800ce
commit abfda989be
3 changed files with 38 additions and 13 deletions

View file

@ -117,19 +117,31 @@ class Pry
end
# @param [String] filename
# @raise [MethodSource::SourceNotFoundError] if the +filename+ is not
# @raise [MethodSource::SourceNotFoundError] if the `filename` is not
# readable for some reason.
# @return [String] absolute path for the given +filename+.
# @return [String] absolute path for the given `filename`.
def abs_path(filename)
abs_path = [File.expand_path(filename, Dir.pwd),
File.expand_path(filename, Pry::INITIAL_PWD)
].detect { |path| File.readable?(path) }
abs_path ||= $LOAD_PATH.map do |path|
File.expand_path(File.basename(filename), path)
end.detect { |path| File.readable?(path) if path }
abs_path = find_path_in_pwd(filename) ||
find_path_in_load_path(filename)
abs_path or raise MethodSource::SourceNotFoundError,
"Cannot open #{filename.inspect} for reading."
end
# @param [String] filename
# @return [String] absolute path for the given `filename` or nil.
def find_path_in_pwd(filename)
[File.expand_path(filename, Dir.pwd),
File.expand_path(filename, Pry::INITIAL_PWD)
].detect { |path| File.readable?(path) if path }
end
# @param [String] filename
# @return [String] absolute path for the given `filename` or nil.
def find_path_in_load_path(filename)
$LOAD_PATH.map do |path|
File.expand_path(filename, path)
end.detect { |path| File.readable?(path) if path }
end
end
# @return [Symbol] The type of code stored in this wrapper.

View file

@ -23,7 +23,7 @@ class Pry
def file_and_line
file_name, line_num = file_with_embedded_line.split(':')
[File.expand_path(file_name), line_num ? line_num.to_i : nil]
[file_name, line_num ? line_num.to_i : nil]
end
def file_name

View file

@ -46,10 +46,23 @@ describe Pry::Code do
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
end
end
should 'find files that are relative to a directory in the $LOAD_PATH' do
$LOAD_PATH << 'spec'
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
describe 'find files that are relative to the $LOAD_PATH' do
before do
$LOAD_PATH << 'spec/commands'
end
after do
$LOAD_PATH.delete 'spec/commands'
end
should 'find files that are in a directory in the $LOAD_PATH' do
Pry::Code.from_file('ls_spec.rb').code_type.should == :ruby
end
should 'find files that are relative to a directory in the $LOAD_PATH' do
Pry::Code.from_file('../helper.rb').code_type.should == :ruby
end
end
end