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

Commit 7f3ace2

Browse files
committed
Merge pull request #1486 from rspec/return_formatters_dup
Dup formatters to prevent mutation
2 parents 1e4783c + 2a4c77b commit 7f3ace2

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Breaking Changes for 3.0.0:
6464
* Remove `backtrace_cleaner` as an alias of `backtrace_formatter`. (Jon Rowe)
6565
* Remove `filename_pattern` as an alias of `pattern`. (Jon Rowe)
6666
* Extract support for legacy formatters to `rspec-legacy_formatters`. (Jon Rowe)
67+
* `RSpec::Configuration#formatters` now returns a dup to prevent mutation. (Jon Rowe)
6768

6869
Enhancements:
6970

lib/rspec/core/configuration.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,14 @@ def default_formatter=(value)
641641
formatter_loader.default_formatter = value
642642
end
643643

644-
# @private
644+
# Returns a duplicate of the formatters currently loaded in
645+
# the `FormatterLoader` for introspection.
646+
#
647+
# Note as this is a duplicate, any mutations will be disregarded.
648+
#
649+
# @return [Array] the formatters currently loaded
645650
def formatters
646-
formatter_loader.formatters
651+
formatter_loader.formatters.dup
647652
end
648653

649654
# @private

spec/rspec/core/configuration_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,14 @@ def metadata_hash(*args)
853853
end
854854
end
855855

856+
describe "#formatters" do
857+
it "returns a dup of the formatter_loader formatters" do
858+
config.add_formatter 'doc'
859+
config.formatters.clear
860+
expect(config.formatters).to_not eq []
861+
end
862+
end
863+
856864
describe "#default_formatter" do
857865
it 'defaults to `progress`' do
858866
expect(config.default_formatter).to eq('progress')

0 commit comments

Comments
 (0)