Skip to content

Commit a237663

Browse files
committed
Merge pull request #2 from e2/silence_warnings_with_env
Allow silencing warnings with environment variable
2 parents 8d27fda + 4c968bd commit a237663

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
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: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,59 @@
66
stub_const('RUBY_VERSION', ruby_version)
77
end
88

9-
context 'with an up-to-date Ruby' do
10-
let(:ruby_version) { '2.3.1' }
11-
it '#show_warnings' do
12-
expect(STDERR).to_not receive(:puts)
13-
subject.show_warnings
14-
end
15-
end
9+
describe '#show_warnings' do
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
16+
end
1617

17-
context 'with a secure but buggy Ruby' do
18-
let(:ruby_version) { '2.2.4' }
19-
it '#show_warnings' do
20-
expect(STDERR).to receive(:puts).with(
21-
'RubyDep: WARNING: your Ruby is outdated/buggy. Please upgrade.')
22-
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
24+
end
2325
end
24-
end
2526

26-
context 'with an insecure Ruby' do
27-
let(:ruby_version) { '2.2.3' }
28-
it '#show_warnings' do
29-
expect(STDERR).to receive(:puts).with(
30-
'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
31-
' Please upgrade!')
32-
subject.show_warnings
33-
end
34-
end
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
52+
end
3553

36-
context 'with an unsupported Ruby' do
37-
let(:ruby_version) { '1.9.3' }
38-
it '#show_warnings' do
39-
expect(STDERR).to receive(:puts).with(
40-
'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
41-
' Please upgrade!')
42-
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
61+
end
4362
end
4463
end
4564
end

0 commit comments

Comments
 (0)