]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Added type checks for dirname(), and additional tests
authorRob Fugina <rfugina@genome.wustl.edu>
Tue, 18 Nov 2014 18:34:55 +0000 (12:34 -0600)
committerRob Fugina <rfugina@genome.wustl.edu>
Wed, 17 Dec 2014 18:55:48 +0000 (12:55 -0600)
lib/puppet/parser/functions/dirname.rb
spec/functions/dirname_spec.rb

index ea8cc1e081eb161a4ce54c7143a4b16979843ad3..40b300d89d3e00f06f7f01e8cf213581c58a3c9d 100644 (file)
@@ -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
 
index 8a3bcabfc4b57cec0e45e0937acb5cf5f4b31b0f..4261144ec7ee549159c3f19eb6d515e545fbc766 100755 (executable)
@@ -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