diff options
author | tphoney <tp@puppet.com> | 2017-07-03 17:08:09 +0100 |
---|---|---|
committer | tphoney <tp@puppet.com> | 2017-07-03 17:08:09 +0100 |
commit | 58131d8ff1886352e964aafef58ffeb65c5d2cdd (patch) | |
tree | 58fca90257dd2606c3fc006574134080b8929440 | |
parent | 517a133209a0b0f8741029c247fea595b50d801e (diff) |
(MODULES-5003) file_line fix all broken lines
-rw-r--r-- | lib/puppet/provider/file_line/ruby.rb | 11 | ||||
-rwxr-xr-x | spec/unit/puppet/provider/file_line/ruby_spec.rb | 37 |
2 files changed, 36 insertions, 12 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 42f287a..1d27424 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,16 +1,21 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? + found = true if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - true + found = true else lines.find do |line| if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - line.chomp =~ Regexp.new(resource[:match]) + found = line.chomp =~ Regexp.new(resource[:match]) else - line.chomp == resource[:line].chomp + found = line.chomp == resource[:line].chomp + end + if found == false then + break end end end + found end def create diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 1f41f62..0e12aa2 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -29,7 +29,7 @@ describe provider_class do File.open(tmpfile, 'w') do |fh| fh.write('foo1') end - expect(provider.exists?).to be_nil + expect(provider.exists?).to eql (false) end it 'should append to an existing file when creating' do provider.create @@ -69,7 +69,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar") end @@ -112,7 +112,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/) expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end @@ -131,11 +131,30 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") end + it 'should replace all lines that match, even when some lines are correct' do + @resource = Puppet::Type::File_line.new( + { + :name => 'neil', + :path => @tmpfile, + :line => "\thard\tcore\t0\n", + :match => '^[ \t]hard[ \t]+core[ \t]+.*', + :multiple => true, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") + end + expect(@provider.exists?).to eql(false) + @provider.create + expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0") + end + it 'should raise an error with invalid values' do expect { @resource = Puppet::Type::File_line.new( @@ -154,7 +173,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -162,7 +181,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") end @@ -170,7 +189,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo = bar\nfoo2") end - expect(@provider.exists?).to be_truthy + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -274,7 +293,7 @@ describe provider_class do } ) @provider = provider_class.new(@resource) - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz") end @@ -367,7 +386,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") end - expect(@provider.exists?).to be_truthy + expect(@provider.exists?).to be_nil end it 'should remove one line if it matches' do |