Only shellword escape filenames in system calls on linux

This commit is contained in:
Conrad Irwin 2013-01-13 14:00:22 -08:00 committed by John Mair
parent e66d2083c1
commit 15caf5fb87
2 changed files with 62 additions and 7 deletions

View File

@ -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

55
spec/editor_spec.rb Normal file
View File

@ -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