Skip to content

Commit 6d351b1

Browse files
byrootpirj
authored andcommitted
Change config.warnings to be either :all, :none or deprecations_only.
1 parent 5623a34 commit 6d351b1

File tree

7 files changed

+95
-82
lines changed

7 files changed

+95
-82
lines changed

rspec-core/lib/rspec/core/configuration.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def initialize
576576
@default_color = :white
577577
@fixed_color = :blue
578578
@detail_color = :cyan
579-
@deprecation_warnings_set = false
579+
@warnings_set = false
580580
@profile_examples = false
581581
@requires = []
582582
@libs = []
@@ -1766,45 +1766,54 @@ def self.delegate_to_ordering_manager(*methods)
17661766

17671767
# Set Ruby warnings on or off.
17681768
def warnings=(value)
1769-
$VERBOSE = !!value
1769+
@warnings_set = true
1770+
1771+
case value
1772+
when true, false
1773+
RSpec.deprecate(
1774+
"RSpec::Core::Configuration#warnings=",
1775+
:message => "config.warnings = #{value} is deprecated, please set warnings to one of: " \
1776+
"`:none`, `:all` or `:deprecations_only.`"
1777+
)
1778+
value = value ? :all : :none
1779+
end
1780+
1781+
case value
1782+
when :none
1783+
$VERBOSE = false
1784+
self.deprecation_warnings = false
1785+
when :all
1786+
$VERBOSE = true
1787+
self.deprecation_warnings = true
1788+
when :deprecations_only
1789+
$VERBOSE = false
1790+
self.deprecation_warnings = true
1791+
else
1792+
raise "Unsupported value for `warnings` (#{value.inspect}). " \
1793+
"Only `:none`, `:all` and `:deprecations_only` are supported."
1794+
end
17701795
end
17711796

17721797
# @return [Boolean] Whether or not ruby warnings are enabled.
17731798
def warnings?
17741799
$VERBOSE
17751800
end
17761801

1802+
# @private
1803+
def warnings_set?
1804+
@warnings_set
1805+
end
1806+
17771807
if defined?(::Warning) && ::Warning.respond_to?(:[]=)
1778-
# Set Ruby deprecation warnings on or off.
1808+
# @private
17791809
def deprecation_warnings=(value)
1780-
@deprecation_warnings_set = true
17811810
::Warning[:deprecated] = value
17821811
end
1783-
1784-
# @return [Boolean] Whether or not ruby warnings are enabled.
1785-
def deprecation_warnings?
1786-
::Warning[:deprecated]
1787-
end
1788-
1789-
# @private
1790-
def deprecation_warnings_set?
1791-
@deprecation_warnings_set || ::Warning[:deprecated]
1792-
end
1793-
else # Rubies older than 2.7
1812+
else
17941813
# :nocov:
17951814
# Set Ruby deprecation warnings on or off.
17961815
def deprecation_warnings=(_value)
17971816
end
1798-
1799-
# @return [Boolean] Whether or not ruby warnings are enabled.
1800-
def deprecation_warnings?
1801-
false
1802-
end
1803-
1804-
# @private
1805-
def deprecation_warnings_set?
1806-
true
1807-
end
18081817
# :nocov:
18091818
end
18101819

rspec-core/lib/rspec/core/project_initializer/spec/spec_helper.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
# triggering implicit auto-inclusion in groups with matching metadata.
4545
config.shared_context_metadata_behavior = :apply_to_host_groups
4646

47-
# This settings enables Ruby deprecations warnings. It's highly recommended,
48-
# and much less noisy than `config.warnings`
49-
config.deprecation_warnings = true
47+
# This setting enables Ruby deprecation warnings. It's recommended, but in some cases may
48+
# be too noisy due to issues in dependencies. It can be set to `:all` or `:none`
49+
# to either display all warnings or none of them.
50+
config.warnings = :deprecations_only
5051

5152
# The settings below are suggested to provide a good initial experience
5253
# with RSpec, but feel free to customize to your heart's content.
@@ -68,10 +69,6 @@
6869
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
6970
config.disable_monkey_patching!
7071
71-
# This setting enables warnings. It's recommended, but in some cases may
72-
# be too noisy due to issues in dependencies.
73-
config.warnings = true
74-
7572
# Many RSpec users commonly either run the entire suite or an individual
7673
# file, and it's useful to allow more verbose output when running an
7774
# individual spec file.

rspec-core/lib/rspec/core/runner.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ def configure(err, out)
131131
@configuration.output_stream = out if @configuration.output_stream == $stdout
132132
@options.configure(@configuration)
133133

134-
unless @configuration.deprecation_warnings_set?
135-
RSpec.warning "config.deprecation_warnings isn't set. Please set it to either `true` or `false` " \
136-
"to either display Ruby deprecation warnings or to hide them. Displaying them is highly" \
137-
"recommended to receive advance notice of breaking changes."
134+
unless @configuration.warnings_set?
135+
RSpec.warning "config.warnings isn't set. Please set it to either `:all`, `:deprecations_only`, or " \
136+
"`:none`. Setting it to at least `:deprecations_only` is highly" \
137+
"recommended to receive advance notice of future Ruby breaking changes.",
138+
:call_site => nil
138139
end
139140
end
140141

rspec-core/spec/rspec/core/configuration_spec.rb

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,60 +2691,63 @@ def strategy.order(list)
26912691
end
26922692
end
26932693

2694-
describe '#warnings' do
2695-
around do |example|
2696-
original_setting = $VERBOSE
2697-
example.run
2698-
$VERBOSE = original_setting
2699-
end
2700-
2701-
it "sets verbose to true when true" do
2702-
config.warnings = true
2703-
expect($VERBOSE).to eq true
2704-
end
2694+
if defined?(::Warning) && ::Warning.respond_to?(:[]=)
2695+
describe '#warnings' do
2696+
around do |example|
2697+
original_deprecated = ::Warning[:deprecated]
2698+
original_setting = $VERBOSE
2699+
example.run
2700+
$VERBOSE = original_setting
2701+
::Warning[:deprecated] = original_deprecated
2702+
end
27052703

2706-
it "sets verbose to false when false" do
2707-
config.warnings = false
2708-
expect($VERBOSE).to eq false
2709-
end
2704+
it "sets $VERBOSE and Warning[:deprecated] to true when true" do
2705+
config.warnings = true
2706+
expect($VERBOSE).to eq true
2707+
expect(::Warning[:deprecated]).to eq true
2708+
end
27102709

2711-
it 'returns the verbosity setting' do
2712-
config.warnings = true
2713-
expect(config.warnings?).to eq true
2710+
it "sets $VERBOSE and Warning[:deprecated] to false when false" do
2711+
config.warnings = false
2712+
expect($VERBOSE).to eq false
2713+
expect(::Warning[:deprecated]).to eq false
2714+
end
27142715

2715-
config.warnings = false
2716-
expect(config.warnings?).to eq false
2717-
end
2716+
it "sets $VERBOSE and Warning[:deprecated] to true when :all" do
2717+
config.warnings = :all
2718+
expect($VERBOSE).to eq true
2719+
expect(::Warning[:deprecated]).to eq true
2720+
end
27182721

2719-
it 'is loaded from config by #force' do
2720-
config.force :warnings => true
2721-
expect($VERBOSE).to eq true
2722-
end
2723-
end
2722+
it "sets $VERBOSE and Warning[:deprecated] to false when :none" do
2723+
config.warnings = :none
2724+
expect($VERBOSE).to eq false
2725+
expect(::Warning[:deprecated]).to eq false
2726+
end
27242727

2725-
describe '#deprecation_warnings' do
2726-
around do |example|
2727-
original_setting = ::Warning[:deprecated]
2728-
example.run
2729-
::Warning[:deprecated] = original_setting
2730-
end
2728+
it "sets $VERBOSE to false but Warning[:deprecated] to true when :deprecations_only" do
2729+
config.warnings = :deprecations_only
2730+
expect($VERBOSE).to eq false
2731+
expect(::Warning[:deprecated]).to eq true
2732+
end
27312733

2732-
if defined?(::Warning) && ::Warning.respond_to?(:[]=)
2733-
it "sets Warning[:deprecated] to true when true" do
2734-
config.deprecation_warnings = true
2735-
expect(Warning[:deprecated]).to eq true
2736-
expect(config.deprecation_warnings?).to eq true
2734+
it "raises on unknown warnings value" do
2735+
expect {
2736+
config.warnings = :unknown
2737+
}.to raise_error(a_string_including('Unsupported value for `warnings` (:unknown)'))
27372738
end
27382739

2739-
it "sets Warning[:deprecated] to false when false" do
2740-
config.deprecation_warnings = false
2741-
expect(Warning[:deprecated]).to eq false
2742-
expect(config.deprecation_warnings?).to eq false
2740+
it 'returns the verbosity setting' do
2741+
config.warnings = true
2742+
expect(config.warnings?).to eq true
2743+
2744+
config.warnings = false
2745+
expect(config.warnings?).to eq false
27432746
end
27442747

2745-
it "sets #deprecation_warnings_set?" do
2746-
config.deprecation_warnings = false
2747-
expect(config.deprecation_warnings_set?).to eq true
2748+
it 'is loaded from config by #force' do
2749+
config.force :warnings => true
2750+
expect($VERBOSE).to eq true
27482751
end
27492752
end
27502753
end

rspec-core/spec/rspec/core/runner_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ def interrupt
369369
allow(config).to receive(:load_spec_files)
370370
allow(config).to receive(:reporter).and_return(double.as_null_object)
371371

372-
expect(config).to receive(:deprecation_warnings_set?).and_return(false)
373-
expect(RSpec).to receive(:warning).with(/deprecation_warnings/)
372+
expect(config).to receive(:warnings_set?).and_return(false)
373+
expect(RSpec).to receive(:warning).with(/config\.warnings isn't set/, :call_site => nil)
374374

375375
runner = build_runner
376376
runner.setup err, stdout

rspec-core/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def self.new(*args, &block)
1010
# We detect ruby warnings via $stderr,
1111
# so direct our deprecations to $stdout instead.
1212
config.deprecation_stream = $stdout
13+
config.warnings = :all
1314
end
1415
end
1516
end

rspec-support/lib/rspec/support/spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
c.include RSpec::Support::FormattingSupport
2020
c.include RSpec::Support::InSubProcess
2121

22+
c.warnings = :all
23+
2224
unless defined?(Debugger) # debugger causes warnings when used
2325
c.before do
2426
warning_preventer.reset!

0 commit comments

Comments
 (0)