Skip to content

Commit d055c22

Browse files
authored
Merge pull request #9113 from cthorn42/maint/main/PUP-11945_add_monkey_patch_for_exists
(PUP-11945) Add patches for File and Dir exists? methods
2 parents 58c702e + 1341b92 commit d055c22

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/puppet/util/monkey_patches.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ def daemonize
3030
end
3131
end
3232

33+
unless Dir.singleton_methods.include?(:exists?)
34+
class Dir
35+
def self.exists?(file_name)
36+
Puppet.warning('exists? is a deprecated name, use exist? instead')
37+
Dir.exist?(file_name)
38+
end
39+
end
40+
end
41+
42+
unless File.singleton_methods.include?(:exists?)
43+
class File
44+
def self.exists?(file_name)
45+
Puppet.warning('exists? is a deprecated name, use exist? instead')
46+
File.exist?(file_name)
47+
end
48+
end
49+
end
50+
3351
require_relative '../../puppet/ssl/openssl_loader'
3452
unless Puppet::Util::Platform.jruby_fips?
3553
class OpenSSL::SSL::SSLContext

spec/unit/util/monkey_patches_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
require 'puppet/util/monkey_patches'
44

5+
describe Dir do
6+
describe '.exists?' do
7+
it 'returns false if the directory does not exist' do
8+
expect(Dir.exists?('/madeupdirectory')).to be false
9+
end
10+
11+
it 'returns true if the directory exists' do
12+
expect(Dir.exists?(__dir__)).to be true
13+
end
14+
15+
if RUBY_VERSION >= '3.2'
16+
it 'logs a warning message' do
17+
expect(Puppet).to receive(:warning).with('exists? is a deprecated name, use exist? instead')
18+
Dir.exists?(__dir__)
19+
end
20+
end
21+
end
22+
end
23+
24+
describe File do
25+
describe '.exists?' do
26+
it 'returns false if the directory does not exist' do
27+
expect(File.exists?('spec/unit/util/made_up_file')).to be false
28+
end
29+
30+
it 'returns true if the file exists' do
31+
expect(File.exists?(__FILE__)).to be true
32+
end
33+
34+
if RUBY_VERSION >= '3.2'
35+
it 'logs a warning message' do
36+
expect(Puppet).to receive(:warning).with('exists? is a deprecated name, use exist? instead')
37+
File.exists?(__FILE__)
38+
end
39+
end
40+
end
41+
end
42+
543
describe Symbol do
644
after :all do
745
$unique_warnings.delete('symbol_comparison') if $unique_warnings

0 commit comments

Comments
 (0)