Skip to content

Commit 668b3c3

Browse files
committed
Merge pull request #369 from rfugina/dirname_typecheck
Dirname typecheck
2 parents 41965fd + 2a3babc commit 668b3c3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/puppet/parser/functions/dirname.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ module Puppet::Parser::Functions
44
EOS
55
) do |arguments|
66

7-
raise(Puppet::ParseError, "dirname(): Wrong number of arguments " +
8-
"given (#{arguments.size} for 1)") if arguments.size < 1
7+
if arguments.size < 1 then
8+
raise(Puppet::ParseError, "dirname(): No arguments given")
9+
end
10+
if arguments.size > 1 then
11+
raise(Puppet::ParseError, "dirname(): Too many arguments given (#{arguments.size})")
12+
end
13+
unless arguments[0].is_a?(String)
14+
raise(Puppet::ParseError, 'dirname(): Requires string as argument')
15+
end
916

10-
path = arguments[0]
11-
return File.dirname(path)
17+
return File.dirname(arguments[0])
1218
end
1319
end
1420

spec/functions/dirname_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
expect { scope.function_dirname([]) }.to( raise_error(Puppet::ParseError))
1313
end
1414

15+
it "should raise a ParseError if there is more than 1 argument" do
16+
expect { scope.function_dirname(['a', 'b']) }.to( raise_error(Puppet::ParseError))
17+
end
18+
1519
it "should return dirname for an absolute path" do
1620
result = scope.function_dirname(['/path/to/a/file.ext'])
1721
expect(result).to(eq('/path/to/a'))
@@ -21,4 +25,14 @@
2125
result = scope.function_dirname(['path/to/a/file.ext'])
2226
expect(result).to(eq('path/to/a'))
2327
end
28+
29+
it "should complain about hash argument" do
30+
expect { scope.function_dirname([{}]) }.to( raise_error(Puppet::ParseError))
31+
end
32+
it "should complain about list argument" do
33+
expect { scope.function_dirname([[]]) }.to( raise_error(Puppet::ParseError))
34+
end
35+
it "should complain about numeric argument" do
36+
expect { scope.function_dirname([2112]) }.to( raise_error(Puppet::ParseError))
37+
end
2438
end

0 commit comments

Comments
 (0)