Skip to content

Commit 2f31fd3

Browse files
committed
add recommendations to warnings
1 parent e24792c commit 2f31fd3

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

lib/ruby_dep/warning.rb

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
module RubyDep
22
class Warning
3-
MSG_BUGGY = 'RubyDep: WARNING: your Ruby is outdated/buggy.'\
4-
' Please upgrade.'.freeze
5-
6-
MSG_INSECURE = 'RubyDep: WARNING: your Ruby has security vulnerabilities!'\
7-
' Please upgrade!'.freeze
3+
PREFIX = 'RubyDep: WARNING: '.freeze
4+
MSG_BUGGY = 'Your Ruby is outdated/buggy.'.freeze
5+
MSG_INSECURE = 'Your Ruby has security vulnerabilities!'.freeze
86

97
MSG_HOW_TO_DISABLE = ' (To disable warnings, set'\
108
' RUBY_DEP_GEM_SILENCE_WARNINGS=1)'.freeze
119

10+
OPEN_ISSUE_FOR_UNRECOGNIZED = 'If this version is important,'\
11+
' please open an issue at http://github.com/e2/ruby_dep'.freeze
12+
1213
def show_warnings
1314
return if silenced?
14-
case check_ruby
15+
case (status = check_ruby)
1516
when :insecure
16-
STDERR.puts MSG_INSECURE + MSG_HOW_TO_DISABLE
17+
warn_ruby(MSG_INSECURE, status)
1718
when :buggy
18-
STDERR.puts MSG_BUGGY + MSG_HOW_TO_DISABLE
19+
warn_ruby(MSG_BUGGY, status)
1920
when :unknown
2021
else
2122
raise "Unknown problem type: #{problem.inspect}"
@@ -54,8 +55,42 @@ def silenced?
5455
(value || '0') !~ /^0|false|no|n$/
5556
end
5657

58+
def warn_ruby(msg, status)
59+
STDERR.puts PREFIX + msg + MSG_HOW_TO_DISABLE
60+
STDERR.puts PREFIX + recommendation(status)
61+
end
62+
63+
def recommendation(status)
64+
msg = "Your Ruby is: #{RUBY_VERSION}"
65+
return msg + recommendation_for_unknown unless recognized?
66+
67+
msg += " (#{status})."
68+
msg += " Recommendation: install #{recommended(:unknown).join(' or ')}."
69+
return msg unless status == :insecure
70+
71+
msg + " (Or, at least to #{recommended(:buggy).join(' or ')})"
72+
end
73+
74+
def recommended(status)
75+
current = Gem::Version.new(RUBY_VERSION)
76+
current_ruby_info.select do |key, value|
77+
value == status && Gem::Version.new(key) > current
78+
end.keys.reverse
79+
end
80+
5781
def current_ruby_info
5882
VERSION_INFO[RUBY_ENGINE] || {}
5983
end
84+
85+
def recognized?
86+
current_ruby_info.any?
87+
end
88+
89+
def recommendation_for_unknown
90+
format(
91+
" '%s' (unrecognized). %s", RUBY_ENGINE,
92+
OPEN_ISSUE_FOR_UNRECOGNIZED
93+
)
94+
end
6095
end
6196
end

spec/lib/ruby_dep/warning_spec.rb

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
let(:ruby_engine) { 'ruby' }
1212

13+
def rquote(str)
14+
Regexp.new(Regexp.quote(str))
15+
end
16+
1317
describe '#show_warnings' do
1418
before { subject.show_warnings }
1519
context 'when silenced' do
@@ -40,31 +44,59 @@
4044
let(:ruby_version) { '2.2.4' }
4145
it 'shows warning about bugs' do
4246
expect(STDERR).to have_received(:puts).with(
43-
%r{RubyDep: WARNING: your Ruby is outdated\/buggy.})
47+
%r{Your Ruby is outdated\/buggy.})
48+
end
49+
50+
it 'shows recommended action' do
51+
expected = rquote(
52+
'Your Ruby is: 2.2.4 (buggy). Recommendation: install'\
53+
' 2.2.5 or 2.3.1')
54+
expect(STDERR).to have_received(:puts).with(expected)
4455
end
4556
end
4657

4758
context 'with an insecure Ruby' do
4859
let(:ruby_version) { '2.2.3' }
4960
it 'shows warning about vulnerability' do
5061
expect(STDERR).to have_received(:puts).with(
51-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
62+
/Your Ruby has security vulnerabilities!/)
63+
end
64+
65+
it 'shows recommended action' do
66+
expected = rquote(
67+
'Your Ruby is: 2.2.3 (insecure). Recommendation:'\
68+
' install 2.2.5 or 2.3.1. (Or, at least to 2.2.4 or 2.3.0)')
69+
expect(STDERR).to have_received(:puts).with(expected)
5270
end
5371
end
5472

5573
context 'with an insecure base Ruby' do
5674
let(:ruby_version) { '2.2.0' }
5775
it 'shows warning about vulnerability' do
5876
expect(STDERR).to have_received(:puts).with(
59-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
77+
/Your Ruby has security vulnerabilities!/)
78+
end
79+
80+
it 'shows recommended action' do
81+
expected = rquote(
82+
'Your Ruby is: 2.2.0 (insecure). Recommendation: install 2.2.5'\
83+
' or 2.3.1. (Or, at least to 2.2.4 or 2.3.0)')
84+
expect(STDERR).to have_received(:puts).with(expected)
6085
end
6186
end
6287

6388
context 'with an unsupported Ruby' do
6489
let(:ruby_version) { '1.9.3' }
6590
it 'shows warning about vulnerability' do
6691
expect(STDERR).to have_received(:puts).with(
67-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
92+
/Your Ruby has security vulnerabilities!/)
93+
end
94+
95+
it 'shows recommended action' do
96+
expected = rquote(
97+
'Your Ruby is: 1.9.3 (insecure). Recommendation: install 2.2.5'\
98+
' or 2.3.1. (Or, at least to 2.1.9 or 2.2.4 or 2.3.0)')
99+
expect(STDERR).to have_received(:puts).with(expected)
68100
end
69101
end
70102

@@ -75,6 +107,12 @@
75107
it 'does not show warning about vulnerability' do
76108
expect(STDERR).to_not have_received(:puts)
77109
end
110+
111+
it 'does not show a recommendation' do
112+
expect(STDERR).to_not have_received(:puts).with(
113+
/RubyDep: Your Ruby is:/)
114+
expect(STDERR).to_not have_received(:puts).with(/Recommendation:/)
115+
end
78116
end
79117
end
80118

@@ -84,7 +122,15 @@
84122
let(:ruby_engine) { 'ironruby' }
85123
it 'shows warning about vulnerability' do
86124
expect(STDERR).to have_received(:puts).with(
87-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
125+
/Your Ruby has security vulnerabilities!/)
126+
end
127+
128+
it 'shows recommended action' do
129+
expected = rquote(
130+
"Your Ruby is: 1.2.3 'ironruby' (unrecognized). If this"\
131+
' version is important, please open an issue at'\
132+
' http://github.com/e2/ruby_dep')
133+
expect(STDERR).to have_received(:puts).with(expected)
88134
end
89135
end
90136
end

0 commit comments

Comments
 (0)