From 58131d8ff1886352e964aafef58ffeb65c5d2cdd Mon Sep 17 00:00:00 2001 From: tphoney Date: Mon, 3 Jul 2017 17:08:09 +0100 Subject: (MODULES-5003) file_line fix all broken lines --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 37 ++++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'spec/unit/puppet/provider/file_line/ruby_spec.rb') 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 -- cgit v1.2.3 From 700b735893b77f6ebc6420f72f1e474d5053f12b Mon Sep 17 00:00:00 2001 From: tphoney Date: Fri, 7 Jul 2017 15:28:52 +0100 Subject: (MODULES-5186) dont run this test on windows --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/unit/puppet/provider/file_line/ruby_spec.rb') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 1f41f62..3d28687 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' require 'tempfile' provider_class = Puppet::Type.type(:file_line).provider(:ruby) -describe provider_class do +# These tests fail on windows when run as part of the rake task. Individually they pass +describe provider_class, :unless => Puppet::Util::Platform.windows? do context "when adding" do let :tmpfile do tmp = Tempfile.new('tmp') -- cgit v1.2.3 From 81d7d35fd78917ee1a5e66cb7459ea341bc452ea Mon Sep 17 00:00:00 2001 From: tkishel Date: Thu, 13 Jul 2017 15:16:04 -0700 Subject: (MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/provider/file_line/ruby_spec.rb') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index dcf8509..ab6edf9 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -190,7 +190,7 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo = bar\nfoo2") end - expect(@provider.exists?).to eql(false) + expect(@provider.exists?).to eql(true) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -387,7 +387,7 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (true) end it 'should remove one line if it matches' do -- cgit v1.2.3 From 703fa777e1daee97e2aea529064d773662ef1fe0 Mon Sep 17 00:00:00 2001 From: "asif.shaikh" Date: Wed, 1 Feb 2017 20:14:27 -0800 Subject: Add new file_line option append_on_no_match --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'spec/unit/puppet/provider/file_line/ruby_spec.rb') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index ab6edf9..dcca4a4 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -194,6 +194,24 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end + + it 'should not add line after no matches found' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'inserted = line', + :match => '^foo3$', + :append_on_no_match => false, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") + end + expect(@provider.exists?).to be true + expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") + end end describe 'using after' do -- cgit v1.2.3 From ad5d92461596d8d0b1c001d49061d70b36761d59 Mon Sep 17 00:00:00 2001 From: Alex Harvey Date: Fri, 29 Sep 2017 16:14:37 +1000 Subject: (maint) Clarify docs and add new tests Based on a Stack Overflow question, it was noted that the documentation for `file_line` isn't complete and the underlying behaviour somewhat confusing. https://stackoverflow.com/questions/46468922/how-to-change-the-contents-of-a-file-by-using-puppet/46473458 In this patch I add additional unit tests and better examples and complete documentation. --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 91 +++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/provider/file_line/ruby_spec.rb') diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index dcca4a4..4ec9bae 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -337,7 +337,7 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do end end - context "when removing" do + context "when removing with a line" 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: @@ -378,6 +378,23 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do @provider.destroy expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n") end + + it 'example in the docs' do + @resource = Puppet::Type::File_line.new( + { + :name => 'bashrc_proxy', + :ensure => 'absent', + :path => @tmpfile, + :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") + end + @provider.destroy + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end end context "when removing with a match" do @@ -416,6 +433,41 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do expect(File.read(@tmpfile)).to eql("foo1\nfoo2") end + it 'the line parameter is actually not used at all but is silently ignored if here' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'supercalifragilisticexpialidocious', + :ensure => 'absent', + :match => 'o$', + :match_for_absence => true, + } + ) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo\nfoo2") + end + @provider.destroy + expect(File.read(@tmpfile)).to eql("foo1\nfoo2") + end + + it 'and may not be here and does not need to be here' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :ensure => 'absent', + :match => 'o$', + :match_for_absence => true, + } + ) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo\nfoo2") + end + @provider.destroy + expect(File.read(@tmpfile)).to eql("foo1\nfoo2") + end + it 'should raise an error if more than one line matches' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") @@ -480,6 +532,41 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do expect(File.read(@tmpfile)).to eql("foo1\nfoo\n") end - end + it 'example in the docs' do + @resource = Puppet::Type::File_line.new( + { + :name => 'bashrc_proxy', + :ensure => 'absent', + :path => @tmpfile, + :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + :match => '^export\ HTTP_PROXY\=', + :match_for_absence => true, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") + end + @provider.destroy + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end + it 'example in the docs showing line is redundant' do + @resource = Puppet::Type::File_line.new( + { + :name => 'bashrc_proxy', + :ensure => 'absent', + :path => @tmpfile, + :match => '^export\ HTTP_PROXY\=', + :match_for_absence => true, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") + end + @provider.destroy + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end + end end -- cgit v1.2.3