From b9560df899fdea34ac69692ef2447ffdd2d3365a Mon Sep 17 00:00:00 2001 From: "Angel L. Mateo" Date: Tue, 2 Sep 2014 11:35:42 +0200 Subject: Check if file exists before loading with loadyaml. If not, return nil --- lib/puppet/parser/functions/loadyaml.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index 10c4005..ca655f6 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -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 -- cgit v1.2.3 From ac24e7acc767bce9cc1977f90c798df94292813e Mon Sep 17 00:00:00 2001 From: "Angel L. Mateo" Date: Mon, 13 Apr 2015 09:27:03 +0200 Subject: test case for when the file does not exist --- spec/functions/loadyaml_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb index cdc3d6f..4b8d406 100755 --- a/spec/functions/loadyaml_spec.rb +++ b/spec/functions/loadyaml_spec.rb @@ -14,6 +14,12 @@ describe "the loadyaml function" do 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| -- cgit v1.2.3