Skip to content

Commit 57a8485

Browse files
committed
Merge pull request #537 from cmurphy/fix_load_module_metadata
Fix load module metadata
2 parents 5b3c623 + 25410c4 commit 57a8485

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

README.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@ Loads the metadata.json of a target module. Can be used to determine module vers
539539
notify { $metadata['author']: }
540540
~~~
541541

542+
If you do not want to fail the catalog compilation if the metadata file for a module is not present:
543+
544+
~~~
545+
$metadata = load_module_metadata('mysql', true)
546+
if empty($metadata) {
547+
notify { "This module does not have a metadata.json file.": }
548+
}
549+
~~~
550+
542551
*Type*: rvalue.
543552

544553
#### `lstrip`

lib/puppet/parser/functions/load_module_metadata.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ module Puppet::Parser::Functions
22
newfunction(:load_module_metadata, :type => :rvalue, :doc => <<-EOT
33
EOT
44
) do |args|
5-
raise(Puppet::ParseError, "load_module_metadata(): Wrong number of arguments, expects one") unless args.size == 1
5+
raise(Puppet::ParseError, "load_module_metadata(): Wrong number of arguments, expects one or two") unless [1,2].include?(args.size)
66
mod = args[0]
7+
allow_empty_metadata = args[1]
78
module_path = function_get_module_path([mod])
89
metadata_json = File.join(module_path, 'metadata.json')
910

10-
raise(Puppet::ParseError, "load_module_metadata(): No metadata.json file for module #{mod}") unless File.exists?(metadata_json)
11-
12-
metadata = PSON.load(File.read(metadata_json))
11+
metadata_exists = File.exists?(metadata_json)
12+
if metadata_exists
13+
metadata = PSON.load(File.read(metadata_json))
14+
else
15+
if allow_empty_metadata
16+
metadata = {}
17+
else
18+
raise(Puppet::ParseError, "load_module_metadata(): No metadata.json file for module #{mod}")
19+
end
20+
end
1321

1422
return metadata
1523
end

spec/functions/load_module_metadata.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe 'load_module_metadata' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
6+
it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
8+
it "should json parse the file" do
9+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
10+
allow(File).to receive(:exists?).with(/metadata.json/).and_return(true)
11+
allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}')
12+
13+
result = subject.call(['science'])
14+
expect(result['name']).to eq('spencer-science')
15+
end
16+
17+
it "should fail by default if there is no metadata.json" do
18+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
19+
allow(File).to receive(:exists?).with(/metadata.json/).and_return(false)
20+
expect {subject.call(['science'])}.to raise_error(Puppet::ParseError)
21+
end
22+
23+
it "should return nil if user allows empty metadata.json" do
24+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
25+
allow(File).to receive(:exists?).with(/metadata.json/).and_return(false)
26+
result = subject.call(['science', true])
27+
expect(result).to eq({})
28+
end
29+
end

0 commit comments

Comments
 (0)