Skip to content

Remove deprecated File.exists? #1357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/load_module_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Puppet::Parser::Functions
module_path = function_get_module_path([mod])
metadata_json = File.join(module_path, 'metadata.json')

metadata_exists = File.exists?(metadata_json) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
metadata_exists = File.exist?(metadata_json)
if metadata_exists
metadata = if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
PSON.load(File.read(metadata_json))
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/loadjson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Puppet::Parser::Functions
else
JSON.parse(contents) || args[1]
end
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
elsif File.exist?(args[0])
content = File.read(args[0])
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
PSON.load(content) || args[1]
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/loadyaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module Puppet::Parser::Functions
args[1]
end
YAML.safe_load(contents) || args[1]
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code
elsif File.exist?(args[0])
YAML.load_file(args[0]) || args[1]
else
warning("Can't load '#{args[0]}' File does not exist!")
Expand Down
6 changes: 3 additions & 3 deletions spec/functions/load_module_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

result = subject.execute('science')
Expand All @@ -38,13 +38,13 @@

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

it 'returns nil if user allows empty metadata.json' do
allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/")
allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
allow(File).to receive(:exist?).with("#{prefix}/path/to/module/metadata.json").and_return(false)
result = subject.execute('science', true)
expect(result).to eq({})
end
Expand Down
9 changes: 6 additions & 3 deletions spec/functions/loadjson_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
end

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

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

before(:each) do
allow(File).to receive(:exists?).with(filename).and_return(true).once
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with(filename).and_return(true).once
allow(File).to receive(:read).with(filename).and_return(json).once
if Puppet::PUPPETVERSION[0].to_i < 8
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
Expand Down
9 changes: 6 additions & 3 deletions spec/functions/loadyaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
let(:filename) { '/tmp/doesnotexist' }

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

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

it 'filename /tmp/doesexist' do
expect(File).to receive(:exists?).with(filename).and_return(true).once
allow(File).to receive(:exist?).and_call_original
expect(File).to receive(:exist?).with(filename).and_return(true).once
allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!')
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
end
Expand Down