diff options
author | Ashley Penney <ashley.penney@puppetlabs.com> | 2014-05-15 17:31:28 -0400 |
---|---|---|
committer | Ashley Penney <ashley.penney@puppetlabs.com> | 2014-05-15 17:31:28 -0400 |
commit | 645de3cccb8050c13ef35cdc855e831f65765e0a (patch) | |
tree | 239d09f3f93f35d3fc79f28499e19b65756ab982 | |
parent | 430d821ad3bc6828b9c0bc1ddf5967fdd4b4d66b (diff) | |
parent | c5b06f9bbca7acc491560c92a73d7e2a153fe0a7 (diff) |
Merge pull request #257 from apenney/revert-before
Revert "Merge pull request #256 from stbenjam/2571-before"
-rw-r--r-- | lib/puppet/provider/file_line/ruby.rb | 16 | ||||
-rw-r--r-- | lib/puppet/type/file_line.rb | 4 | ||||
-rwxr-xr-x | spec/unit/puppet/provider/file_line/ruby_spec.rb | 59 | ||||
-rwxr-xr-x | spec/unit/puppet/type/file_line_spec.rb | 8 |
4 files changed, 7 insertions, 80 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 2cbd172..94e7fac 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -9,9 +9,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do if resource[:match] handle_create_with_match elsif resource[:after] - handle_create_with_position :after - elsif resource[:before] - handle_create_with_position :before + handle_create_with_after else append_line end @@ -51,29 +49,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do end end - def handle_create_with_position(position) - regex = resource[position] ? Regexp.new(resource[position]) : nil + def handle_create_with_after + regex = Regexp.new(resource[:after]) count = lines.count {|l| l.match(regex)} case count - when 1 # find the line to put our line before/after + when 1 # find the line to put our line after File.open(resource[:path], 'w') do |fh| lines.each do |l| - fh.puts(l) if position == :after + fh.puts(l) if regex.match(l) then fh.puts(resource[:line]) end - fh.puts(l) if position == :before end end when 0 # append the line to the end of the file append_line else - raise Puppet::Error, "#{count} lines match pattern '#{resource[position]}' in file '#{resource[:path]}'. One or no line must match the pattern." + raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." end end + ## # append the line to the file. # # @api private diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index bc6745f..323fc4c 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -46,10 +46,6 @@ Puppet::Type.newtype(:file_line) do desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' end - newparam(:before) do - desc 'An optional value used to specify the line before which we will add any new lines. (Existing lines are added in place)' - end - newparam(:line) do desc 'The line to be appended to the file located by the path parameter.' end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index d004af4..a016b68 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -183,65 +183,6 @@ describe provider_class do end end end - - describe 'using before' do - let :resource do - Puppet::Type::File_line.new( - { - :name => 'foo', - :path => @tmpfile, - :line => 'inserted = line', - :before => '^foo1', - } - ) - end - - let :provider do - provider_class.new(resource) - end - - context 'with one line matching the before expression' do - before :each do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") - end - end - - it 'inserts the specified line before the line matching the "before" expression' do - provider.create - File.read(@tmpfile).chomp.should eql("inserted = line\nfoo1\nfoo = blah\nfoo2\nfoo = baz") - end - end - - context 'with two lines matching the before expression' do - before :each do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") - end - end - - it 'errors out stating "One or no line must match the pattern"' do - expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/) - end - end - - context 'with no lines matching the after expression' do - let :content do - "foo3\nfoo = blah\nfoo2\nfoo = baz\n" - end - - before :each do - File.open(@tmpfile, 'w') do |fh| - fh.write(content) - end - end - - it 'appends the specified line to the file' do - provider.create - File.read(@tmpfile).should eq(content << resource[:line] << "\n") - end - end - end end context "when removing" do diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb index b85b9f4..ab5b81b 100755 --- a/spec/unit/puppet/type/file_line_spec.rb +++ b/spec/unit/puppet/type/file_line_spec.rb @@ -15,14 +15,6 @@ describe Puppet::Type.type(:file_line) do file_line[:match] = '^foo.*$' file_line[:match].should == '^foo.*$' end - it 'should accept an after regex' do - file_line[:after] = '^foo.*$' - file_line[:after].should == '^foo.*$' - end - it 'should accept a before regex' do - file_line[:before] = '^foo.*$' - file_line[:before].should == '^foo.*$' - end it 'should not accept a match regex that does not match the specified line' do expect { Puppet::Type.type(:file_line).new( |