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:
parent
46c5e800ce
commit
abfda989be
3 changed files with 38 additions and 13 deletions
|
@ -117,19 +117,31 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [String] filename
|
# @param [String] filename
|
||||||
# @raise [MethodSource::SourceNotFoundError] if the +filename+ is not
|
# @raise [MethodSource::SourceNotFoundError] if the `filename` is not
|
||||||
# readable for some reason.
|
# readable for some reason.
|
||||||
# @return [String] absolute path for the given +filename+.
|
# @return [String] absolute path for the given `filename`.
|
||||||
def abs_path(filename)
|
def abs_path(filename)
|
||||||
abs_path = [File.expand_path(filename, Dir.pwd),
|
abs_path = find_path_in_pwd(filename) ||
|
||||||
File.expand_path(filename, Pry::INITIAL_PWD)
|
find_path_in_load_path(filename)
|
||||||
].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 or raise MethodSource::SourceNotFoundError,
|
abs_path or raise MethodSource::SourceNotFoundError,
|
||||||
"Cannot open #{filename.inspect} for reading."
|
"Cannot open #{filename.inspect} for reading."
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# @return [Symbol] The type of code stored in this wrapper.
|
# @return [Symbol] The type of code stored in this wrapper.
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Pry
|
||||||
def file_and_line
|
def file_and_line
|
||||||
file_name, line_num = file_with_embedded_line.split(':')
|
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
|
end
|
||||||
|
|
||||||
def file_name
|
def file_name
|
||||||
|
|
|
@ -47,9 +47,22 @@ describe Pry::Code do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should 'find files that are relative to a directory in the $LOAD_PATH' do
|
describe 'find files that are relative to the $LOAD_PATH' do
|
||||||
$LOAD_PATH << 'spec'
|
before do
|
||||||
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
|
$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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue