summaryrefslogtreecommitdiff
path: root/spec/integration/cli_spec.rb
blob: ec5298e79181aaafdb5d9614625291fc0bca0f57 (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
# frozen_string_literal: true

require 'rbconfig'

RSpec.describe 'The bin/pry CLI' do
  let(:call_pry) do
    lambda { |*args|
      pry_dir = File.expand_path(File.join(__FILE__, '../../../lib'))

      # the :err option is equivalent to 2>&1
      out = IO.popen([RbConfig.ruby,
                      "-I",
                      pry_dir,
                      'bin/pry',
                      *args,
                      err: [:child, :out]], &:read)
      status = $CHILD_STATUS

      # Pry will emit silent garbage because of our auto indent feature.
      # This lambda cleans the output of that garbage.
      out = out.strip.sub(/^\e\[0[FG]/, "")

      [out, status]
    }
  end

  context "ARGV forwarding" do
    let(:code) { "p(ARGV) and exit" }

    it "forwards ARGV as an empty array when - is passed without following arguments" do
      out, status = call_pry.call('-e', code, '-')
      expect(status).to be_success
      expect(out).to eq([].inspect)
    end

    it "forwards ARGV as an empty array when -- is passed without following arguments" do
      out, status = call_pry.call('-e', code, '--')
      expect(status).to be_success
      expect(out).to eq([].inspect)
    end

    it "forwards its remaining arguments as ARGV when - is passed" do
      out, status = call_pry.call('-e', code, '-', '1', '-foo', '--bar', '--baz', 'daz')
      expect(status).to be_success
      expect(out).to eq(%w[1 -foo --bar --baz daz].inspect)
    end

    it "forwards its remaining arguments as ARGV when -- is passed" do
      out, status = call_pry.call('-e', code, '--', '1', '-foo', '--bar', '--baz', 'daz')
      expect(status).to be_success
      expect(out).to eq(%w[1 -foo --bar --baz daz].inspect)
    end
  end

  context '-I path' do
    it 'adds an additional path to $LOAD_PATH' do
      code = 'p($LOAD_PATH) and exit'
      out, status = call_pry.call('-I', '/added/at/cli', '-e', code)
      expect(status).to be_success
      expect(out).to include('/added/at/cli')
    end

    it 'adds multiple additional paths to $LOAD_PATH' do
      code = 'p($LOAD_PATH) and exit'
      out, status = call_pry.call('-I', '/added-1/at/cli',
                                  '-I', '/added/at/cli/also',
                                  '-e', code)
      expect(status).to be_success
      expect(out).to include('/added-1/at/cli')
      expect(out).to include('/added/at/cli/also')
    end
  end
end