Skip to content

Commit eb2c7d2

Browse files
authored
Merge pull request #9129 from joshcooper/ruby_warning
(PUP-11974) Only warn when Ruby's verbose is enabled
2 parents 6f19bb8 + f4dc8e4 commit eb2c7d2

File tree

6 files changed

+32
-54
lines changed

6 files changed

+32
-54
lines changed

lib/puppet/util/monkey_patches.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def daemonize
3333
unless Dir.singleton_methods.include?(:exists?)
3434
class Dir
3535
def self.exists?(file_name)
36-
warn('exists? is a deprecated name, use exist? instead')
36+
warn("Dir.exists?('#{file_name}') is deprecated, use Dir.exist? instead") if $VERBOSE
3737
Dir.exist?(file_name)
3838
end
3939
end
@@ -42,7 +42,7 @@ def self.exists?(file_name)
4242
unless File.singleton_methods.include?(:exists?)
4343
class File
4444
def self.exists?(file_name)
45-
warn('exists? is a deprecated name, use exist? instead')
45+
warn("File.exists?('#{file_name}') is deprecated, use File.exist? instead") if $VERBOSE
4646
File.exist?(file_name)
4747
end
4848
end

spec/lib/puppet_spec/verbose.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
# Support code for running stuff with warnings disabled.
1+
# Support code for running stuff with warnings disabled or enabled
22
module Kernel
33
def with_verbose_disabled
44
verbose, $VERBOSE = $VERBOSE, nil
5-
result = yield
6-
$VERBOSE = verbose
7-
return result
5+
begin
6+
yield
7+
ensure
8+
$VERBOSE = verbose
9+
end
10+
end
11+
12+
def with_verbose_enabled
13+
verbose, $VERBOSE = $VERBOSE, true
14+
begin
15+
yield
16+
ensure
17+
$VERBOSE = verbose
18+
end
819
end
920
end

spec/unit/agent_spec.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@ def stop
1515
end
1616
end
1717

18-
def without_warnings
19-
flag = $VERBOSE
20-
$VERBOSE = nil
21-
yield
22-
$VERBOSE = flag
23-
end
24-
2518
describe Puppet::Agent do
2619
before do
2720
@agent = Puppet::Agent.new(AgentTestClient, false)
2821

29-
# make Puppet::Application safe for stubbing; restore in an :after block; silence warnings for this.
30-
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
22+
# make Puppet::Application safe for stubbing
23+
stub_const('Puppet::Application', Class.new(Puppet::Application))
3124
allow(Puppet::Application).to receive(:clear?).and_return(true)
3225
Puppet::Application.class_eval do
3326
class << self
@@ -42,11 +35,6 @@ def controlled_run(&block)
4235
allow(Puppet::SSL::StateMachine).to receive(:new).and_return(machine)
4336
end
4437

45-
after do
46-
# restore Puppet::Application from stub-safe subclass, and silence warnings
47-
without_warnings { Puppet::Application = Puppet::Application.superclass }
48-
end
49-
5038
it "should set its client class at initialization" do
5139
expect(Puppet::Agent.new("foo", false).client_class).to eq("foo")
5240
end

spec/unit/daemon_spec.rb

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
require 'puppet/agent'
44
require 'puppet/configurer'
55

6-
def without_warnings
7-
flag = $VERBOSE
8-
$VERBOSE = nil
9-
yield
10-
$VERBOSE = flag
11-
end
12-
136
describe Puppet::Daemon, :unless => Puppet::Util::Platform.windows? do
147
include PuppetSpec::Files
158

@@ -91,14 +84,8 @@ def run_loop(jobs)
9184
describe "when stopping" do
9285
before do
9386
allow(Puppet::Util::Log).to receive(:close_all)
94-
# to make the global safe to mock, set it to a subclass of itself,
95-
# then restore it in an after pass
96-
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
97-
end
98-
99-
after do
100-
# restore from the superclass so we lose the stub garbage
101-
without_warnings { Puppet::Application = Puppet::Application.superclass }
87+
# to make the global safe to mock, set it to a subclass of itself
88+
stub_const('Puppet::Application', Class.new(Puppet::Application))
10289
end
10390

10491
it 'should request a stop from Puppet::Application' do
@@ -143,11 +130,7 @@ def run_loop(jobs)
143130

144131
describe "when restarting" do
145132
before do
146-
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
147-
end
148-
149-
after do
150-
without_warnings { Puppet::Application = Puppet::Application.superclass }
133+
stub_const('Puppet::Application', Class.new(Puppet::Application))
151134
end
152135

153136
it 'should set Puppet::Application.restart!' do

spec/unit/type/exec_spec.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,7 @@ def exec_stub(options = {})
266266

267267
describe "on platforms where path separator is not :" do
268268
before :each do
269-
@old_verbosity = $VERBOSE
270-
$VERBOSE = nil
271-
@old_separator = File::PATH_SEPARATOR
272-
File::PATH_SEPARATOR = 'q'
273-
end
274-
275-
after :each do
276-
File::PATH_SEPARATOR = @old_separator
277-
$VERBOSE = @old_verbosity
269+
stub_const('File::PATH_SEPARATOR', 'q')
278270
end
279271

280272
it "should use the path separator of the current platform" do

spec/unit/util/monkey_patches_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
expect(Dir.exists?(__dir__)).to be true
1313
end
1414

15-
if RUBY_VERSION >= '3.2'
15+
if RUBY_VERSION >= '3.2'
1616
it 'logs a warning message' do
17-
expect(Dir).to receive(:warn).with('exists? is a deprecated name, use exist? instead')
18-
Dir.exists?(__dir__)
17+
expect(Dir).to receive(:warn).with("Dir.exists?('#{__dir__}') is deprecated, use Dir.exist? instead")
18+
with_verbose_enabled do
19+
Dir.exists?(__dir__)
20+
end
1921
end
2022
end
2123
end
@@ -33,8 +35,10 @@
3335

3436
if RUBY_VERSION >= '3.2'
3537
it 'logs a warning message' do
36-
expect(File).to receive(:warn).with('exists? is a deprecated name, use exist? instead')
37-
File.exists?(__FILE__)
38+
expect(File).to receive(:warn).with("File.exists?('#{__FILE__}') is deprecated, use File.exist? instead")
39+
with_verbose_enabled do
40+
File.exists?(__FILE__)
41+
end
3842
end
3943
end
4044
end

0 commit comments

Comments
 (0)