Skip to content

Commit 8d27fda

Browse files
committed
Merge pull request #1 from e2/warn_about_outdated_insecure_ruby
detect outdated and insecure Ruby versions
2 parents 70da8b3 + d841fb7 commit 8d27fda

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
Your gem doesn't support all possible Ruby versions.
88

9+
And not all Ruby versions are secure to even have installed.
10+
911
So, you need to tell users which Ruby versions you support in:
1012

1113
1. Your gemspec
1214
2. Your README
1315
3. Your .travis.yml file
16+
4. Any issues you get about which version of Ruby is supported or not
1417

15-
That breaks the principle of single responsibility.
18+
But maintaning that information in 4 different places breaks the principle of
19+
single responsibility.
1620

1721

1822
## The solution
@@ -23,11 +27,14 @@ It assumes you are using Travis and the versions listed in your `.travis.yml` ar
2327

2428
This helps you limit the Ruby versions you support - just by adding/removing entries in your Travis configuration file.
2529

30+
Also, you it can warn users if they are using an outdated version of Ruby.
31+
32+
(Or one with security vulnerabilities).
2633

2734

2835
## Usage
2936

30-
E.g. in your gemspec file:
37+
1. E.g. in your gemspec file:
3138

3239
```ruby
3340
begin
@@ -40,16 +47,22 @@ E.g. in your gemspec file:
4047
s.add_development_dependency 'ruby_dep', '~> 1.0'
4148
```
4249

43-
In your `README.md`:
50+
2. In your `README.md`:
4451

4552
Replace your mentions of "supported Ruby versions" to point to the Travis build.
4653

4754
(Or, you can point to the rubygems.org site where the required Ruby version is listed).
4855

49-
If it works on Travis, it's assumed to be supported, right?
56+
If it works on Travis, it's assumed to be supported, right?
5057

5158
If it fails, it isn't, right?
5259

60+
3. In your library:
61+
62+
require 'ruby_dep/warnings'
63+
RubyDep::Warning.show_warnings
64+
65+
5366
## Roadmap
5467

5568
Pull Requests are welcome.

lib/ruby_dep/warning.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module RubyDep
2+
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
8+
9+
def show_warnings
10+
case check_ruby
11+
when :insecure
12+
STDERR.puts MSG_INSECURE
13+
when :buggy
14+
STDERR.puts MSG_BUGGY
15+
when :unknown
16+
else
17+
raise "Unknown problem type: #{problem.inspect}"
18+
end
19+
end
20+
21+
private
22+
23+
VERSION_INFO = {
24+
'2.3.1' => :unknown,
25+
'2.3.0' => :buggy,
26+
'2.2.5' => :unknown,
27+
'2.2.4' => :buggy,
28+
'2.2.0' => :insecure,
29+
'2.1.9' => :buggy,
30+
'2.0.0' => :insecure
31+
}.freeze
32+
33+
def check_ruby
34+
version = Gem::Version.new(RUBY_VERSION)
35+
VERSION_INFO.each do |ruby, status|
36+
return status if version >= Gem::Version.new(ruby)
37+
end
38+
:insecure
39+
end
40+
end
41+
end

spec/lib/ruby_dep/warning_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'ruby_dep/warning'
2+
3+
RSpec.describe RubyDep::Warning do
4+
before do
5+
allow(STDERR).to receive(:puts)
6+
stub_const('RUBY_VERSION', ruby_version)
7+
end
8+
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
16+
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
23+
end
24+
end
25+
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
35+
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
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)