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

Dup formatters to prevent mutation #1486

Merged
merged 3 commits into from
Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Breaking Changes for 3.0.0:
* Remove `backtrace_cleaner` as an alias of `backtrace_formatter`. (Jon Rowe)
* Remove `filename_pattern` as an alias of `pattern`. (Jon Rowe)
* Extract support for legacy formatters to `rspec-legacy_formatters`. (Jon Rowe)
* `RSpec::Configuration#formatters` now returns a dup to prevent mutation. (Jon Rowe)

Enhancements:

Expand Down
9 changes: 7 additions & 2 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,14 @@ def default_formatter=(value)
formatter_loader.default_formatter = value
end

# @private
# Returns a duplicate of the formatters currently loaded in
# the `FormatterLoader` for introspection.
#
# Note as this is a duplicate, any mutations will be disregarded.
#
# @return [Array] the formatters currently loaded
def formatters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this method public and doc it. Without it being public there's no way to query RSpec to ask what formatters are being used. The doc can mention that a dup is returned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

formatter_loader.formatters
formatter_loader.formatters.dup
end

# @private
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,14 @@ def metadata_hash(*args)
end
end

describe "#formatters" do
it "returns a dup of the formatter_loader formatters" do
config.add_formatter 'doc'
config.formatters.clear
expect(config.formatters).to_not eq []
end
end

describe "#default_formatter" do
it 'defaults to `progress`' do
expect(config.default_formatter).to eq('progress')
Expand Down