From abfda989be88cd015a489c10738fd674273be6fa Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Wed, 13 Nov 2013 20:25:11 -0800 Subject: [PATCH] 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. --- lib/pry/code.rb | 28 ++++++++++++++++++-------- lib/pry/commands/cat/file_formatter.rb | 2 +- spec/code_spec.rb | 21 +++++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index f91ef223..5fab8091 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -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. diff --git a/lib/pry/commands/cat/file_formatter.rb b/lib/pry/commands/cat/file_formatter.rb index c60c55e8..ad72e456 100644 --- a/lib/pry/commands/cat/file_formatter.rb +++ b/lib/pry/commands/cat/file_formatter.rb @@ -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 diff --git a/spec/code_spec.rb b/spec/code_spec.rb index c81a4ff1..090d55df 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -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