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

Commit 322dec2

Browse files
committed
Add fail_if_no_examples option: exit 1 if no RSpec examples
Default behavior not changed. CLI option not implemented.
1 parent f81ff4a commit 322dec2

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
Feature: fail if no examples
2+
3+
Use the `fail_if_no_examples` option to make RSpec return non zero exit status
4+
when there are 0 examples.
5+
6+
Scenario: A failing spec example and `fail_if_no_examples` set to true
7+
Given a file named "spec/spec_helper.rb" with:
8+
"""ruby
9+
RSpec.configure { |c| c.fail_if_no_examples = true }
10+
"""
11+
Given a file named "spec/example_spec.rb" with:
12+
"""ruby
13+
require 'spec_helper'
14+
15+
RSpec.describe "something" do
16+
it "fails" do
17+
fail
18+
end
19+
end
20+
"""
21+
When I run `rspec spec/example_spec.rb`
22+
Then the exit status should be 1
23+
24+
Scenario: A failing spec example and `fail_if_no_examples` set to false
25+
Given a file named "spec/spec_helper.rb" with:
26+
"""ruby
27+
RSpec.configure { |c| c.fail_if_no_examples = false }
28+
"""
29+
Given a file named "spec/example_spec.rb" with:
30+
"""ruby
31+
require 'spec_helper'
32+
33+
RSpec.describe "something" do
34+
it "fails" do
35+
fail
36+
end
37+
end
38+
"""
39+
When I run `rspec spec/example_spec.rb`
40+
Then the exit status should be 1
41+
42+
Scenario: A passing spec example and `fail_if_no_examples` set to true
43+
Given a file named "spec/spec_helper.rb" with:
44+
"""ruby
45+
RSpec.configure { |c| c.fail_if_no_examples = true }
46+
"""
47+
Given a file named "spec/example_spec.rb" with:
48+
"""ruby
49+
require 'spec_helper'
50+
51+
RSpec.describe "something" do
52+
it "succeeds" do
53+
true
54+
end
55+
end
56+
"""
57+
When I run `rspec spec/example_spec.rb`
58+
Then the exit status should be 0
59+
60+
Scenario: A passing spec example and `fail_if_no_examples` set to false
61+
Given a file named "spec/spec_helper.rb" with:
62+
"""ruby
63+
RSpec.configure { |c| c.fail_if_no_examples = false }
64+
"""
65+
Given a file named "spec/example_spec.rb" with:
66+
"""ruby
67+
require 'spec_helper'
68+
69+
RSpec.describe "something" do
70+
it "succeeds" do
71+
true
72+
end
73+
end
74+
"""
75+
When I run `rspec spec/example_spec.rb`
76+
Then the exit status should be 0
77+
78+
Scenario: No examples and `fail_if_no_examples` set to true
79+
Given a file named "spec/spec_helper.rb" with:
80+
"""ruby
81+
RSpec.configure { |c| c.fail_if_no_examples = true }
82+
"""
83+
Given a file named "spec/example_spec.rb" with:
84+
"""ruby
85+
require 'spec_helper'
86+
87+
RSpec.describe "something" do
88+
end
89+
"""
90+
When I run `rspec spec/example_spec.rb`
91+
Then the exit status should be 1
92+
93+
Scenario: No examples and `fail_if_no_examples` set to false
94+
Given a file named "spec/spec_helper.rb" with:
95+
"""ruby
96+
RSpec.configure { |c| c.fail_if_no_examples = false }
97+
"""
98+
Given a file named "spec/example_spec.rb" with:
99+
"""ruby
100+
require 'spec_helper'
101+
102+
RSpec.describe "something" do
103+
end
104+
"""
105+
When I run `rspec spec/example_spec.rb`
106+
Then the exit status should be 0

lib/rspec/core/configuration.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ def only_failures_but_not_configured?
199199
# The exit code to return if there are any failures (default: 1).
200200
add_setting :failure_exit_code
201201

202+
# @macro add_setting
203+
# Whether or not to fail when there are no RSpec examples (default: false).
204+
add_setting :fail_if_no_examples
205+
202206
# @macro define_reader
203207
# Indicates files configured to be required.
204208
define_reader :requires
@@ -425,6 +429,7 @@ def initialize
425429
@pattern = '**{,/*/**}/*_spec.rb'
426430
@exclude_pattern = ''
427431
@failure_exit_code = 1
432+
@fail_if_no_examples = false
428433
@spec_files_loaded = false
429434

430435
@backtrace_formatter = BacktraceFormatter.new

lib/rspec/core/runner.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def setup(err, out)
110110
def run_specs(example_groups)
111111
@configuration.reporter.report(@world.example_count(example_groups)) do |reporter|
112112
@configuration.with_suite_hooks do
113+
if @world.example_count(example_groups) == 0 && @configuration.fail_if_no_examples
114+
return 1
115+
end
113116
example_groups.map { |g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
114117
end
115118
end

spec/rspec/core/configuration_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ module RSpec::Core
4242
end
4343
end
4444

45+
describe 'fail_if_no_examples' do
46+
it 'defaults to false' do
47+
expect(RSpec::Core::Configuration.new.fail_if_no_examples).to be(false)
48+
end
49+
50+
it 'can be set to true' do
51+
config.fail_if_no_examples = true
52+
expect(config.fail_if_no_examples).to eq(true)
53+
end
54+
55+
it 'can be set to false' do
56+
config.fail_if_no_examples = false
57+
expect(config.fail_if_no_examples).to eq(false)
58+
end
59+
end
60+
4561
describe '#deprecation_stream' do
4662
it 'defaults to standard error' do
4763
expect($rspec_core_without_stderr_monkey_patch.deprecation_stream).to eq STDERR

0 commit comments

Comments
 (0)