Merge branch 'pull-163'

This closes GH-163
This commit is contained in:
Adrien Thebo 2013-07-11 16:26:45 -07:00
commit e0d4588bd2
3 changed files with 40 additions and 3 deletions

View file

@ -1,4 +1,3 @@
Puppet::Type.type(:file_line).provide(:ruby) do
def exists?
@ -35,7 +34,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do
def handle_create_with_match()
regex = resource[:match] ? Regexp.new(resource[:match]) : nil
match_count = lines.select { |l| regex.match(l) }.size
if match_count > 1
if match_count > 1 && resource[:multiple].to_s != 'true'
raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
end
File.open(resource[:path], 'w') do |fh|

View file

@ -37,6 +37,11 @@ Puppet::Type.newtype(:file_line) do
'if a match is found, we replace that line rather than adding a new line.'
end
newparam(:multiple) do
desc 'An optional value to determine if match can change multiple lines.'
newvalues(true, false)
end
newparam(:line) do
desc 'The line to be appended to the file located by the path parameter.'
end

View file

@ -61,6 +61,39 @@ describe provider_class do
File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
it 'should replace all lines that matches' do
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => true
}
)
@provider = provider_class.new(@resource)
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
@provider.exists?.should be_nil
@provider.create
File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
end
it 'should raise an error with invalid values' do
expect {
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => 'asgadga'
}
)
}.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
end
it 'should replace a line that matches' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2")