1 #! /usr/bin/env ruby -S rspec
4 provider_class = Puppet::Type.type(:file_line).provider(:ruby)
5 describe provider_class do
6 context "when adding" do
8 tmp = Tempfile.new('tmp')
14 Puppet::Type::File_line.new(
15 {:name => 'foo', :path => tmpfile, :line => 'foo'}
19 provider_class.new(resource)
22 it 'should detect if the line exists in the file' do
23 File.open(tmpfile, 'w') do |fh|
26 expect(provider.exists?).to be_truthy
28 it 'should detect if the line does not exist in the file' do
29 File.open(tmpfile, 'w') do |fh|
32 expect(provider.exists?).to be_nil
34 it 'should append to an existing file when creating' do
36 expect(File.read(tmpfile).chomp).to eq('foo')
40 context "when matching" do
42 # TODO: these should be ported over to use the PuppetLabs spec_helper
43 # file fixtures once the following pull request has been merged:
44 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
45 tmp = Tempfile.new('tmp')
48 @resource = Puppet::Type::File_line.new(
53 :match => '^foo\s*=.*$',
56 @provider = provider_class.new(@resource)
59 describe 'using match' do
60 it 'should raise an error if more than one line matches, and should not have modified the file' do
61 File.open(@tmpfile, 'w') do |fh|
62 fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
64 expect(@provider.exists?).to be_nil
65 expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
66 expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
69 it 'should replace all lines that matches' do
70 @resource = Puppet::Type::File_line.new(
75 :match => '^foo\s*=.*$',
79 @provider = provider_class.new(@resource)
80 File.open(@tmpfile, 'w') do |fh|
81 fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
83 expect(@provider.exists?).to be_nil
85 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
88 it 'should raise an error with invalid values' do
90 @resource = Puppet::Type::File_line.new(
95 :match => '^foo\s*=.*$',
96 :multiple => 'asgadga',
99 }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
102 it 'should replace a line that matches' do
103 File.open(@tmpfile, 'w') do |fh|
104 fh.write("foo1\nfoo=blah\nfoo2")
106 expect(@provider.exists?).to be_nil
108 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
110 it 'should add a new line if no lines match' do
111 File.open(@tmpfile, 'w') do |fh|
112 fh.write("foo1\nfoo2")
114 expect(@provider.exists?).to be_nil
116 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
118 it 'should do nothing if the exact line already exists' do
119 File.open(@tmpfile, 'w') do |fh|
120 fh.write("foo1\nfoo = bar\nfoo2")
122 expect(@provider.exists?).to be_truthy
124 expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
128 describe 'using after' do
130 Puppet::Type::File_line.new(
134 :line => 'inserted = line',
141 provider_class.new(resource)
143 context 'match and after set' do
144 shared_context 'resource_create' do
145 let(:match) { '^foo2$' }
146 let(:after) { '^foo1$' }
148 Puppet::Type::File_line.new(
152 :line => 'inserted = line',
160 File.open(@tmpfile, 'w') do |fh|
161 fh.write("foo1\nfoo2\nfoo = baz")
164 describe 'inserts at match' do
165 include_context 'resource_create'
168 expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
171 describe 'inserts a new line after when no match' do
172 include_context 'resource_create' do
173 let(:match) { '^nevergoingtomatch$' }
177 expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
180 describe 'append to end of file if no match for both after and match' do
181 include_context 'resource_create' do
182 let(:match) { '^nevergoingtomatch$' }
183 let(:after) { '^stillneverafter' }
187 expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
191 context 'with one line matching the after expression' do
193 File.open(@tmpfile, 'w') do |fh|
194 fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
198 it 'inserts the specified line after the line matching the "after" expression' do
200 expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
204 context 'with two lines matching the after expression' do
206 File.open(@tmpfile, 'w') do |fh|
207 fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
211 it 'errors out stating "One or no line must match the pattern"' do
212 expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/)
216 context 'with no lines matching the after expression' do
218 "foo3\nfoo = blah\nfoo2\nfoo = baz\n"
222 File.open(@tmpfile, 'w') do |fh|
227 it 'appends the specified line to the file' do
229 expect(File.read(@tmpfile)).to eq(content << resource[:line] << "\n")
235 context "when removing" do
237 # TODO: these should be ported over to use the PuppetLabs spec_helper
238 # file fixtures once the following pull request has been merged:
239 # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
240 tmp = Tempfile.new('tmp')
243 @resource = Puppet::Type::File_line.new(
251 @provider = provider_class.new(@resource)
253 it 'should remove the line if it exists' do
254 File.open(@tmpfile, 'w') do |fh|
255 fh.write("foo1\nfoo\nfoo2")
258 expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
261 it 'should remove the line without touching the last new line' do
262 File.open(@tmpfile, 'w') do |fh|
263 fh.write("foo1\nfoo\nfoo2\n")
266 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
269 it 'should remove any occurence of the line' do
270 File.open(@tmpfile, 'w') do |fh|
271 fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
274 expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")