Skip to content

Commit 1d8cf64

Browse files
committed
Remove Warning.warn override in MSpec and --warnings option
* Overriding Warning.warn cause several problems, including it's much harder and confusing to test Warning.warn in specs. Notably caused by Warning.warn in 2.7 not supporting the category: keyword argument. * specs fail when $VERBOSE is true globally, so there is no real value to filter those $VERBOSE=true-only warnings. * mspec requires ruby 2.7+, which has Warning.warn, so `$VERBOSE = nil unless ENV['OUTPUT_WARNINGS']` was dead code, and so the --warnings option had no effect.
1 parent 1346122 commit 1d8cf64

File tree

5 files changed

+2
-79
lines changed

5 files changed

+2
-79
lines changed

lib/mspec/commands/mspec.rb

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ def options(argv = ARGV)
3838

3939
options.targets
4040

41-
options.on("--warnings", "Don't suppress warnings") do
42-
config[:flags] << '-w'
43-
ENV['OUTPUT_WARNINGS'] = '1'
44-
end
45-
4641
options.on("-j", "--multi", "Run multiple (possibly parallel) subprocesses") do
4742
config[:multi] = true
4843
end

lib/mspec/helpers/io.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize
77
end
88

99
def write(*str)
10-
self << str.join
10+
self << str.join('')
1111
end
1212

1313
def << str
@@ -16,7 +16,7 @@ def << str
1616
end
1717

1818
def print(*str)
19-
write(str.join + $\.to_s)
19+
write(str.join('') + $\.to_s)
2020
end
2121

2222
def method_missing(name, *args, &block)

lib/mspec/matchers/complain.rb

-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ def matches?(proc)
1919
@verbose = $VERBOSE
2020
err = IOStub.new
2121

22-
Thread.current[:in_mspec_complain_matcher] = true
2322
$stderr = err
2423
$VERBOSE = @options.key?(:verbose) ? @options[:verbose] : false
2524
begin
2625
proc.call
2726
ensure
2827
$VERBOSE = @verbose
2928
$stderr = @saved_err
30-
Thread.current[:in_mspec_complain_matcher] = false
3129
end
3230

3331
@warning = err.to_s

lib/mspec/utils/warnings.rb

-43
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,3 @@
88
Warning[:deprecated] = true
99
Warning[:experimental] = false
1010
end
11-
12-
if Object.const_defined?(:Warning) and Warning.respond_to?(:warn)
13-
def Warning.warn(message, category: nil)
14-
# Suppress any warning inside the method to prevent recursion
15-
verbose = $VERBOSE
16-
$VERBOSE = nil
17-
18-
if Thread.current[:in_mspec_complain_matcher]
19-
return $stderr.write(message)
20-
end
21-
22-
case message
23-
# $VERBOSE = true warnings
24-
when /(.+\.rb):(\d+):.+possibly useless use of (<|<=|==|>=|>) in void context/
25-
# Make sure there is a .should otherwise it is missing
26-
line_nb = Integer($2)
27-
unless File.exist?($1) and /\.should(_not)? (<|<=|==|>=|>)/ === File.readlines($1)[line_nb-1]
28-
$stderr.write message
29-
end
30-
when /possibly useless use of (\+|-) in void context/
31-
when /assigned but unused variable/
32-
when /method redefined/
33-
when /previous definition of/
34-
when /instance variable @.+ not initialized/
35-
when /statement not reached/
36-
when /shadowing outer local variable/
37-
when /setting Encoding.default_(in|ex)ternal/
38-
when /unknown (un)?pack directive/
39-
when /(un)?trust(ed\?)? is deprecated/
40-
when /\.exists\? is a deprecated name/
41-
when /Float .+ out of range/
42-
when /passing a block to String#(bytes|chars|codepoints|lines) is deprecated/
43-
when /core\/string\/modulo_spec\.rb:\d+: warning: too many arguments for format string/
44-
when /regexp\/shared\/new_ascii(_8bit)?\.rb:\d+: warning: Unknown escape .+ is ignored/
45-
else
46-
$stderr.write message
47-
end
48-
ensure
49-
$VERBOSE = verbose
50-
end
51-
else
52-
$VERBOSE = nil unless ENV['OUTPUT_WARNINGS']
53-
end

spec/commands/mspec_spec.rb

-27
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,6 @@
9292
end
9393
end
9494

95-
RSpec.describe "The --warnings option" do
96-
before :each do
97-
@options, @config = new_option
98-
allow(MSpecOptions).to receive(:new).and_return(@options)
99-
@script = MSpecMain.new
100-
allow(@script).to receive(:config).and_return(@config)
101-
end
102-
103-
it "is enabled by #options" do
104-
allow(@options).to receive(:on)
105-
expect(@options).to receive(:on).with("--warnings", an_instance_of(String))
106-
@script.options
107-
end
108-
109-
it "sets flags to -w" do
110-
@config[:flags] = []
111-
@script.options ["--warnings"]
112-
expect(@config[:flags]).to include("-w")
113-
end
114-
115-
it "set OUTPUT_WARNINGS = '1' in the environment" do
116-
ENV['OUTPUT_WARNINGS'] = '0'
117-
@script.options ["--warnings"]
118-
expect(ENV['OUTPUT_WARNINGS']).to eq('1')
119-
end
120-
end
121-
12295
RSpec.describe "The -j, --multi option" do
12396
before :each do
12497
@options, @config = new_option

0 commit comments

Comments
 (0)