From 221277e8522b42bf170fded6ea23dfc526703b07 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Thu, 29 Aug 2013 12:19:16 -0400 Subject: Update file_line resource to support 'after'. When adding new lines to a file the 'after' option can be useful when you need to insert file lines into the middle of a file. This is particularly helpful when using file_line with sectioned config files. NOTE: the after option only works when adding new lines. If you are updating an existing (matched) line it will simply modify it in place. This assumes it was in the right place to begin with. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 648c05b..c8575ab 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -80,6 +80,24 @@ describe provider_class do File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar") end + it 'should replace all lines that matches with after' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'inserted = line', + :after => '^foo1', + } + ) + @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\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") + end + it 'should raise an error with invalid values' do expect { @resource = Puppet::Type::File_line.new( -- cgit v1.2.3 From 948be0bb99374c699d79eb11c00366a6e2d2f977 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Thu, 29 Aug 2013 16:27:27 -0700 Subject: (maint) Improve the tests and readability of file_line Without this patch the implementation of the file_line provider is a bit convoluted with respect to the newly introduced "after" parameter. This patch addresses the problem by separating out the concerns of each case into their own methods of handling the behavior with the match parameter, handling the behavior with the after parameter, or simply appending the line. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 208 ++++++++++++++--------- 1 file changed, 127 insertions(+), 81 deletions(-) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index c8575ab..65b5d20 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -3,33 +3,36 @@ require 'tempfile' provider_class = Puppet::Type.type(:file_line).provider(:ruby) describe provider_class do context "when adding" do - before :each do - # TODO: these should be ported over to use the PuppetLabs spec_helper - # file fixtures once the following pull request has been merged: - # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files + let :tmpfile do tmp = Tempfile.new('tmp') - @tmpfile = tmp.path + path = tmp.path tmp.close! - @resource = Puppet::Type::File_line.new( - {:name => 'foo', :path => @tmpfile, :line => 'foo'} + path + end + let :resource do + Puppet::Type::File_line.new( + {:name => 'foo', :path => tmpfile, :line => 'foo'} ) - @provider = provider_class.new(@resource) end + let :provider do + provider_class.new(resource) + end + it 'should detect if the line exists in the file' do - File.open(@tmpfile, 'w') do |fh| + File.open(tmpfile, 'w') do |fh| fh.write('foo') end - @provider.exists?.should be_true + provider.exists?.should be_true end it 'should detect if the line does not exist in the file' do - File.open(@tmpfile, 'w') do |fh| + File.open(tmpfile, 'w') do |fh| fh.write('foo1') end - @provider.exists?.should be_nil + provider.exists?.should be_nil end it 'should append to an existing file when creating' do - @provider.create - File.read(@tmpfile).chomp.should == 'foo' + provider.create + File.read(tmpfile).chomp.should == 'foo' end end @@ -52,89 +55,132 @@ describe provider_class do @provider = provider_class.new(@resource) end - it 'should raise an error if more than one line matches, and should not have modified the file' do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") + describe 'using match' do + it 'should raise an error if more than one line matches, and should not have modified the file' do + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") + end + @provider.exists?.should be_nil + expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/) + File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end - @provider.exists?.should be_nil - expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/) - 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( + 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) - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") + ) + @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 - @provider.exists?.should be_nil - @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar") - end - it 'should replace all lines that matches with after' do - @resource = Puppet::Type::File_line.new( - { - :name => 'foo', - :path => @tmpfile, - :line => 'inserted = line', - :after => '^foo1', - } - ) - @provider = provider_class.new(@resource) - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") + 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") + end + @provider.exists?.should be_nil + @provider.create + File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") + end + it 'should add a new line if no lines match' do + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo2") + end + @provider.exists?.should be_nil + @provider.create + File.read(@tmpfile).should eql("foo1\nfoo2\nfoo = bar\n") + end + it 'should do nothing if the exact line already exists' do + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo = bar\nfoo2") + end + @provider.exists?.should be_true + @provider.create + File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") end - @provider.exists?.should be_nil - @provider.create - File.read(@tmpfile).chomp.should eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") end - it 'should raise an error with invalid values' do - expect { - @resource = Puppet::Type::File_line.new( + describe 'using after' do + let :resource do + Puppet::Type::File_line.new( { - :name => 'foo', - :path => @tmpfile, - :line => 'foo = bar', - :match => '^foo\s*=.*$', - :multiple => 'asgadga' + :name => 'foo', + :path => @tmpfile, + :line => 'inserted = line', + :after => '^foo1', } ) - }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./) - end + end - it 'should replace a line that matches' do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo=blah\nfoo2") + let :provider do + provider_class.new(resource) end - @provider.exists?.should be_nil - @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") - end - it 'should add a new line if no lines match' do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo2") + + context 'with one line matching the after 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 after the line matching the "after" expression' do + provider.create + File.read(@tmpfile).chomp.should eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") + end end - @provider.exists?.should be_nil - @provider.create - File.read(@tmpfile).should eql("foo1\nfoo2\nfoo = bar\n") - end - it 'should do nothing if the exact line already exists' do - File.open(@tmpfile, 'w') do |fh| - fh.write("foo1\nfoo = bar\nfoo2") + + context 'with two lines matching the after 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 - @provider.exists?.should be_true - @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") end end -- cgit v1.2.3 From 3854e076ccb75d1bcb1ddd29f5976b194d857765 Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Wed, 5 Mar 2014 15:43:58 -0500 Subject: Numerous changes to update testing gems. This work updates a number of Gems to the latest versions (rspec, rspec-puppet), and updates and tweaks a bunch of tests to work with the updated gems. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 65b5d20..c356bd2 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -1,4 +1,4 @@ -require 'puppet' +require 'spec_helper' require 'tempfile' provider_class = Puppet::Type.type(:file_line).provider(:ruby) describe provider_class do -- cgit v1.2.3 From 09f892023c726eabaa85a308c2dbffd8e8b71fbd Mon Sep 17 00:00:00 2001 From: Andrea Veri Date: Wed, 7 May 2014 11:49:25 +0200 Subject: Add the missing shebangs and fix the wrong ones for rpmlint to stop complaining loudly --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index c356bd2..a016b68 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -1,3 +1,4 @@ +#! /usr/bin/env ruby -S rspec require 'spec_helper' require 'tempfile' provider_class = Puppet::Type.type(:file_line).provider(:ruby) -- cgit v1.2.3 From c66a2e4f49d6c9ebcbff718f3ec119049fb4c514 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 7 May 2014 10:09:32 -0700 Subject: Add mode +x to spec .rb files --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 spec/unit/puppet/provider/file_line/ruby_spec.rb (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb old mode 100644 new mode 100755 -- cgit v1.2.3 From 6eaa592cd8d5af097a4624b3d1cead74408aabf2 Mon Sep 17 00:00:00 2001 From: Stephen Benjamin Date: Wed, 14 May 2014 20:33:57 +0200 Subject: (PUP-2571) add 'before' functionality to file_line file_line supports adding lines after a match, but there are use cases when having "before" would be useful. For example, in Debian-based OS's, the last line of /etc/rc.local is "exit 0" it's an incredible pain to deal with that scenario today. This commit adds a 'before' parameter to the file_line type, and implements it for the ruby provider. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index a016b68..d004af4 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -183,6 +183,65 @@ 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 -- cgit v1.2.3 From c5b06f9bbca7acc491560c92a73d7e2a153fe0a7 Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Thu, 15 May 2014 17:28:59 -0400 Subject: Revert "Merge pull request #256 from stbenjam/2571-before" This reverts commit 8499ebdb7f892f2623295058649c67a5553d4732, reversing changes made to 08b00d9229961d7b3c3cba997bfb35c8d47e4c4b. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 59 ------------------------ 1 file changed, 59 deletions(-) (limited to 'spec/unit/puppet/provider') 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 -- cgit v1.2.3 From d65d2354a7458c3281386e7065bd1938d2c2adee Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Wed, 4 Jun 2014 14:37:45 -0400 Subject: Convert specs to RSpec 2.99.0 syntax with Transpec This conversion is done by Transpec 2.2.1 with the following command: transpec spec/unit * 53 conversions from: obj.should to: expect(obj).to * 19 conversions from: == expected to: eq(expected) * 5 conversions from: lambda { }.should to: expect { }.to * 2 conversions from: be_true to: be_truthy For more details: https://github.com/yujinakayama/transpec#supported-conversions --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 36 ++++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'spec/unit/puppet/provider') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index a016b68..d2a129c 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -23,17 +23,17 @@ describe provider_class do File.open(tmpfile, 'w') do |fh| fh.write('foo') end - provider.exists?.should be_true + expect(provider.exists?).to be_truthy end it 'should detect if the line does not exist in the file' do File.open(tmpfile, 'w') do |fh| fh.write('foo1') end - provider.exists?.should be_nil + expect(provider.exists?).to be_nil end it 'should append to an existing file when creating' do provider.create - File.read(tmpfile).chomp.should == 'foo' + expect(File.read(tmpfile).chomp).to eq('foo') end end @@ -61,9 +61,9 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - @provider.exists?.should be_nil + expect(@provider.exists?).to be_nil expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/) - File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz") + expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end it 'should replace all lines that matches' do @@ -80,9 +80,9 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - @provider.exists?.should be_nil + expect(@provider.exists?).to be_nil @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar") + expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") end it 'should raise an error with invalid values' do @@ -103,25 +103,25 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2") end - @provider.exists?.should be_nil + expect(@provider.exists?).to be_nil @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") + expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end it 'should add a new line if no lines match' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo2") end - @provider.exists?.should be_nil + expect(@provider.exists?).to be_nil @provider.create - File.read(@tmpfile).should eql("foo1\nfoo2\nfoo = bar\n") + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") end it 'should do nothing if the exact line already exists' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo = bar\nfoo2") end - @provider.exists?.should be_true + expect(@provider.exists?).to be_truthy @provider.create - File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2") + expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end end @@ -150,7 +150,7 @@ describe provider_class do it 'inserts the specified line after the line matching the "after" expression' do provider.create - File.read(@tmpfile).chomp.should eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") + expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") end end @@ -179,7 +179,7 @@ describe provider_class do it 'appends the specified line to the file' do provider.create - File.read(@tmpfile).should eq(content << resource[:line] << "\n") + expect(File.read(@tmpfile)).to eq(content << resource[:line] << "\n") end end end @@ -203,7 +203,7 @@ describe provider_class do fh.write("foo1\nfoo\nfoo2") end @provider.destroy - File.read(@tmpfile).should eql("foo1\nfoo2") + expect(File.read(@tmpfile)).to eql("foo1\nfoo2") end it 'should remove the line without touching the last new line' do @@ -211,7 +211,7 @@ describe provider_class do fh.write("foo1\nfoo\nfoo2\n") end @provider.destroy - File.read(@tmpfile).should eql("foo1\nfoo2\n") + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n") end it 'should remove any occurence of the line' do @@ -219,7 +219,7 @@ describe provider_class do fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") end @provider.destroy - File.read(@tmpfile).should eql("foo1\nfoo2\n") + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n") end end end -- cgit v1.2.3