summaryrefslogtreecommitdiff
path: root/spec/unit/puppet
diff options
context:
space:
mode:
authorStephen Benjamin <stephen@bitbin.de>2014-05-14 20:33:57 +0200
committerStephen Benjamin <stephen@bitbin.de>2014-05-14 20:33:57 +0200
commit6eaa592cd8d5af097a4624b3d1cead74408aabf2 (patch)
tree436a27d393345adb11b3d13a349257648b6f0813 /spec/unit/puppet
parent08b00d9229961d7b3c3cba997bfb35c8d47e4c4b (diff)
downloadpuppet-stdlib-6eaa592cd8d5af097a4624b3d1cead74408aabf2.tar.gz
puppet-stdlib-6eaa592cd8d5af097a4624b3d1cead74408aabf2.tar.bz2
(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.
Diffstat (limited to 'spec/unit/puppet')
-rwxr-xr-xspec/unit/puppet/provider/file_line/ruby_spec.rb59
-rwxr-xr-xspec/unit/puppet/type/file_line_spec.rb8
2 files changed, 67 insertions, 0 deletions
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
diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb
index ab5b81b..b85b9f4 100755
--- a/spec/unit/puppet/type/file_line_spec.rb
+++ b/spec/unit/puppet/type/file_line_spec.rb
@@ -15,6 +15,14 @@ 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(