Skip to content

Commit cd8a440

Browse files
committed
(PUP-11974) Only warn when Ruby's verbose is enabled
In ruby 2.7.x, the rb_file_exists_p and rb_dir_exists_p methods call rb_warning to log that the methods are deprecated[1][2] However, the rb_warning method is a noop if $VERBOSE is nil or false[3] To preserve the same behavior, only warn when $VERBOSE is truthy. Also include the class name and path in the warning so we can identify and fix the issue. [1] https://github.com/ruby/ruby/blob/1f4d4558484b370999954f3ede7e3aa3a3a01ef3/file.c#L1819 [2] https://github.com/ruby/ruby/blob/1f4d4558484b370999954f3ede7e3aa3a3a01ef3/dir.c#L3301 [3] https://github.com/ruby/ruby/blob/1f4d4558484b370999954f3ede7e3aa3a3a01ef3/error.c#L336-L338
1 parent 6f19bb8 commit cd8a440

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
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/unit/util/monkey_patches_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
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+
$VERBOSE = true
19+
begin
20+
Dir.exists?(__dir__)
21+
rescue
22+
$VERBOSE = nil
23+
end
1924
end
2025
end
2126
end
@@ -33,8 +38,13 @@
3338

3439
if RUBY_VERSION >= '3.2'
3540
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__)
41+
expect(File).to receive(:warn).with("File.exists?('#{__FILE__}') is deprecated, use File.exist? instead")
42+
$VERBOSE = true
43+
begin
44+
File.exists?(__FILE__)
45+
ensure
46+
$VERBOSE = nil
47+
end
3848
end
3949
end
4050
end

0 commit comments

Comments
 (0)