Skip to content

Commit 55fe71c

Browse files
committed
Merge pull request #9 from e2/show_details_on_warning
allow install on OSX 10.10 (MRI 2.0.0)
2 parents 8202df4 + c14b2f7 commit 55fe71c

File tree

3 files changed

+135
-30
lines changed

3 files changed

+135
-30
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ sudo: false
22
language: ruby
33
bundler_args: --without development
44
rvm:
5+
- 2.0.0
6+
- 2.1.9
57
- 2.2.4
68
- jruby-9.0.5.0
79
before_install: gem install bundler -v 1.12.1

lib/ruby_dep/warning.rb

Lines changed: 48 additions & 10 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}"
@@ -43,8 +44,7 @@ def show_warnings
4344

4445
def check_ruby
4546
version = Gem::Version.new(RUBY_VERSION)
46-
info = VERSION_INFO[RUBY_ENGINE] || {}
47-
info.each do |ruby, status|
47+
current_ruby_info.each do |ruby, status|
4848
return status if version >= Gem::Version.new(ruby)
4949
end
5050
:insecure
@@ -54,5 +54,43 @@ def silenced?
5454
value = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
5555
(value || '0') !~ /^0|false|no|n$/
5656
end
57+
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+
81+
def current_ruby_info
82+
VERSION_INFO[RUBY_ENGINE] || {}
83+
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
5795
end
5896
end

spec/lib/ruby_dep/warning_spec.rb

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
RSpec.describe RubyDep::Warning do
44
before do
5-
allow(STDERR).to receive(:puts)
5+
stub_const('STDERR', instance_double(IO))
66
stub_const('RUBY_VERSION', ruby_version)
77
stub_const('RUBY_ENGINE', ruby_engine)
8+
allow(STDERR).to receive(:puts)
89
end
910

1011
let(:ruby_engine) { 'ruby' }
1112

13+
def rquote(str)
14+
Regexp.new(Regexp.quote(str))
15+
end
16+
1217
describe '#show_warnings' do
18+
before { subject.show_warnings }
1319
context 'when silenced' do
1420
around do |example|
1521
old = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
@@ -21,8 +27,7 @@
2127
context 'with any outdated Ruby' do
2228
let(:ruby_version) { '1.9.3' }
2329
it 'does not show warning' do
24-
expect(STDERR).to_not receive(:puts)
25-
subject.show_warnings
30+
expect(STDERR).to_not have_received(:puts)
2631
end
2732
end
2833
end
@@ -31,35 +36,67 @@
3136
context 'with an up-to-date Ruby' do
3237
let(:ruby_version) { '2.3.1' }
3338
it 'does not show warning' do
34-
expect(STDERR).to_not receive(:puts)
35-
subject.show_warnings
39+
expect(STDERR).to_not have_received(:puts)
3640
end
3741
end
3842

3943
context 'with a secure but buggy Ruby' do
4044
let(:ruby_version) { '2.2.4' }
4145
it 'shows warning about bugs' do
42-
expect(STDERR).to receive(:puts).with(
43-
%r{RubyDep: WARNING: your Ruby is outdated\/buggy.})
44-
subject.show_warnings
46+
expect(STDERR).to have_received(:puts).with(
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)
4555
end
4656
end
4757

4858
context 'with an insecure Ruby' do
4959
let(:ruby_version) { '2.2.3' }
5060
it 'shows warning about vulnerability' do
51-
expect(STDERR).to receive(:puts).with(
52-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
53-
subject.show_warnings
61+
expect(STDERR).to have_received(:puts).with(
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)
70+
end
71+
end
72+
73+
context 'with an insecure base Ruby' do
74+
let(:ruby_version) { '2.2.0' }
75+
it 'shows warning about vulnerability' do
76+
expect(STDERR).to have_received(:puts).with(
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)
5485
end
5586
end
5687

5788
context 'with an unsupported Ruby' do
5889
let(:ruby_version) { '1.9.3' }
5990
it 'shows warning about vulnerability' do
60-
expect(STDERR).to receive(:puts).with(
61-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
62-
subject.show_warnings
91+
expect(STDERR).to have_received(:puts).with(
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)
63100
end
64101
end
65102

@@ -68,20 +105,48 @@
68105
let(:ruby_version) { '2.2.3' }
69106
let(:ruby_engine) { 'jruby' }
70107
it 'does not show warning about vulnerability' do
71-
expect(STDERR).to_not receive(:puts)
72-
subject.show_warnings
108+
expect(STDERR).to_not have_received(:puts)
109+
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:/)
73115
end
74116
end
75117
end
76118

77-
context 'with an untracked ruby' do
119+
context 'with JRuby head' do
78120
context 'when the JRuby is not known to be vulnerable' do
121+
let(:ruby_version) { '2.3.0' }
122+
let(:ruby_engine) { 'jruby' }
123+
it 'does not show warning about vulnerability' do
124+
expect(STDERR).to_not have_received(:puts)
125+
end
126+
127+
it 'does not show a recommendation' do
128+
expect(STDERR).to_not have_received(:puts).with(
129+
/RubyDep: Your Ruby is:/)
130+
expect(STDERR).to_not have_received(:puts).with(/Recommendation:/)
131+
end
132+
end
133+
end
134+
135+
context 'with an untracked ruby' do
136+
context 'when the Ruby is not listed' do
79137
let(:ruby_version) { '1.2.3' }
80138
let(:ruby_engine) { 'ironruby' }
81139
it 'shows warning about vulnerability' do
82-
expect(STDERR).to receive(:puts).with(
83-
/RubyDep: WARNING: your Ruby has security vulnerabilities!/)
84-
subject.show_warnings
140+
expect(STDERR).to have_received(:puts).with(
141+
/Your Ruby has security vulnerabilities!/)
142+
end
143+
144+
it 'shows recommended action' do
145+
expected = rquote(
146+
"Your Ruby is: 1.2.3 'ironruby' (unrecognized). If this"\
147+
' version is important, please open an issue at'\
148+
' http://github.com/e2/ruby_dep')
149+
expect(STDERR).to have_received(:puts).with(expected)
85150
end
86151
end
87152
end

0 commit comments

Comments
 (0)