Skip to content

Commit

Permalink
Merge pull request #9129 from joshcooper/ruby_warning
Browse files Browse the repository at this point in the history
(PUP-11974) Only warn when Ruby's verbose is enabled
  • Loading branch information
joshcooper authored Oct 18, 2023
2 parents 6f19bb8 + f4dc8e4 commit eb2c7d2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 54 deletions.
4 changes: 2 additions & 2 deletions lib/puppet/util/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def daemonize
unless Dir.singleton_methods.include?(:exists?)
class Dir
def self.exists?(file_name)
warn('exists? is a deprecated name, use exist? instead')
warn("Dir.exists?('#{file_name}') is deprecated, use Dir.exist? instead") if $VERBOSE
Dir.exist?(file_name)
end
end
Expand All @@ -42,7 +42,7 @@ def self.exists?(file_name)
unless File.singleton_methods.include?(:exists?)
class File
def self.exists?(file_name)
warn('exists? is a deprecated name, use exist? instead')
warn("File.exists?('#{file_name}') is deprecated, use File.exist? instead") if $VERBOSE
File.exist?(file_name)
end
end
Expand Down
19 changes: 15 additions & 4 deletions spec/lib/puppet_spec/verbose.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# Support code for running stuff with warnings disabled.
# Support code for running stuff with warnings disabled or enabled
module Kernel
def with_verbose_disabled
verbose, $VERBOSE = $VERBOSE, nil
result = yield
$VERBOSE = verbose
return result
begin
yield
ensure
$VERBOSE = verbose
end
end

def with_verbose_enabled
verbose, $VERBOSE = $VERBOSE, true
begin
yield
ensure
$VERBOSE = verbose
end
end
end
16 changes: 2 additions & 14 deletions spec/unit/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ def stop
end
end

def without_warnings
flag = $VERBOSE
$VERBOSE = nil
yield
$VERBOSE = flag
end

describe Puppet::Agent do
before do
@agent = Puppet::Agent.new(AgentTestClient, false)

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

after do
# restore Puppet::Application from stub-safe subclass, and silence warnings
without_warnings { Puppet::Application = Puppet::Application.superclass }
end

it "should set its client class at initialization" do
expect(Puppet::Agent.new("foo", false).client_class).to eq("foo")
end
Expand Down
23 changes: 3 additions & 20 deletions spec/unit/daemon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
require 'puppet/agent'
require 'puppet/configurer'

def without_warnings
flag = $VERBOSE
$VERBOSE = nil
yield
$VERBOSE = flag
end

describe Puppet::Daemon, :unless => Puppet::Util::Platform.windows? do
include PuppetSpec::Files

Expand Down Expand Up @@ -91,14 +84,8 @@ def run_loop(jobs)
describe "when stopping" do
before do
allow(Puppet::Util::Log).to receive(:close_all)
# to make the global safe to mock, set it to a subclass of itself,
# then restore it in an after pass
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
end

after do
# restore from the superclass so we lose the stub garbage
without_warnings { Puppet::Application = Puppet::Application.superclass }
# to make the global safe to mock, set it to a subclass of itself
stub_const('Puppet::Application', Class.new(Puppet::Application))
end

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

describe "when restarting" do
before do
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
end

after do
without_warnings { Puppet::Application = Puppet::Application.superclass }
stub_const('Puppet::Application', Class.new(Puppet::Application))
end

it 'should set Puppet::Application.restart!' do
Expand Down
10 changes: 1 addition & 9 deletions spec/unit/type/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,7 @@ def exec_stub(options = {})

describe "on platforms where path separator is not :" do
before :each do
@old_verbosity = $VERBOSE
$VERBOSE = nil
@old_separator = File::PATH_SEPARATOR
File::PATH_SEPARATOR = 'q'
end

after :each do
File::PATH_SEPARATOR = @old_separator
$VERBOSE = @old_verbosity
stub_const('File::PATH_SEPARATOR', 'q')
end

it "should use the path separator of the current platform" do
Expand Down
14 changes: 9 additions & 5 deletions spec/unit/util/monkey_patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
expect(Dir.exists?(__dir__)).to be true
end

if RUBY_VERSION >= '3.2'
if RUBY_VERSION >= '3.2'
it 'logs a warning message' do
expect(Dir).to receive(:warn).with('exists? is a deprecated name, use exist? instead')
Dir.exists?(__dir__)
expect(Dir).to receive(:warn).with("Dir.exists?('#{__dir__}') is deprecated, use Dir.exist? instead")
with_verbose_enabled do
Dir.exists?(__dir__)
end
end
end
end
Expand All @@ -33,8 +35,10 @@

if RUBY_VERSION >= '3.2'
it 'logs a warning message' do
expect(File).to receive(:warn).with('exists? is a deprecated name, use exist? instead')
File.exists?(__FILE__)
expect(File).to receive(:warn).with("File.exists?('#{__FILE__}') is deprecated, use File.exist? instead")
with_verbose_enabled do
File.exists?(__FILE__)
end
end
end
end
Expand Down

0 comments on commit eb2c7d2

Please sign in to comment.