diff options
author | Hunter Haugen <hunter@puppetlabs.com> | 2015-03-05 11:00:36 -0800 |
---|---|---|
committer | Hunter Haugen <hunter@puppetlabs.com> | 2015-03-05 11:00:36 -0800 |
commit | 668b3c31b591b9ee67ab215715b95adf8e78608a (patch) | |
tree | 92d88af6d4afce56552b4a20589d7214c8dc304f | |
parent | 41965fd2c91ef4e5f87206a763456919644c8bbd (diff) | |
parent | 2a3babc348895a4c8990d57f003c41e50f3ed932 (diff) |
Merge pull request #369 from rfugina/dirname_typecheck
Dirname typecheck
-rw-r--r-- | lib/puppet/parser/functions/dirname.rb | 14 | ||||
-rwxr-xr-x | spec/functions/dirname_spec.rb | 14 |
2 files changed, 24 insertions, 4 deletions
diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb index ea8cc1e..40b300d 100644 --- a/lib/puppet/parser/functions/dirname.rb +++ b/lib/puppet/parser/functions/dirname.rb @@ -4,11 +4,17 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "dirname(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + if arguments.size < 1 then + raise(Puppet::ParseError, "dirname(): No arguments given") + end + if arguments.size > 1 then + raise(Puppet::ParseError, "dirname(): Too many arguments given (#{arguments.size})") + end + unless arguments[0].is_a?(String) + raise(Puppet::ParseError, 'dirname(): Requires string as argument') + end - path = arguments[0] - return File.dirname(path) + return File.dirname(arguments[0]) end end diff --git a/spec/functions/dirname_spec.rb b/spec/functions/dirname_spec.rb index 8a3bcab..4261144 100755 --- a/spec/functions/dirname_spec.rb +++ b/spec/functions/dirname_spec.rb @@ -12,6 +12,10 @@ describe "the dirname function" do expect { scope.function_dirname([]) }.to( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if there is more than 1 argument" do + expect { scope.function_dirname(['a', 'b']) }.to( raise_error(Puppet::ParseError)) + end + it "should return dirname for an absolute path" do result = scope.function_dirname(['/path/to/a/file.ext']) expect(result).to(eq('/path/to/a')) @@ -21,4 +25,14 @@ describe "the dirname function" do result = scope.function_dirname(['path/to/a/file.ext']) expect(result).to(eq('path/to/a')) end + + it "should complain about hash argument" do + expect { scope.function_dirname([{}]) }.to( raise_error(Puppet::ParseError)) + end + it "should complain about list argument" do + expect { scope.function_dirname([[]]) }.to( raise_error(Puppet::ParseError)) + end + it "should complain about numeric argument" do + expect { scope.function_dirname([2112]) }.to( raise_error(Puppet::ParseError)) + end end |