Skip to content

Commit 671182f

Browse files
committed
[GR-17457] Remove Warning.warn override in MSpec and --warnings option
PullRequest: truffleruby/3674
2 parents 49eaf5b + 5c8d013 commit 671182f

File tree

10 files changed

+65
-79
lines changed

10 files changed

+65
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Bug fixes:
116116
* Fix `Kernel#sleep` and `Mutex#sleep` for durations smaller than 1 millisecond (#2716, @eregon).
117117
* Fix `IO#{wait,wait_readable,wait_writable}` with a timeout > INT_MAX seconds (@eregon).
118118
* Use the compatible encoding for `String#{sub,gsub,index,rindex}` (#2749, @eregon).
119+
* Fix `Warning#warn` called with category specified is no longer throwing exception (#20446, @horakivo).
119120

120121
Compatibility:
121122

spec/mspec/lib/mspec/commands/mspec.rb

Lines changed: 0 additions & 5 deletions
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

spec/mspec/lib/mspec/helpers/io.rb

Lines changed: 2 additions & 2 deletions
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)

spec/mspec/lib/mspec/matchers/complain.rb

Lines changed: 0 additions & 2 deletions
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

spec/mspec/lib/mspec/utils/warnings.rb

Lines changed: 0 additions & 43 deletions
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/mspec/spec/commands/mspec_spec.rb

Lines changed: 0 additions & 27 deletions
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

spec/ruby/core/warning/warn_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,54 @@ def Warning.warn(msg)
7373
$VERBOSE = verbose
7474
end
7575
end
76+
77+
it "should print message when category is :deprecated and Warning[:deprecated] is true" do
78+
warn_deprecated = Warning[:deprecated]
79+
Warning[:deprecated] = true
80+
begin
81+
-> {
82+
warn("foo", category: :deprecated)
83+
}.should complain("foo\n")
84+
ensure
85+
Warning[:deprecated] = warn_deprecated
86+
end
87+
end
88+
89+
it "should print message when category is :experimental and Warning[:experimental] is true" do
90+
warn_experimental = Warning[:experimental]
91+
Warning[:experimental] = true
92+
begin
93+
-> {
94+
warn("foo", category: :experimental)
95+
}.should complain("foo\n")
96+
ensure
97+
Warning[:experimental] = warn_experimental
98+
end
99+
end
100+
101+
it "should not print message when category is :deprecated but Warning[:deprecated] is false" do
102+
warn_deprecated = Warning[:deprecated]
103+
Warning[:deprecated] = false
104+
begin
105+
-> {
106+
warn("foo", category: :deprecated)
107+
}.should_not complain
108+
ensure
109+
Warning[:deprecated] = warn_deprecated
110+
end
111+
end
112+
113+
it "should not print message when category is :experimental but Warning[:experimental] is false" do
114+
warn_experimental = Warning[:experimental]
115+
Warning[:experimental] = false
116+
begin
117+
-> {
118+
warn("foo", category: :experimental)
119+
}.should_not complain
120+
ensure
121+
Warning[:experimental] = warn_experimental
122+
end
123+
end
76124
end
77125

78126
ruby_version_is ''...'3.0' do

spec/ruby/language/predefined_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,10 @@ def obj.foo2; yield; end
979979
$VERBOSE = @verbose
980980
end
981981

982+
it "is false by default" do
983+
$VERBOSE.should be_false
984+
end
985+
982986
it "converts truthy values to true" do
983987
[true, 1, 0, [], ""].each do |true_value|
984988
$VERBOSE = true_value

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
10481048
"/core/truffle/method_operations.rb",
10491049
"/core/method.rb",
10501050
"/core/unbound_method.rb",
1051+
"/core/truffle/warning_operations.rb",
10511052
"/core/warning.rb",
10521053
"/core/weakmap.rb",
10531054
"/core/tracepoint.rb",

src/main/ruby/truffleruby/core/warning.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ def warn(message, category: nil)
1616
unless message.encoding.ascii_compatible?
1717
raise Encoding::CompatibilityError, "ASCII incompatible encoding: #{message.encoding}"
1818
end
19+
1920
unless Primitive.nil?(category)
2021
Truffle::Type.rb_check_type(category, Symbol)
2122
Truffle::WarningOperations.check_category(category)
23+
24+
if category == :deprecated && !Warning[:deprecated]
25+
return nil
26+
end
27+
28+
if category == :experimental && !Warning[:experimental]
29+
return nil
30+
end
2231
end
2332

2433
$stderr.write message

0 commit comments

Comments
 (0)