From 0f8df10084c875edac521a2307ce4caf7317b636 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Wed, 14 Oct 2015 15:59:09 -0700 Subject: Rename load_module_metadata test path `rake spec` only finds test files that end in _spec.rb, so this test was not being run. Correct the path name so that the test runs properly. --- spec/functions/load_module_metadata_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 spec/functions/load_module_metadata_spec.rb (limited to 'spec/functions/load_module_metadata_spec.rb') diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb new file mode 100755 index 0000000..ba542eb --- /dev/null +++ b/spec/functions/load_module_metadata_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'load_module_metadata' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } + it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } + + it "should json parse the file" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with(/metadata.json/).and_return(true) + allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}') + + result = subject.call(['science']) + expect(result['name']).to eq('spencer-science') + end +end -- cgit v1.2.3 From 25410c4598d3c0029fcd05adc2e305dbf5f8d902 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Wed, 14 Oct 2015 16:09:05 -0700 Subject: Let load_module_metadata succeed on empty file Some modules or module versions don't have a metadata.json file, but we might still want to use the load_module_metadata function on them. The lack of a file can still give us important information. For example, it might tell us that the version of the module installed is "very old" even if we can't read the version number directly. This patch adds a parameter to let the user specify if an empty file is acceptable. To preserve backwards compatibility it does not change the current default behavior, which is to raise an error if metadata.json does not exist. --- spec/functions/load_module_metadata_spec.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'spec/functions/load_module_metadata_spec.rb') diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb index ba542eb..fe665fb 100755 --- a/spec/functions/load_module_metadata_spec.rb +++ b/spec/functions/load_module_metadata_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'load_module_metadata' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } - it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } + it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it "should json parse the file" do allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') @@ -13,4 +13,17 @@ describe 'load_module_metadata' do result = subject.call(['science']) expect(result['name']).to eq('spencer-science') end + + it "should fail by default if there is no metadata.json" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with(/metadata.json/).and_return(false) + expect {subject.call(['science'])}.to raise_error(Puppet::ParseError) + end + + it "should return nil if user allows empty metadata.json" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with(/metadata.json/).and_return(false) + result = subject.call(['science', true]) + expect(result).to eq({}) + end end -- cgit v1.2.3 From 5c51463c1f9e89bfd012c737c19581ac0f771926 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 30 Jun 2016 17:52:45 +0100 Subject: Fix load_module_metadata and loadjson tests to pass with fully deployed module When replacing the lib/ and manifests/ symlinks in the fixtures with a proper top-level symlink, puppet 4 starts loading the metadata.json before loading functions, which confuses these tests. Added more specific expectations, and provide data for that call. --- spec/functions/load_module_metadata_spec.rb | 43 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'spec/functions/load_module_metadata_spec.rb') diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb index fe665fb..1a61e2c 100755 --- a/spec/functions/load_module_metadata_spec.rb +++ b/spec/functions/load_module_metadata_spec.rb @@ -5,25 +5,34 @@ describe 'load_module_metadata' do it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } - it "should json parse the file" do - allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') - allow(File).to receive(:exists?).with(/metadata.json/).and_return(true) - allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}') + describe "when calling with valid arguments" do + before :each do + if RSpec.configuration.puppet_future + allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}') + else + allow(File).to receive(:read).with(/\/stdlib\/metadata.json/).and_return('{"name": "puppetlabs-stdlib"}') + end + end + it "should json parse the file" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(true) + allow(File).to receive(:read).with('/path/to/module/metadata.json').and_return('{"name": "spencer-science"}') - result = subject.call(['science']) - expect(result['name']).to eq('spencer-science') - end + result = subject.call(['science']) + expect(result['name']).to eq('spencer-science') + end - it "should fail by default if there is no metadata.json" do - allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') - allow(File).to receive(:exists?).with(/metadata.json/).and_return(false) - expect {subject.call(['science'])}.to raise_error(Puppet::ParseError) - end + it "should fail by default if there is no metadata.json" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false) + expect {subject.call(['science'])}.to raise_error(Puppet::ParseError) + end - it "should return nil if user allows empty metadata.json" do - allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') - allow(File).to receive(:exists?).with(/metadata.json/).and_return(false) - result = subject.call(['science', true]) - expect(result).to eq({}) + it "should return nil if user allows empty metadata.json" do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/') + allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false) + result = subject.call(['science', true]) + expect(result).to eq({}) + end end end -- cgit v1.2.3