Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/puppet/parser/functions/dirname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions spec/functions/dirname_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
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'))
Expand All @@ -21,4 +25,14 @@
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