Skip to content

Commit 9c49247

Browse files
committed
(maint) Use stub_const instead of disabling $VERBOSE
Previously we were disabling $VERBOSE so that we could stub a constant during tests without generating warnings like: warning: already initialized constant ... Just use `stub_const`, which doesn't cause those warnings and it automatically restores the original value when the block completes.
1 parent ff1880c commit 9c49247

File tree

3 files changed

+6
-36
lines changed

3 files changed

+6
-36
lines changed

spec/unit/agent_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def stop
1919
before do
2020
@agent = Puppet::Agent.new(AgentTestClient, false)
2121

22-
# make Puppet::Application safe for stubbing; restore in an :after block; silence warnings for this.
23-
with_verbose_disabled { Puppet::Application = Class.new(Puppet::Application) }
22+
# make Puppet::Application safe for stubbing
23+
stub_const('Puppet::Application', Class.new(Puppet::Application))
2424
allow(Puppet::Application).to receive(:clear?).and_return(true)
2525
Puppet::Application.class_eval do
2626
class << self
@@ -35,11 +35,6 @@ def controlled_run(&block)
3535
allow(Puppet::SSL::StateMachine).to receive(:new).and_return(machine)
3636
end
3737

38-
after do
39-
# restore Puppet::Application from stub-safe subclass, and silence warnings
40-
with_verbose_disabled { Puppet::Application = Puppet::Application.superclass }
41-
end
42-
4338
it "should set its client class at initialization" do
4439
expect(Puppet::Agent.new("foo", false).client_class).to eq("foo")
4540
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

0 commit comments

Comments
 (0)