Skip to content

Commit 2c61c1b

Browse files
authored
Merge pull request #1357 from ekohl/remove-exists
Remove deprecated File.exists?
2 parents d79b6d0 + 43fb939 commit 2c61c1b

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

lib/puppet/parser/functions/load_module_metadata.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Puppet::Parser::Functions
2323
module_path = function_get_module_path([mod])
2424
metadata_json = File.join(module_path, 'metadata.json')
2525

26-
metadata_exists = File.exists?(metadata_json) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
26+
metadata_exists = File.exist?(metadata_json)
2727
if metadata_exists
2828
metadata = if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
2929
PSON.load(File.read(metadata_json))

lib/puppet/parser/functions/loadjson.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module Puppet::Parser::Functions
5555
else
5656
JSON.parse(contents) || args[1]
5757
end
58-
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
58+
elsif File.exist?(args[0])
5959
content = File.read(args[0])
6060
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
6161
PSON.load(content) || args[1]

lib/puppet/parser/functions/loadyaml.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module Puppet::Parser::Functions
5050
args[1]
5151
end
5252
YAML.safe_load(contents) || args[1]
53-
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
53+
elsif File.exist?(args[0])
5454
YAML.load_file(args[0]) || args[1]
5555
else
5656
warning("Can't load '#{args[0]}' File does not exist!")

spec/functions/load_module_metadata_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
it 'jsons parse the file' do
3131
allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
32-
allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(true)
32+
allow(File).to receive(:exist?).with("#{prefix}/path/to/module/metadata.json").and_return(true)
3333
allow(File).to receive(:read).with("#{prefix}/path/to/module/metadata.json").and_return('{"name": "spencer-science"}')
3434

3535
result = subject.execute('science')
@@ -38,13 +38,13 @@
3838

3939
it 'fails by default if there is no metadata.json' do
4040
allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
41-
allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
41+
allow(File).to receive(:exist?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
4242
expect { subject.call(['science']) }.to raise_error(Puppet::ParseError)
4343
end
4444

4545
it 'returns nil if user allows empty metadata.json' do
4646
allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
47-
allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
47+
allow(File).to receive(:exist?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
4848
result = subject.execute('science', true)
4949
expect(result).to eq({})
5050
end

spec/functions/loadjson_spec.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
end
2727

2828
before(:each) do
29-
allow(File).to receive(:exists?).with(filename).and_return(false).once
29+
allow(File).to receive(:exist?).and_call_original
30+
allow(File).to receive(:exist?).with(filename).and_return(false).once
3031
if Puppet::PUPPETVERSION[0].to_i < 8
3132
allow(PSON).to receive(:load).never # rubocop:disable RSpec/ReceiveNever Switching to not_to receive breaks testing in this case
3233
else
@@ -51,7 +52,8 @@
5152
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
5253

5354
before(:each) do
54-
allow(File).to receive(:exists?).with(filename).and_return(true).once
55+
allow(File).to receive(:exist?).and_call_original
56+
allow(File).to receive(:exist?).with(filename).and_return(true).once
5557
allow(File).to receive(:read).with(filename).and_return(json).once
5658
allow(File).to receive(:read).with(filename).and_return(json).once
5759
if Puppet::PUPPETVERSION[0].to_i < 8
@@ -75,7 +77,8 @@
7577
let(:json) { '{"key":"value"}' }
7678

7779
before(:each) do
78-
allow(File).to receive(:exists?).with(filename).and_return(true).once
80+
allow(File).to receive(:exist?).and_call_original
81+
allow(File).to receive(:exist?).with(filename).and_return(true).once
7982
allow(File).to receive(:read).with(filename).and_return(json).once
8083
if Puppet::PUPPETVERSION[0].to_i < 8
8184
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'

spec/functions/loadyaml_spec.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
let(:filename) { '/tmp/doesnotexist' }
1111

1212
it "'default' => 'value'" do
13-
expect(File).to receive(:exists?).with(filename).and_return(false).once
13+
allow(File).to receive(:exist?).and_call_original
14+
expect(File).to receive(:exist?).with(filename).and_return(false).once
1415
expect(YAML).not_to receive(:load_file)
1516
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
1617
end
@@ -21,7 +22,8 @@
2122
let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
2223

2324
it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do
24-
expect(File).to receive(:exists?).with(filename).and_return(true).once
25+
allow(File).to receive(:exist?).and_call_original
26+
expect(File).to receive(:exist?).with(filename).and_return(true).once
2527
expect(YAML).to receive(:load_file).with(filename).and_return(data).once
2628
expect(subject).to run.with_params(filename).and_return(data)
2729
end
@@ -31,7 +33,8 @@
3133
let(:filename) { '/tmp/doesexist' }
3234

3335
it 'filename /tmp/doesexist' do
34-
expect(File).to receive(:exists?).with(filename).and_return(true).once
36+
allow(File).to receive(:exist?).and_call_original
37+
expect(File).to receive(:exist?).with(filename).and_return(true).once
3538
allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!')
3639
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
3740
end

0 commit comments

Comments
 (0)