diff --git a/lib/puppet/parser/functions/load_module_metadata.rb b/lib/puppet/parser/functions/load_module_metadata.rb index 9a44b6186..d5c6672b8 100644 --- a/lib/puppet/parser/functions/load_module_metadata.rb +++ b/lib/puppet/parser/functions/load_module_metadata.rb @@ -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)) diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 6ec9fae1d..a85cd18ee 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -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] diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index 9bf047a0a..bb2a7b00a 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -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!") diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb index f2d857f14..6a8124a0f 100644 --- a/spec/functions/load_module_metadata_spec.rb +++ b/spec/functions/load_module_metadata_spec.rb @@ -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') @@ -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 diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index b9f0df95a..17d01e451 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -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 @@ -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 @@ -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!' diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb index cc08c477a..99e87d8a9 100644 --- a/spec/functions/loadyaml_spec.rb +++ b/spec/functions/loadyaml_spec.rb @@ -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 @@ -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 @@ -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