Skip to content

Commit 4c968bd

Browse files
committed
allow silencing warnings
1 parent 7754e02 commit 4c968bd

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

lib/ruby_dep/warning.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ class Warning
66
MSG_INSECURE = 'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
77
' Please upgrade!'.freeze
88

9+
MSG_HOW_TO_DISABLE = ' (To disable warnings, set'\
10+
' RUBY_DEP_GEM_SILENCE_WARNINGS=1)'.freeze
11+
912
def show_warnings
13+
return if silenced?
1014
case check_ruby
1115
when :insecure
12-
STDERR.puts MSG_INSECURE
16+
STDERR.puts MSG_INSECURE + MSG_HOW_TO_DISABLE
1317
when :buggy
14-
STDERR.puts MSG_BUGGY
18+
STDERR.puts MSG_BUGGY + MSG_HOW_TO_DISABLE
1519
when :unknown
1620
else
1721
raise "Unknown problem type: #{problem.inspect}"
@@ -37,5 +41,10 @@ def check_ruby
3741
end
3842
:insecure
3943
end
44+
45+
def silenced?
46+
value = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
47+
(value || '0') !~ /^0|false|no|n$/
48+
end
4049
end
4150
end

spec/lib/ruby_dep/warning_spec.rb

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,57 @@
77
end
88

99
describe '#show_warnings' do
10-
context 'with an up-to-date Ruby' do
11-
let(:ruby_version) { '2.3.1' }
12-
it 'does not show warning' do
13-
expect(STDERR).to_not receive(:puts)
14-
subject.show_warnings
10+
context 'when silenced' do
11+
around do |example|
12+
old = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
13+
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = '1'
14+
example.run
15+
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = old
1516
end
16-
end
1717

18-
context 'with a secure but buggy Ruby' do
19-
let(:ruby_version) { '2.2.4' }
20-
it 'shows warning about bugs' do
21-
expect(STDERR).to receive(:puts).with(
22-
'RubyDep: WARNING: your Ruby is outdated/buggy. Please upgrade.')
23-
subject.show_warnings
18+
context 'with any outdated Ruby' do
19+
let(:ruby_version) { '1.9.3' }
20+
it 'does not show warning' do
21+
expect(STDERR).to_not receive(:puts)
22+
subject.show_warnings
23+
end
2424
end
2525
end
2626

27-
context 'with an insecure Ruby' do
28-
let(:ruby_version) { '2.2.3' }
29-
it 'shows warning about vulnerability' do
30-
expect(STDERR).to receive(:puts).with(
31-
'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
32-
' Please upgrade!')
33-
subject.show_warnings
27+
context 'when not silenced' do
28+
context 'with an up-to-date Ruby' do
29+
let(:ruby_version) { '2.3.1' }
30+
it 'does not show warning' do
31+
expect(STDERR).to_not receive(:puts)
32+
subject.show_warnings
33+
end
34+
end
35+
36+
context 'with a secure but buggy Ruby' do
37+
let(:ruby_version) { '2.2.4' }
38+
it 'shows warning about bugs' do
39+
expect(STDERR).to receive(:puts).with(
40+
%r{RubyDep: WARNING: your Ruby is outdated\/buggy.})
41+
subject.show_warnings
42+
end
43+
end
44+
45+
context 'with an insecure Ruby' do
46+
let(:ruby_version) { '2.2.3' }
47+
it 'shows warning about vulnerability' do
48+
expect(STDERR).to receive(:puts).with(
49+
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
50+
subject.show_warnings
51+
end
3452
end
35-
end
3653

37-
context 'with an unsupported Ruby' do
38-
let(:ruby_version) { '1.9.3' }
39-
it 'shows warning about vulnerability' do
40-
expect(STDERR).to receive(:puts).with(
41-
'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
42-
' Please upgrade!')
43-
subject.show_warnings
54+
context 'with an unsupported Ruby' do
55+
let(:ruby_version) { '1.9.3' }
56+
it 'shows warning about vulnerability' do
57+
expect(STDERR).to receive(:puts).with(
58+
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
59+
subject.show_warnings
60+
end
4461
end
4562
end
4663
end

0 commit comments

Comments
 (0)