Skip to content

Commit a1d1da4

Browse files
authored
Merge pull request #16 from e2/more_ways_to_disable_warnings
add more warning silencing options
2 parents 0141e1a + dd09dbd commit a1d1da4

File tree

11 files changed

+139
-13
lines changed

11 files changed

+139
-13
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
sudo: false
22
language: ruby
33
bundler_args: --without development
4+
env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false'
45
rvm:
56
- 2.2.5
67
- 2.3.1
78
- jruby-9.1.2.0
9+
810
before_install: gem install bundler -v 1.12.5
911
cache: bundler

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ end
1313

1414
group :test do
1515
gem 'rspec', '~> 3.4'
16+
gem 'gem_isolator', '~> 0.2', '>= 0.2.3'
1617
end

Guardfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ group :specs, halt_on_fail: true do
6363
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
6464
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
6565

66+
watch(%r{^spec/acceptance/fixtures/(.+)\.rb$}) { 'spec/acceptance' }
67+
6668
# Turnip features and steps
6769
watch(%r{^spec/acceptance/(.+)\.feature$})
6870
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|

lib/ruby_dep/quiet.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'ruby_dep/warning'
2+
3+
RubyDep::Warning.new.silence!

lib/ruby_dep/ruby_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
module RubyDep
33
class RubyVersion
4-
attr_reader :status
4+
attr_reader :status # NOTE: monkey-patched by acceptance tests
55
attr_reader :version
66
attr_reader :engine
77

lib/ruby_dep/warning.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Warning
1919

2020
NOTICE_BUGGY_ALTERNATIVE = '(Or, at least to %s)'.freeze
2121

22-
NOTICE_HOW_TO_DISABLE = '(To disable warnings, set'\
23-
" #{DISABLING_ENVIRONMENT_VAR}=1)".freeze
22+
NOTICE_HOW_TO_DISABLE = '(To disable warnings, see:'\
23+
"#{PROJECT_URL}/wiki/Disabling-warnings )".freeze
2424

2525
NOTICE_OPEN_ISSUE = 'If you need this version supported,'\
2626
" please open an issue at #{PROJECT_URL}".freeze
@@ -37,6 +37,10 @@ def show_warnings
3737
raise "Unknown problem type: #{problem.inspect}"
3838
end
3939

40+
def silence!
41+
ENV[DISABLING_ENVIRONMENT_VAR] = '1'
42+
end
43+
4044
private
4145

4246
def silenced?
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'ruby_dep/warning'
2+
3+
# Monkey patch so warning happens on every tested Ruby version
4+
module RubyDep
5+
class RubyVersion
6+
def status
7+
:insecure
8+
end
9+
end
10+
end
11+
12+
RubyDep::Warning.new.show_warnings
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'ruby_dep/quiet'
2+
require 'ruby_dep/warning'
3+
4+
# Monkey patch so warning happens on every tested Ruby version
5+
module RubyDep
6+
class RubyVersion
7+
def status
8+
:insecure
9+
end
10+
end
11+
end
12+
13+
RubyDep::Warning.new.show_warnings

spec/acceptance/warnings_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'gem_isolator'
2+
3+
RSpec.describe 'warnings' do
4+
let(:gems) { [['ruby_dep', { path: Dir.pwd }]] }
5+
6+
let!(:spec_path) do
7+
File.expand_path("spec/acceptance/fixtures/#{spec}")
8+
end
9+
10+
let(:subcmd) do
11+
"bundle exec ruby #{spec_path} 2>&1"
12+
end
13+
14+
let(:code) do
15+
"o=`#{subcmd}`"\
16+
";raise \"Unexpected output: \#{o.inspect}\" unless o.empty?"
17+
end
18+
19+
let(:cmd) do
20+
"ruby -e '#{code}'"
21+
end
22+
23+
def run_isolated(cmd)
24+
GemIsolator.isolate(gems: gems) do |env, isolation|
25+
new_env = env.dup
26+
yield(new_env, isolation) if block_given?
27+
unless isolation.system(env, 'gem install -q bundler')
28+
raise 'failed to install bundler'
29+
end
30+
expect(isolation.system(new_env, cmd)).to eq(true)
31+
end
32+
end
33+
34+
context 'when not silenced' do
35+
let!(:spec) { 'show_warnings.rb' }
36+
37+
let(:code) do
38+
"o=`#{subcmd}`;"\
39+
"unless o =~ /Ruby has security vulnerabilities/\n"\
40+
" raise \"Warning expected, got: \#{o.inspect}\"\n"\
41+
'end'
42+
end
43+
44+
it 'produces a warning' do
45+
run_isolated(cmd)
46+
end
47+
end
48+
49+
context 'when silenced' do
50+
context 'with environment variable' do
51+
let!(:spec) { 'show_warnings.rb' }
52+
53+
it 'produces no output' do
54+
run_isolated(cmd) do |env, _isolation|
55+
env['RUBY_DEP_GEM_SILENCE_WARNINGS'] = '1'
56+
end
57+
end
58+
end
59+
60+
context 'with special include file' do
61+
let!(:spec) { 'warnings_silenced_by_require.rb' }
62+
63+
it 'produces no output' do
64+
run_isolated(cmd)
65+
end
66+
end
67+
end
68+
end

spec/lib/ruby_dep/warning_spec.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,37 @@ def rquote(str)
6363
before { subject.show_warnings }
6464

6565
context 'when silenced' do
66-
around do |example|
67-
old = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
68-
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = '1'
69-
example.run
70-
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = old
66+
context 'with an environment variable' do
67+
around do |example|
68+
old = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
69+
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = '1'
70+
example.run
71+
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = old
72+
end
73+
74+
context 'with any outdated Ruby' do
75+
let(:ruby_version) { outdated_ruby }
76+
it 'does not show anything' do
77+
expect(logger).to_not have_received(:warning)
78+
expect(logger).to_not have_received(:notice)
79+
end
80+
end
7181
end
7282

73-
context 'with any outdated Ruby' do
74-
let(:ruby_version) { outdated_ruby }
75-
it 'does not show anything' do
76-
expect(logger).to_not have_received(:warning)
77-
expect(logger).to_not have_received(:notice)
83+
context 'with a method call' do
84+
around do |example|
85+
old = ENV['RUBY_DEP_GEM_SILENCE_WARNINGS']
86+
subject.silence!
87+
example.run
88+
ENV['RUBY_DEP_GEM_SILENCE_WARNINGS'] = old
89+
end
90+
91+
context 'with any outdated Ruby' do
92+
let(:ruby_version) { outdated_ruby }
93+
it 'does not show anything' do
94+
expect(logger).to_not have_received(:warning)
95+
expect(logger).to_not have_received(:notice)
96+
end
7897
end
7998
end
8099
end

0 commit comments

Comments
 (0)