summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/provider/file_line/ruby.rb14
-rwxr-xr-xspec/functions/validate_augeas_spec.rb4
-rwxr-xr-xspec/unit/puppet/provider/file_line/ruby_spec.rb88
3 files changed, 85 insertions, 21 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index ae1a8b3..e7854f0 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -34,13 +34,22 @@ Puppet::Type.type(:file_line).provide(:ruby) do
def handle_create_with_match()
regex = resource[:match] ? Regexp.new(resource[:match]) : nil
+ regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil
match_count = count_matches(regex)
+
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|
lines.each do |l|
fh.puts(regex.match(l) ? resource[:line] : l)
+ if (match_count == 0 and regex_after)
+ if regex_after.match(l)
+ fh.puts(resource[:line])
+ match_count += 1 #Increment match_count to indicate that the new line has been inserted.
+ end
+ end
end
if (match_count == 0)
@@ -78,7 +87,10 @@ Puppet::Type.type(:file_line).provide(:ruby) do
#
# @api private
def append_line
- File.open(resource[:path], 'a') do |fh|
+ File.open(resource[:path], 'w') do |fh|
+ lines.each do |l|
+ fh.puts(l)
+ end
fh.puts resource[:line]
end
end
diff --git a/spec/functions/validate_augeas_spec.rb b/spec/functions/validate_augeas_spec.rb
index c695ba2..99523ab 100755
--- a/spec/functions/validate_augeas_spec.rb
+++ b/spec/functions/validate_augeas_spec.rb
@@ -60,7 +60,7 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
end
describe "Nicer Error Messages" do
- # The intent here is to make sure the function returns the 3rd argument
+ # The intent here is to make sure the function returns the 4th argument
# in the exception thrown
inputs = [
[ "root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content' ],
@@ -69,7 +69,7 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
inputs.each do |input|
it "validate_augeas(#{input.inspect}) should fail" do
- expect { subject.call input }.to raise_error /#{input[2]}/
+ expect { subject.call input }.to raise_error /#{input[3]}/
end
end
end
diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb
index d2a129c..a84fc78 100755
--- a/spec/unit/puppet/provider/file_line/ruby_spec.rb
+++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb
@@ -46,12 +46,12 @@ describe provider_class do
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
- {
- :name => 'foo',
- :path => @tmpfile,
- :line => 'foo = bar',
- :match => '^foo\s*=.*$',
- }
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ }
)
@provider = provider_class.new(@resource)
end
@@ -69,11 +69,11 @@ describe provider_class do
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
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ :multiple => true,
}
)
@provider = provider_class.new(@resource)
@@ -89,11 +89,11 @@ describe provider_class do
expect {
@resource = Puppet::Type::File_line.new(
{
- :name => 'foo',
- :path => @tmpfile,
- :line => 'foo = bar',
- :match => '^foo\s*=.*$',
- :multiple => 'asgadga'
+ :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\./)
@@ -140,7 +140,54 @@ describe provider_class do
let :provider do
provider_class.new(resource)
end
-
+ context 'match and after set' do
+ shared_context 'resource_create' do
+ let(:match) { '^foo2$' }
+ let(:after) { '^foo1$' }
+ let(:resource) {
+ Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'inserted = line',
+ :after => after,
+ :match => match,
+ }
+ )
+ }
+ end
+ before :each do
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo2\nfoo = baz")
+ end
+ end
+ describe 'inserts at match' do
+ include_context 'resource_create'
+ it {
+ provider.create
+ expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
+ }
+ end
+ describe 'inserts a new line after when no match' do
+ include_context 'resource_create' do
+ let(:match) { '^nevergoingtomatch$' }
+ end
+ it {
+ provider.create
+ expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
+ }
+ end
+ describe 'append to end of file if no match for both after and match' do
+ include_context 'resource_create' do
+ let(:match) { '^nevergoingtomatch$' }
+ let(:after) { '^stillneverafter' }
+ end
+ it {
+ provider.create
+ expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
+ }
+ end
+ end
context 'with one line matching the after expression' do
before :each do
File.open(@tmpfile, 'w') do |fh|
@@ -194,7 +241,12 @@ describe provider_class do
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
- {:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' }
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo',
+ :ensure => 'absent',
+ }
)
@provider = provider_class.new(@resource)
end