Skip to content

Commit

Permalink
Merge pull request #314 from amateo/feature/loadyaml_check_file
Browse files Browse the repository at this point in the history
Check if file exists before loading with loadyaml. If not, return nil
  • Loading branch information
Morgan Haskel committed Apr 16, 2015
2 parents 59b3fb4 + ac24e7a commit 8a1d1e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/puppet/parser/functions/loadyaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ module Puppet::Parser::Functions
raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)")
end

YAML.load_file(args[0])
if File.exists?(args[0]) then
YAML.load_file(args[0])
else
warning("Can't load " + args[0] + ". File does not exist!")
nil
end

end

Expand Down
6 changes: 6 additions & 0 deletions spec/functions/loadyaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
expect { scope.function_loadyaml([]) }.to raise_error(Puppet::ParseError)
end

it "should return nil when file does not exist" do
yaml_file = tmpfilename ('yamlfile')
result = scope.function_loadyaml([yaml_file])
expect(result).to(eq(nil))
end

it "should convert YAML file to a data structure" do
yaml_file = tmpfilename ('yamlfile')
File.open(yaml_file, 'w') do |fh|
Expand Down

0 comments on commit 8a1d1e2

Please sign in to comment.