File: runner_spec.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (307 lines) | stat: -rw-r--r-- 11,180 bytes parent folder | download
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
require 'rspec/core/bisect/runner'
require 'rspec/core/formatters/bisect_formatter'

module RSpec::Core
  RSpec.describe Bisect::Runner do
    let(:server) { instance_double("RSpec::Core::Bisect::Server", :drb_port => 1234) }
    let(:runner) { described_class.new(server, original_cli_args) }

    describe "#run" do
      let(:original_cli_args) { %w[ spec/1_spec.rb ] }
      let(:target_specs) { %w[ spec/1_spec.rb[1:1] spec/1_spec.rb[1:2] ] }

      it "passes the failed examples from the original run as the expected failures so the runs can abort early" do
        original_results = Formatters::BisectFormatter::RunResults.new(
          [], %w[ spec/failure_spec.rb[1:1] spec/failure_spec.rb[1:2] ]
        )

        expect(server).to receive(:capture_run_results).
          with(original_cli_args).
          ordered.
          and_return(original_results)

        expect(server).to receive(:capture_run_results).
          with(target_specs, original_results.failed_example_ids).
          ordered

        runner.run(target_specs)
      end
    end

    describe "#command_for" do
      def command_for(locations, options={})
        load_path = options.fetch(:load_path) { [] }
        orig_load_path = $LOAD_PATH.dup
        $LOAD_PATH.replace(load_path)
        runner.command_for(locations)
      ensure
        $LOAD_PATH.replace(orig_load_path)
      end

      let(:original_cli_args) { %w[ spec/unit -rfoo -Ibar --warnings --backtrace ] }

      it "includes the original CLI arg options" do
        cmd = command_for(%w[ spec/1.rb spec/2.rb ])
        expect(cmd).to include("-rfoo -Ibar --warnings --backtrace")
      end

      it 'replaces the locations from the original CLI args with the provided locations' do
        cmd = command_for(%w[ spec/1.rb spec/2.rb ])
        expect(cmd).to match(%r{'?spec/1\.rb'? '?spec/2\.rb'?}).and exclude("spec/unit")
      end

      it 'escapes locations' do
        cmd = command_for(["path/with spaces/to/spec.rb"])
        if uses_quoting_for_escaping?
          expect(cmd).to include("'path/with spaces/to/spec.rb'")
        else
          expect(cmd).to include('path/with\ spaces/to/spec.rb')
        end
      end

      it "includes an option for the server's DRB port" do
        cmd = command_for([])
        expect(cmd).to include("--drb-port #{server.drb_port}")
      end

      it "ignores an existing --drb-port option (since we use the server's port instead)" do
        original_cli_args << "--drb-port" << "9999"
        cmd = command_for([])
        expect(cmd).to include("--drb-port #{server.drb_port}").and exclude("9999")
        expect(cmd.scan("--drb-port").count).to eq(1)
      end

      %w[ --bisect --bisect=verbose --bisect=blah ].each do |value|
        it "ignores a `#{value}` option since that would infinitely recurse" do
          original_cli_args << value
          cmd = command_for([])
          expect(cmd).to exclude(value)
        end
      end

      it 'uses the bisect formatter' do
        cmd = command_for([])
        expect(cmd).to include("--format bisect")
      end

      def expect_formatters_to_be_excluded
        cmd = command_for([])
        expect(cmd).to include("--format bisect").and exclude(
          "progress", "html", "--out", "specs.html", "-f ", "-o "
        )
        expect(cmd.scan("--format").count).to eq(1)
      end

      it 'excludes any --format and matching --out options passed in the original args' do
        original_cli_args.concat %w[ --format progress --format html --out specs.html ]
        expect_formatters_to_be_excluded
      end

      it 'excludes any -f <value> and matching -o <value> options passed in the original args' do
        original_cli_args.concat %w[ -f progress -f html -o specs.html ]
        expect_formatters_to_be_excluded
      end

      it 'excludes any -f<value> and matching -o<value> options passed in the original args' do
        original_cli_args.concat %w[ -fprogress -fhtml -ospecs.html ]
        expect_formatters_to_be_excluded
      end

      it 'starts with the path to the current ruby executable' do
        cmd = command_for([])
        expect(cmd).to start_with(File.join(
          RbConfig::CONFIG['bindir'],
          RbConfig::CONFIG['ruby_install_name']
        ))
      end

      it 'includes the path to the rspec executable after the ruby executable' do
        cmd = command_for([])
        expect(cmd).to first_include("ruby").then_include(RSpec::Core.path_to_executable)
      end

      it 'escapes the rspec executable' do
        allow(RSpec::Core).to receive(:path_to_executable).and_return("path/with spaces/rspec")
        cmd = command_for([])

        if uses_quoting_for_escaping?
          expect(cmd).to include("'path/with spaces/rspec'")
        else
          expect(cmd).to include('path/with\ spaces/rspec')
        end
      end

      it 'includes the current load path as an option to `ruby`, not as an option to `rspec`' do
        cmd = command_for([], :load_path => %W[ lp/foo lp/bar ])
        if uses_quoting_for_escaping?
          expect(cmd).to first_include("-I'lp/foo':'lp/bar'").then_include(RSpec::Core.path_to_executable)
        else
          expect(cmd).to first_include("-Ilp/foo:lp/bar").then_include(RSpec::Core.path_to_executable)
        end
      end

      it 'escapes the load path entries' do
        cmd = command_for([], :load_path => ['l p/foo', 'l p/bar' ])
        if uses_quoting_for_escaping?
          expect(cmd).to first_include("-I'l p/foo':'l p/bar'").then_include(RSpec::Core.path_to_executable)
        else
          expect(cmd).to first_include('-Il\ p/foo:l\ p/bar').then_include(RSpec::Core.path_to_executable)
        end
      end
    end

    describe "#repro_command_from", :simulate_shell_allowing_unquoted_ids do
      let(:original_cli_args) { %w[ spec/unit --seed 1234 ] }

      def repro_command_from(ids)
        runner.repro_command_from(ids)
      end

      it 'starts with `rspec #{example_ids}`' do
        cmd = repro_command_from(%w[ ./spec/unit/1_spec.rb[1:1] ./spec/unit/2_spec.rb[1:1] ])
        expect(cmd).to start_with("rspec ./spec/unit/1_spec.rb[1:1] ./spec/unit/2_spec.rb[1:1]")
      end

      it 'includes the original CLI args but excludes the original CLI locations' do
        cmd = repro_command_from(%w[ ./spec/unit/1_spec.rb[1:1] ./spec/unit/2_spec.rb[1:1] ])
        expect(cmd).to include("--seed 1234").and exclude("spec/unit ")
      end

      it 'includes the original SPEC_OPTS but excludes the --bisect flag' do
        with_env_vars('SPEC_OPTS' => '--bisect --seed 1234') do
          cmd = repro_command_from(%w[ ./spec/unit/1_spec.rb[1:1] ])
          expect(cmd).to include('SPEC_OPTS="--seed 1234"').and exclude("--bisect")
        end
      end

      it 'includes original options that `command_for` excludes' do
        original_cli_args << "--format" << "progress"
        expect(runner.command_for(%w[ ./foo.rb[1:1] ])).to exclude("--format progress")
        expect(repro_command_from(%w[ ./foo.rb[1:1] ])).to include("--format progress")
      end

      it 'groups multiple ids for the same file together' do
        cmd = repro_command_from(%w[ ./spec/unit/1_spec.rb[1:1] ./spec/unit/1_spec.rb[1:2] ])
        expect(cmd).to include("./spec/unit/1_spec.rb[1:1,1:2]")
      end

      it 'prints the files in alphabetical order' do
        cmd = repro_command_from(%w[ ./spec/unit/2_spec.rb[1:1] ./spec/unit/1_spec.rb[1:1] ])
        expect(cmd).to include("./spec/unit/1_spec.rb[1:1] ./spec/unit/2_spec.rb[1:1]")
      end

      it 'prints ids from the same file in sequential order' do
        cmd = repro_command_from(%w[
          ./spec/unit/1_spec.rb[2:1]
          ./spec/unit/1_spec.rb[1:2]
          ./spec/unit/1_spec.rb[1:1]
          ./spec/unit/1_spec.rb[1:10]
          ./spec/unit/1_spec.rb[1:9]
        ])

        expect(cmd).to include("./spec/unit/1_spec.rb[1:1,1:2,1:9,1:10,2:1]")
      end

      it 'does not include `--bisect` even though the original args do' do
        original_cli_args << "--bisect"
        expect(repro_command_from(%w[ ./foo.rb[1:1] ])).to exclude("bisect")
      end

      it 'quotes the ids on a shell like ZSH that requires it' do
        with_env_vars 'SHELL' => '/usr/local/bin/zsh' do
          expect(repro_command_from(%w[ ./foo.rb[1:1] ])).to include("'./foo.rb[1:1]'")
        end
      end
    end

    describe "#original_results" do
      let(:original_cli_args) { %w[spec/unit --seed 1234] }

      open3_method = Open3.respond_to?(:capture2e) ? :capture2e : :popen3
      open3_method = :popen3 if RSpec::Support::Ruby.jruby?

      def called_environment
        @called_environment
      end

      if open3_method == :capture2e
        RSpec::Matchers.define :invoke_command_with_env do |command, environment|
          match do |block|
            block.call

            expect(Open3).to have_received(open3_method).with(environment, command)
          end

          supports_block_expectations
        end
      elsif open3_method == :popen3
        RSpec::Matchers.define :invoke_command_with_env do |command, environment|
          match do |block|
            block.call

            expect(Open3).to have_received(open3_method).with(command)
            expect(called_environment).to include(environment)
          end

          supports_block_expectations
        end
      end

      before do
        allow(Open3).to receive(open3_method) do
          @called_environment = ENV.to_hash.dup
          [double("Exit Status"), double("Stdout/err")]
        end

        allow(server).to receive(:capture_run_results) do |&block|
          block.call
          "the results"
        end
      end

      it "runs the suite with the original CLI options" do
        expect {
          runner.original_results
        }.to invoke_command_with_env(a_string_including("--seed 1234"), {})
      end

      context 'when --bisect is present in SPEC_OPTS' do
        it "runs the suite with --bisect removed from the environment" do
          expect {
            with_env_vars 'SPEC_OPTS' => '--bisect --fail-fast' do
              runner.original_results
            end
          }.to invoke_command_with_env(
            a_string_including("--seed 1234"),
            { 'SPEC_OPTS' => '--fail-fast' }
          )
        end
      end

      context 'when --bisect=verbose is present in SPEC_OPTS' do
        it "runs the suite with --bisect removed from the environment" do
          expect {
            with_env_vars 'SPEC_OPTS' => '--bisect=verbose --fail-fast' do
              runner.original_results
            end
          }.to invoke_command_with_env(
            a_string_including("--seed 1234"),
            { 'SPEC_OPTS' => '--fail-fast' }
          )
        end
      end

      it 'returns the run results' do
        expect(runner.original_results).to eq("the results")
      end

      it 'memoizes, since it is expensive to re-run the suite' do
        expect(runner.original_results).to be(runner.original_results)
      end
    end

    def uses_quoting_for_escaping?
      RSpec::Support::OS.windows? || RSpec::Support::Ruby.jruby?
    end
  end
end