File: invocations_spec.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (217 lines) | stat: -rw-r--r-- 6,721 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require 'rspec/core/drb'
require 'rspec/core/bisect/coordinator'
require 'rspec/core/project_initializer'

module RSpec::Core
  RSpec.describe Invocations do
    let(:configuration_options) { instance_double(ConfigurationOptions) }
    let(:err) { StringIO.new }
    let(:out) { StringIO.new }

    def run_invocation
      subject.call(configuration_options, err, out)
    end

    describe Invocations::InitializeProject do
      it "initializes a project and returns a 0 exit code" do
        project_init = instance_double(ProjectInitializer, :run => nil)
        allow(ProjectInitializer).to receive_messages(:new => project_init)

        exit_code = run_invocation

        expect(project_init).to have_received(:run)
        expect(exit_code).to eq(0)
      end
    end

    describe Invocations::DRbWithFallback do
      context 'when a DRb server is running' do
        it "builds a DRbRunner and runs the specs" do
          drb_proxy = instance_double(RSpec::Core::DRbRunner, :run => 0)
          allow(RSpec::Core::DRbRunner).to receive(:new).and_return(drb_proxy)

          exit_code = run_invocation

          expect(drb_proxy).to have_received(:run).with(err, out)
          expect(exit_code).to eq(0)
        end
      end

      context 'when a DRb server is not running' do
        let(:runner) { instance_double(RSpec::Core::Runner, :run => 0) }

        before(:each) do
          allow(RSpec::Core::Runner).to receive(:new).and_return(runner)
          allow(RSpec::Core::DRbRunner).to receive(:new).and_raise(DRb::DRbConnError)
        end

        it "outputs a message" do
          run_invocation

          expect(err.string).to include(
            "No DRb server is running. Running in local process instead ..."
          )
        end

        it "builds a runner instance and runs the specs" do
          run_invocation

          expect(RSpec::Core::Runner).to have_received(:new).with(configuration_options)
          expect(runner).to have_received(:run).with(err, out)
        end

        if RSpec::Support::RubyFeatures.supports_exception_cause?
          it "prevents the DRb error from being listed as the cause of expectation failures" do
            allow(RSpec::Core::Runner).to receive(:new) do |configuration_options|
              raise RSpec::Expectations::ExpectationNotMetError
            end

            expect {
              run_invocation
            }.to raise_error(RSpec::Expectations::ExpectationNotMetError) do |e|
              expect(e.cause).to be_nil
            end
          end
        end
      end
    end

    describe Invocations::Bisect do
      let(:original_cli_args) { %w[--bisect --seed 1234] }
      let(:configuration_options) { ConfigurationOptions.new(original_cli_args) }
      let(:success) { true }

      before do
        allow(RSpec::Core::Bisect::Coordinator).to receive(:bisect_with).and_return(success)
      end

      it "starts the bisection coordinator" do
        run_invocation

        expect(RSpec::Core::Bisect::Coordinator).to have_received(:bisect_with).with(
          an_instance_of(Runner),
          configuration_options.args,
          an_instance_of(Formatters::BisectProgressFormatter)
        )
      end

      context "when the bisection is successful" do
        it "returns 0" do
          exit_code = run_invocation

          expect(exit_code).to eq(0)
        end
      end

      context "when the bisection is unsuccessful" do
        let(:success) { false }

        it "returns 1" do
          exit_code = run_invocation

          expect(exit_code).to eq(1)
        end

        context "with a custom failure code set" do
          it "returns the custom failure code" do
            in_sub_process do
              RSpec.configuration.failure_exit_code = 42
              exit_code = run_invocation
              expect(exit_code).to eq(42)
            end
          end
        end
      end

      context "and the verbose option is specified" do
        let(:original_cli_args) { %w[--bisect=verbose --seed 1234] }

        it "starts the bisection coordinator with the debug formatter" do
          run_invocation

          expect(RSpec::Core::Bisect::Coordinator).to have_received(:bisect_with).with(
            an_instance_of(Runner),
            configuration_options.args,
            an_instance_of(Formatters::BisectDebugFormatter)
          )
        end
      end
    end

    describe Invocations::PrintVersion do
      before do
        allow(subject).to receive(:require).and_call_original
        allow(subject).to receive(:require).with("rspec/rails/version").and_raise(LoadError)
      end

      it "prints the major.minor version of RSpec as a whole" do
        stub_const("RSpec::Core::Version::STRING", "9.18.23")
        run_invocation
        expect(out.string).to include("RSpec 9.18\n")
      end

      it "prints off the whole version if it's a pre-release" do
        stub_const("RSpec::Core::Version::STRING", "9.18.0-beta1")
        run_invocation
        expect(out.string).to include("RSpec 9.18.0-beta1\n")
      end

      it "prints off the version of each part of RSpec" do
        [:Core, :Expectations, :Mocks, :Support].each_with_index do |const_name, index|
          # validate that this is an existing const
          expect(RSpec.const_get(const_name)::Version::STRING).to be_a String

          stub_const("RSpec::#{const_name}::Version::STRING", "9.2.#{index}")
        end

        run_invocation

        expect(out.string).to include(
          "- rspec-core 9.2.0",
          "- rspec-expectations 9.2.1",
          "- rspec-mocks 9.2.2",
          "- rspec-support 9.2.3"
        )
      end

      it "indicates a part is not installed if it cannot be loaded" do
        run_invocation

        expect(out.string).not_to include("rspec-rails")
      end

      it "returns a zero exit code" do
        expect(run_invocation).to eq 0
      end
    end

    describe Invocations::PrintHelp do
      let(:parser) { instance_double(OptionParser) }
      let(:invalid_options) { %w[ -d ] }

      subject { described_class.new(parser, invalid_options) }

      before do
        allow(parser).to receive(:to_s).and_return(<<-EOS)
        -d
        --bisect[=verbose]           Repeatedly runs the suite in order...
        EOS
      end

      it "prints the CLI options and returns a zero exit code" do
        exit_code = run_invocation

        expect(exit_code).to eq(0)
        expect(out.string).to include("--bisect")
      end

      it "won't display invalid options in the help output" do
        useless_lines = /^\s*-d\s*$\n/

        run_invocation

        expect(out.string).to_not match(useless_lines)
      end
    end
  end
end