summaryrefslogtreecommitdiff
path: root/spec/pryrc_spec.rb
blob: e5bfe2590c663554f64801e5294388d709495b41 (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
103
104
105
106
107
108
109
# frozen_string_literal: true

describe Pry do
  describe 'loading rc files' do
    before do
      Pry.config.rc_file = 'spec/fixtures/testrc'
      stub_const('Pry::LOCAL_RC_FILE', 'spec/fixtures/testrc/../testrc')

      Pry.instance_variable_set(:@initial_session, true)
      Pry.config.should_load_rc = true
      Pry.config.should_load_local_rc = true
    end

    after do
      Pry.config.should_load_rc = false
      Object.remove_const(:TEST_RC) if defined?(TEST_RC)
    end

    it "should never run the rc file twice" do
      Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)
      expect(TEST_RC).to eq [0]

      Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)
      expect(TEST_RC).to eq [0]
    end

    it "should not load the rc file twice if it's symlinked differently" do
      Pry.config.rc_file = 'spec/fixtures/testrc'
      stub_const('Pry::LOCAL_RC_FILE', 'spec/fixtures/testlinkrc')

      Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)

      expect(TEST_RC).to eq [0]
    end

    it "should not load the pryrc if pryrc's directory permissions do not allow this" do
      Dir.mktmpdir do |dir|
        File.chmod 0o000, dir
        stub_const('Pry::LOCAL_RC_FILE', File.join(dir, '.pryrc'))
        Pry.config.should_load_rc = true
        expect do
          Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)
        end.to_not raise_error
        File.chmod 0o777, dir
      end
    end

    it "should not load the pryrc if it cannot expand ENV[HOME]" do
      old_home = ENV['HOME']
      ENV['HOME'] = nil
      Pry.config.should_load_rc = true
      expect do
        Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)
      end.to_not raise_error

      ENV['HOME'] = old_home
    end

    it "should not run the rc file at all if Pry.config.should_load_rc is false" do
      Pry.config.should_load_rc = false
      Pry.config.should_load_local_rc = false
      Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new)
      expect(Object.const_defined?(:TEST_RC)).to eq false
    end

    describe "that raise exceptions" do
      before do
        Pry.config.rc_file = 'spec/fixtures/testrcbad'
        Pry.config.should_load_local_rc = false

        putsed = nil

        # YUCK! horrible hack to get round the fact that output is not configured
        # at the point this message is printed.
        (class << Pry; self; end).send(:define_method, :puts) do |str|
          putsed = str
        end

        @doing_it = lambda {
          input = StringIO.new("Object::TEST_AFTER_RAISE=1\nexit-all\n")
          Pry.start(self, input: input, output: StringIO.new)
          putsed
        }
      end

      after do
        Object.remove_const(:TEST_BEFORE_RAISE)
        Object.remove_const(:TEST_AFTER_RAISE)
        (class << Pry; undef_method :puts; end)
      end

      it "should not raise exceptions" do
        expect(&@doing_it).to_not raise_error
      end

      it "should continue to run pry" do
        @doing_it[]
        expect(Object.const_defined?(:TEST_BEFORE_RAISE)).to eq true
        expect(Object.const_defined?(:TEST_AFTER_RAISE)).to eq true
      end

      it "should output an error" do
        expect(@doing_it.call.split("\n").first).to match(
          %r{Error loading .*spec/fixtures/testrcbad: messin with ya}
        )
      end
    end
  end
end