diff --git a/lib/pry/editor.rb b/lib/pry/editor.rb index c9c118de..12c34164 100644 --- a/lib/pry/editor.rb +++ b/lib/pry/editor.rb @@ -33,14 +33,18 @@ class Pry # all the flags we want as well as the file and line number we # want to open at. def build_editor_invocation_string(file, line, blocking) - # Sanitize blanks. - file = Shellwords.escape(file) if Pry.config.editor.respond_to?(:call) args = [file, line, blocking][0...(Pry.config.editor.arity)] Pry.config.editor.call(*args) else - "#{Pry.config.editor} #{blocking_flag_for_editor(blocking)} #{start_line_syntax_for_editor(file, line)}" + sanitized_file = if windows? + file.gsub(/\//, '\\') + else + Shellwords.escape(file) + end + + "#{Pry.config.editor} #{blocking_flag_for_editor(blocking)} #{start_line_syntax_for_editor(sanitized_file, line)}" end end @@ -83,10 +87,6 @@ class Pry # Return the syntax for a given editor for starting the editor # and moving to a particular line within that file def start_line_syntax_for_editor(file_name, line_number) - if windows? - file_name = file_name.gsub(/\//, '\\') - end - # special case for 1st line return file_name if line_number <= 1 diff --git a/spec/editor_spec.rb b/spec/editor_spec.rb new file mode 100644 index 00000000..d1a782df --- /dev/null +++ b/spec/editor_spec.rb @@ -0,0 +1,55 @@ +require 'helper' +describe Pry::Editor do + describe "build_editor_invocation_string" do + before do + class << Pry::Editor + public :build_editor_invocation_string + end + end + + it 'should shell-escape files' do + Pry::Editor.build_editor_invocation_string("/tmp/hello world.rb", 5, true).should =~ %r(/tmp/hello\\ world.rb) + end + end + + describe "build_editor_invocation_string on windows" do + before do + class << Pry::Editor + def windows?; true; end + end + end + + after do + class << Pry::Editor + undef windows? + end + end + + it "should replace / by \\" do + Pry::Editor.build_editor_invocation_string("/tmp/hello world.rb", 5, true).should =~ %r(\\tmp\\) + end + + it "should not shell-escape files" do + Pry::Editor.build_editor_invocation_string("/tmp/hello world.rb", 5, true).should =~ %r(hello world.rb) + end + end + + describe 'invoke_editor with a proc' do + before do + @old_editor = Pry.config.editor + Pry.config.editor = proc{ |file, line, blocking| + @file = file + nil + } + end + + after do + Pry.config.editor = @old_editor + end + + it 'should not shell-escape files' do + Pry::Editor.invoke_editor('/tmp/hello world.rb', 10, true) + @file.should == "/tmp/hello world.rb" + end + end +end