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

Commit 7b6517f

Browse files
committed
Fix generated spec_helper so we don't get double formatters.
The spec_helper generated by `rspec --init` could cause both the progress and documentation formatter to be used for a command like: rspec path/to/spec.rb --format progress This happened because formatters are loaded _after_ `--require` files, so the `config.formatters.none?` check returned true even though a formatter had been explicitly set at the command line. It's important that requires come first so that custom formatters can be appropriately loaded.
1 parent 1e5ea31 commit 7b6517f

File tree

6 files changed

+74
-17
lines changed

6 files changed

+74
-17
lines changed

features/command_line/init.feature

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Feature: --init option
44
files for an rspec project. It generates a `.rspec` and
55
`spec/spec_helper.rb` with some example settings to get you started.
66

7+
These settings treat the case where you run an individual spec file
8+
differently, using the documentation formatter if no formatter has
9+
been explicitly set.
10+
711
Scenario: generate .rspec
812
When I run `rspec --init`
913
Then the following files should exist:
@@ -39,19 +43,7 @@ Feature: --init option
3943
end
4044
"""
4145

42-
When I run `rspec`
43-
Then the examples should all pass
44-
And the output should not contain:
45-
"""
46-
Addition
47-
works
48-
"""
49-
50-
When I run `rspec spec/addition_spec.rb`
51-
Then the examples should all pass
52-
And the output should contain:
53-
"""
54-
Addition
55-
works
56-
"""
46+
Then the output from `rspec` should not be in documentation format
47+
But the output from `rspec spec/addition_spec.rb` should be in documentation format
48+
But the output from `rspec spec/addition_spec.rb --format progress` should not be in documentation format
5749

features/step_definitions/additional_cli_steps.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
step %q{the exit status should be 0}
3232
end
3333

34+
addition_example_formatter_output = <<-EOS
35+
Addition
36+
works
37+
EOS
38+
39+
Then /^the output from `([^`]+)` (should(?: not)?) be in documentation format$/ do |cmd, should_or_not|
40+
step %Q{I run `#{cmd}`}
41+
step %q{the examples should all pass}
42+
step %Q{the output from "#{cmd}" #{should_or_not} contain "#{addition_example_formatter_output}"}
43+
end
44+
3445
Then /^the backtrace\-normalized output should contain:$/ do |partial_output|
3546
# ruby 1.9 includes additional stuff in the backtrace,
3647
# so we need to normalize it to compare it with our expected output.

lib/rspec/core/configuration.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,23 @@ def add_formatter(formatter_to_use, *paths)
580580
end
581581
alias_method :formatter=, :add_formatter
582582

583+
# The formatter that will be used if no formatter has been set.
584+
# Defaults to 'progress'.
585+
def default_formatter
586+
formatter_loader.default_formatter
587+
end
588+
589+
# Sets a fallback formatter to use if none other has been set.
590+
#
591+
# @example
592+
#
593+
# RSpec.configure do |rspec|
594+
# rspec.default_formatter = 'doc'
595+
# end
596+
def default_formatter=(value)
597+
formatter_loader.default_formatter = value
598+
end
599+
583600
# @api private
584601
def formatters
585602
formatter_loader.formatters

lib/rspec/core/formatters.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ def self.formatters
8080
def initialize(reporter)
8181
@formatters = []
8282
@reporter = reporter
83+
self.default_formatter = 'progress'
8384
end
8485
attr_reader :formatters, :reporter
86+
attr_accessor :default_formatter
8587

8688
# @api private
8789
def setup_default(output_stream, deprecation_stream)
8890
if @formatters.empty?
89-
add 'progress', output_stream
91+
add default_formatter, output_stream
9092
end
9193
unless @formatters.any? { |formatter| DeprecationFormatter === formatter }
9294
add DeprecationFormatter, deprecation_stream, output_stream

lib/rspec/core/project_initializer/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# Use the documentation formatter for detailed output,
3737
# unless a formatter has already been configured
3838
# (e.g. via a command-line flag).
39-
config.formatter = 'doc' if config.formatters.none?
39+
config.default_formatter = 'doc'
4040
end
4141
4242
# Print the 10 slowest examples and example groups at the

spec/rspec/core/configuration_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,41 @@ def metadata_hash(*args)
824824
end
825825
end
826826

827+
describe "#default_formatter" do
828+
it 'defaults to `progress`' do
829+
expect(config.default_formatter).to eq('progress')
830+
end
831+
832+
it 'remembers changes' do
833+
config.default_formatter = 'doc'
834+
expect(config.default_formatter).to eq('doc')
835+
end
836+
837+
context 'when another formatter has been set' do
838+
it 'does not get used' do
839+
config.default_formatter = 'doc'
840+
config.add_formatter 'progress'
841+
842+
expect(used_formatters).to include(an_instance_of Formatters::ProgressFormatter)
843+
expect(used_formatters).not_to include(an_instance_of Formatters::DocumentationFormatter)
844+
end
845+
end
846+
847+
context 'when no other formatter has been set' do
848+
it 'gets used' do
849+
config.default_formatter = 'doc'
850+
851+
expect(used_formatters).not_to include(an_instance_of Formatters::ProgressFormatter)
852+
expect(used_formatters).to include(an_instance_of Formatters::DocumentationFormatter)
853+
end
854+
end
855+
856+
def used_formatters
857+
config.reporter # to force freezing of formatters
858+
config.formatters
859+
end
860+
end
861+
827862
describe "#filter_run_including" do
828863
it_behaves_like "metadata hash builder" do
829864
def metadata_hash(*args)

0 commit comments

Comments
 (0)