Commit 17ada5f9 authored by Raphaël Pinson's avatar Raphaël Pinson
Browse files

Add whichquote method

parent 18798ce8
......@@ -439,13 +439,13 @@ Puppet::Type.type(:augeasprovider).provide(:default) do
label || path.split("/")[-1].split("[")[0]
end
# Automatically quote a value
# Determine which quote is needed
#
# @param [String] value the value to quote
# @param [String] oldvalue the optional old value, used to auto-detect existing quoting
# @return [String] the quoted value
# @api public
def self.quoteit(value, resource = nil, oldvalue = nil)
def self.whichquote(value, resource = nil, oldvalue = nil)
oldquote = readquote oldvalue
if resource and resource.parameters.include? :quoted
......@@ -463,16 +463,27 @@ Puppet::Type.type(:augeasprovider).provide(:default) do
:none
end
end
case quote
when :double
"\"#{value}\""
'"'
when :single
"'#{value}'"
"'"
else
value
''
end
end
# Automatically quote a value
#
# @param [String] value the value to quote
# @param [String] oldvalue the optional old value, used to auto-detect existing quoting
# @return [String] the quoted value
# @api public
def self.quoteit(value, resource = nil, oldvalue = nil)
quote = whichquote(value, resource, oldvalue)
"#{quote}#{value}#{quote}"
end
# Detect what type of quoting a value uses
#
......@@ -815,6 +826,16 @@ Puppet::Type.type(:augeasprovider).provide(:default) do
self.class.path_label(aug, path)
end
# Determine which quote is needed
#
# @param [String] value the value to quote
# @param [String] oldvalue the optional old value, used to auto-detect existing quoting
# @return [String] the quoted value
# @api public
def whichquote(value, oldvalue = nil)
self.class.whichquote(value, self.resource, oldvalue)
end
# Automatically quote a value
#
# @param [String] value the value to quote
......
......@@ -69,6 +69,49 @@ describe provider_class do
end
end
describe "#whichquote" do
it "return an empty string for alphanum values" do
subject.whichquote('foo').should == ''
end
it "should double-quote by default for values containing spaces or special characters" do
subject.whichquote('foo bar').should == '"'
subject.whichquote('foo&bar').should == '"'
subject.whichquote('foo;bar').should == '"'
subject.whichquote('foo<bar').should == '"'
subject.whichquote('foo>bar').should == '"'
subject.whichquote('foo(bar').should == '"'
subject.whichquote('foo)bar').should == '"'
subject.whichquote('foo|bar').should == '"'
end
it "should call #readquote and use its value when oldvalue is passed" do
subject.whichquote('foo', nil, "'bar'").should == "'"
subject.whichquote('foo', nil, '"bar"').should == '"'
subject.whichquote('foo', nil, 'bar').should == ''
subject.whichquote('foo bar', nil, "'bar'").should == "'"
end
it "should double-quote special values when oldvalue is not quoted" do
subject.whichquote('foo bar', nil, 'bar').should == '"'
end
it "should use the :quoted parameter when present" do
resource = { }
resource.stubs(:parameters).returns([:quoted])
resource[:quoted] = :single
subject.whichquote('foo', resource).should == "'"
resource[:quoted] = :double
subject.whichquote('foo', resource).should == '"'
resource[:quoted] = :auto
subject.whichquote('foo', resource).should == ''
subject.whichquote('foo bar', resource).should == '"'
end
end
describe "#quoteit" do
it "should not do anything by default for alphanum values" do
subject.quoteit('foo').should == 'foo'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment