2019-05-02 18:33:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-01-13 20:25:36 -05:00
|
|
|
require 'pathname'
|
|
|
|
|
2013-01-13 17:00:22 -05:00
|
|
|
describe Pry::Editor do
|
2013-01-13 20:25:36 -05:00
|
|
|
before do
|
|
|
|
# OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
|
|
|
|
# something "Temp".
|
2013-01-14 05:11:37 -05:00
|
|
|
@tf_dir =
|
2018-11-01 12:16:35 -04:00
|
|
|
if Pry::Helpers::Platform.mri_19?
|
2013-01-14 05:11:37 -05:00
|
|
|
Pathname.new(Dir::Tmpname.tmpdir)
|
2013-01-14 11:43:19 -05:00
|
|
|
else
|
|
|
|
Pathname.new(Dir.tmpdir)
|
2013-01-14 05:11:37 -05:00
|
|
|
end
|
2013-01-13 20:25:36 -05:00
|
|
|
|
2013-01-13 23:55:35 -05:00
|
|
|
@tf_path = File.join(@tf_dir.to_s, 'hello world.rb')
|
2014-04-29 03:27:16 -04:00
|
|
|
|
|
|
|
@editor = Pry::Editor.new(Pry.new)
|
2013-01-13 20:25:36 -05:00
|
|
|
end
|
|
|
|
|
2019-04-09 18:24:47 -04:00
|
|
|
describe ".default" do
|
|
|
|
context "when $VISUAL is defined" do
|
2019-06-02 02:47:51 -04:00
|
|
|
before do
|
|
|
|
allow(Pry::Env).to receive(:[])
|
|
|
|
expect(Pry::Env).to receive(:[]).with('VISUAL').and_return('emacs')
|
|
|
|
end
|
2019-04-09 18:24:47 -04:00
|
|
|
|
|
|
|
it "returns the value of $VISUAL" do
|
|
|
|
expect(described_class.default).to eq('emacs')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when $EDITOR is defined" do
|
2019-06-02 02:47:51 -04:00
|
|
|
before do
|
|
|
|
allow(Pry::Env).to receive(:[])
|
|
|
|
expect(Pry::Env).to receive(:[]).with('EDITOR').and_return('vim')
|
|
|
|
end
|
2019-04-09 18:24:47 -04:00
|
|
|
|
|
|
|
it "returns the value of $EDITOR" do
|
|
|
|
expect(described_class.default).to eq('vim')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when platform is Windows" do
|
|
|
|
before do
|
2019-06-02 02:47:51 -04:00
|
|
|
allow(Pry::Env).to receive(:[])
|
|
|
|
allow(Pry::Env).to receive(:[]).with('VISUAL').and_return(nil)
|
|
|
|
allow(Pry::Env).to receive(:[]).with('EDITOR').and_return(nil)
|
|
|
|
|
2019-04-09 18:24:47 -04:00
|
|
|
allow(Pry::Helpers::Platform).to receive(:windows?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 'notepad'" do
|
|
|
|
expect(described_class.default).to eq('notepad')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when no editor is detected" do
|
2019-06-02 02:47:51 -04:00
|
|
|
before do
|
|
|
|
allow(ENV).to receive(:key?).and_return(false)
|
|
|
|
allow(Kernel).to receive(:system)
|
|
|
|
end
|
2019-04-09 18:24:47 -04:00
|
|
|
|
|
|
|
%w[editor nano vi].each do |text_editor_name|
|
|
|
|
it "shells out to find '#{text_editor_name}'" do
|
|
|
|
expect(Kernel).to receive(:system)
|
|
|
|
.with("which #{text_editor_name} > /dev/null 2>&1")
|
|
|
|
described_class.default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-01 12:16:35 -04:00
|
|
|
describe "build_editor_invocation_string", skip: !Pry::Helpers::Platform.windows? do
|
|
|
|
it 'should shell-escape files' do
|
|
|
|
invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
|
2019-03-02 10:01:04 -05:00
|
|
|
expect(invocation_str).to match(/#{@tf_dir}.+hello\\ world\.rb/)
|
2013-01-13 17:24:58 -05:00
|
|
|
end
|
2013-01-13 17:00:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "build_editor_invocation_string on windows" do
|
2013-01-13 17:24:58 -05:00
|
|
|
before do
|
2018-11-01 12:16:35 -04:00
|
|
|
allow(Pry::Helpers::Platform).to receive(:windows?).and_return(true)
|
2013-01-13 17:24:58 -05:00
|
|
|
end
|
2013-01-13 17:00:22 -05:00
|
|
|
|
2013-01-13 17:24:58 -05:00
|
|
|
it "should not shell-escape files" do
|
2014-04-29 03:27:16 -04:00
|
|
|
invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(invocation_str).to match(/hello world\.rb/)
|
2013-01-13 17:24:58 -05:00
|
|
|
end
|
2013-01-13 17:00:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'invoke_editor with a proc' do
|
2014-04-29 03:27:16 -04:00
|
|
|
it 'should not shell-escape files' do
|
2018-11-11 07:22:03 -05:00
|
|
|
editor = Pry::Editor.new(Pry.new(editor: proc { |file, _line, _blocking|
|
2013-01-13 17:24:58 -05:00
|
|
|
@file = file
|
|
|
|
nil
|
2014-04-29 03:27:16 -04:00
|
|
|
}))
|
2013-01-13 17:00:22 -05:00
|
|
|
|
2014-04-29 03:27:16 -04:00
|
|
|
editor.invoke_editor(@tf_path, 10, true)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@file).to eq(@tf_path)
|
2013-01-13 17:24:58 -05:00
|
|
|
end
|
2013-01-13 17:00:22 -05:00
|
|
|
end
|
|
|
|
end
|