summaryrefslogtreecommitdiff
path: root/spec/editor_spec.rb
blob: a89edfd9a686f4c9b220461ccbc06f4e6d22fc68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true

require 'pathname'

describe Pry::Editor do
  before do
    # OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
    # something "Temp".
    @tf_dir = Pathname.new(Dir.tmpdir)
    @tf_path = File.join(@tf_dir.to_s, 'hello world.rb')

    @editor = Pry::Editor.new(Pry.new)
  end

  describe ".default" do
    context "when $VISUAL is defined" do
      before do
        allow(Pry::Env).to receive(:[])
        expect(Pry::Env).to receive(:[]).with('VISUAL').and_return('emacs')
      end

      it "returns the value of $VISUAL" do
        expect(described_class.default).to eq('emacs')
      end
    end

    context "when $EDITOR is defined" do
      before do
        allow(Pry::Env).to receive(:[])
        expect(Pry::Env).to receive(:[]).with('EDITOR').and_return('vim')
      end

      it "returns the value of $EDITOR" do
        expect(described_class.default).to eq('vim')
      end
    end

    context "when platform is Windows" do
      before do
        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)

        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
      before do
        allow(ENV).to receive(:key?).and_return(false)
        allow(Kernel).to receive(:system)
        allow(Pry::Helpers::Platform).to receive(:windows?).and_return(false)
      end

      %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

  describe "build_editor_invocation_string" do
    before do
      allow(Pry::Helpers::Platform).to receive(:windows?).and_return(false)
    end

    it 'should shell-escape files' do
      invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
      expect(invocation_str).to match(/#{@tf_dir}.+hello\\ world\.rb/)
    end
  end

  describe "build_editor_invocation_string on windows" do
    before do
      allow(Pry::Helpers::Platform).to receive(:windows?).and_return(true)
    end

    it "should not shell-escape files" do
      invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
      expect(invocation_str).to match(/hello world\.rb/)
    end
  end

  describe 'invoke_editor with a proc' do
    it 'should not shell-escape files' do
      editor = Pry::Editor.new(Pry.new(editor: proc { |file, _line, _blocking|
        @file = file
        nil
      }))

      editor.invoke_editor(@tf_path, 10, true)
      expect(@file).to eq(@tf_path)
    end
  end
end