Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9696865
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
View Options
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
@@ -1,15 +1,21 @@
module Puppet::Parser::Functions
newfunction(:dirname, :type => :rvalue, :doc => <<-EOS
Returns the dirname of a path.
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
# vim: set ts=2 sw=2 et :
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
@@ -1,24 +1,38 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
describe "the dirname function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
it "should exist" do
expect(Puppet::Parser::Functions.function("dirname")).to eq("function_dirname")
end
it "should raise a ParseError if there is less than 1 arguments" 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'))
end
it "should return dirname for a relative path" 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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Aug 18, 9:54 PM (1 w, 22 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3297311
Attached To
rSPSTD puppet-puppetlabs-stdlib
Event Timeline
Log In to Comment