Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 6c9e8d8

Browse files
committed
Add fail_if_no_examples option: support custom failure_exit_code
and correct cucumber test to be better rendered on relish
1 parent 5ffb939 commit 6c9e8d8

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
Feature: fail if no examples
22

3-
Use the `fail_if_no_examples` option to make RSpec exit with exit status 1
4-
if there are 0 examples. Using this option, it is recommended to add:
5-
a `--require spec_helper` option to `.rspec` file to ensure the `fail_if_no_examples`
6-
option is set even if no spec files are loaded.
3+
Use the `fail_if_no_examples` option to make RSpec exit with a failure status (by default 1) if there are 0 examples. Using this option, it is recommended to add a `--require spec_helper` option to `.rspec` file to ensure the `fail_if_no_examples` option is set even if no spec files are loaded.
74

8-
This option may be particularly useful when you happen to not run RSpec tests
9-
locally, but rely on CI to do this. This prevents from false positive builds,
10-
when you expected some RSpec examples to be run, but none were run. Such
11-
a situation may be caused by your misconfiguration or regression/major changes
12-
in RSpec.
5+
This option may be particularly useful when you happen to not run RSpec tests locally, but rely on CI to do this. This prevents from false positive builds, when you expected some RSpec examples to be run, but none were run. Such a situation may be caused by your misconfiguration or regression/major changes in RSpec.
136

147
Background:
158
Given a file named "spec/spec_helper.rb" with:
@@ -29,18 +22,10 @@ Feature: fail if no examples
2922
end
3023
"""
3124

32-
Scenario: Examples file has invalid name, thus there are no examples run
25+
Scenario: Examples file name is not matched by RSpec pattern, thus there are no examples run
3326
When I run `rspec`
3427
Then it should fail with "0 examples, 0 failures"
3528

36-
Scenario: Examples file has valid name and 1 example is run
37-
Given a file named "spec/some_spec.rb" with:
38-
"""ruby
39-
RSpec.describe 'something' do
40-
it 'succeeds' do
41-
true
42-
end
43-
end
44-
"""
45-
When I run `rspec --pattern spec/**/*_spec.rb`
29+
Scenario: Examples file name is matched by RSpec pattern, 1 example is run
30+
When I run `rspec --pattern spec/**/*.spec.rb`
4631
Then it should pass with "1 example, 0 failures"

lib/rspec/core/runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def run_specs(example_groups)
112112
@configuration.reporter.report(examples_count) do |reporter|
113113
@configuration.with_suite_hooks do
114114
if examples_count == 0 && @configuration.fail_if_no_examples
115-
return 1
115+
return @configuration.failure_exit_code
116116
end
117117
example_groups.map { |g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
118118
end

spec/integration/fail_if_no_examples_spec.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ def passing_example(fail_if_no_examples)
2121
write_file 'spec/example_spec.rb', passing_example(true)
2222
run_command ""
2323
expect(last_cmd_stdout).to include("1 example, 0 failures")
24-
expect(last_cmd_exitstatus).to eq(0)
24+
expect(last_cmd_exit_status).to eq(0)
2525
end
2626

2727
it 'succeeds if fail_if_no_examples set to false' do
2828
write_file 'spec/example_spec.rb', passing_example(false)
2929
run_command ""
3030
expect(last_cmd_stdout).to include("1 example, 0 failures")
31-
expect(last_cmd_exitstatus).to eq(0)
31+
expect(last_cmd_exit_status).to eq(0)
3232
end
3333
end
3434

@@ -49,14 +49,14 @@ def failing_example(fail_if_no_examples)
4949
write_file 'spec/example_spec.rb', failing_example(true)
5050
run_command ""
5151
expect(last_cmd_stdout).to include("1 example, 1 failure")
52-
expect(last_cmd_exitstatus).to eq(1)
52+
expect(last_cmd_exit_status).to eq(1)
5353
end
5454

5555
it 'fails if fail_if_no_examples set to false' do
5656
write_file 'spec/example_spec.rb', failing_example(false)
5757
run_command ""
5858
expect(last_cmd_stdout).to include("1 example, 1 failure")
59-
expect(last_cmd_exitstatus).to eq(1)
59+
expect(last_cmd_exit_status).to eq(1)
6060
end
6161
end
6262

@@ -74,14 +74,35 @@ def no_examples(fail_if_no_examples)
7474
write_file 'spec/example_spec.rb', no_examples(true)
7575
run_command ""
7676
expect(last_cmd_stdout).to include("0 examples, 0 failures")
77-
expect(last_cmd_exitstatus).to eq(1)
77+
expect(last_cmd_exit_status).to eq(1)
7878
end
7979

8080
it 'succeeds if fail_if_no_examples set to false' do
8181
write_file 'spec/example_spec.rb', no_examples(false)
8282
run_command ""
8383
expect(last_cmd_stdout).to include("0 examples, 0 failures")
84-
expect(last_cmd_exitstatus).to eq(0)
84+
expect(last_cmd_exit_status).to eq(0)
85+
end
86+
87+
context 'when custom failure_exit_code set' do
88+
def no_examples_custom_failure_exit_code(fail_if_no_examples)
89+
"
90+
RSpec.configure do |c|
91+
c.fail_if_no_examples = #{fail_if_no_examples}
92+
c.failure_exit_code = 15
93+
end
94+
95+
RSpec.describe 'something' do
96+
end
97+
"
98+
end
99+
100+
it 'fails if fail_if_no_examples set to true' do
101+
write_file 'spec/example_spec.rb', no_examples_custom_failure_exit_code(true)
102+
run_command ""
103+
expect(last_cmd_stdout).to include("0 examples, 0 failures")
104+
expect(last_cmd_exit_status).to eq(15)
105+
end
85106
end
86107
end
87108
end

spec/support/aruba_support.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module ArubaLoader
1010
let(:stderr) { StringIO.new }
1111
let(:stdout) { StringIO.new }
1212

13-
attr_reader :last_cmd_stdout, :last_cmd_stderr, :last_cmd_exitstatus
13+
attr_reader :last_cmd_stdout, :last_cmd_stderr, :last_cmd_exit_status
1414

1515
def run_command(cmd)
1616
RSpec.configuration.color = true
@@ -24,7 +24,7 @@ def run_command(cmd)
2424

2525
handle_current_dir_change do
2626
in_current_dir do
27-
@last_cmd_exitstatus = RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
27+
@last_cmd_exit_status = RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
2828
end
2929
end
3030
ensure

0 commit comments

Comments
 (0)