]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / unit / puppet / provider / file_line / ruby_spec.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper'
3 require 'tempfile'
4 provider_class = Puppet::Type.type(:file_line).provider(:ruby)
5 describe provider_class do
6   context "when adding" do
7     let :tmpfile do
8       tmp = Tempfile.new('tmp')
9       path = tmp.path
10       tmp.close!
11       path
12     end
13     let :resource do
14       Puppet::Type::File_line.new(
15         {:name => 'foo', :path => tmpfile, :line => 'foo'}
16       )
17     end
18     let :provider do
19       provider_class.new(resource)
20     end
21
22     it 'should detect if the line exists in the file' do
23       File.open(tmpfile, 'w') do |fh|
24         fh.write('foo')
25       end
26       expect(provider.exists?).to be_truthy
27     end
28     it 'should detect if the line does not exist in the file' do
29       File.open(tmpfile, 'w') do |fh|
30         fh.write('foo1')
31       end
32       expect(provider.exists?).to be_nil
33     end
34     it 'should append to an existing file when creating' do
35       provider.create
36       expect(File.read(tmpfile).chomp).to eq('foo')
37     end
38   end
39
40   context "when matching" do
41     before :each 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')
46       @tmpfile = tmp.path
47       tmp.close!
48       @resource = Puppet::Type::File_line.new(
49         {
50           :name  => 'foo',
51           :path  => @tmpfile,
52           :line  => 'foo = bar',
53           :match => '^foo\s*=.*$',
54         }
55       )
56       @provider = provider_class.new(@resource)
57     end
58
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")
63         end
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")
67       end
68
69       it 'should replace all lines that matches' do
70         @resource = Puppet::Type::File_line.new(
71           {
72             :name     => 'foo',
73             :path     => @tmpfile,
74             :line     => 'foo = bar',
75             :match    => '^foo\s*=.*$',
76             :multiple => true,
77           }
78         )
79         @provider = provider_class.new(@resource)
80         File.open(@tmpfile, 'w') do |fh|
81           fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
82         end
83         expect(@provider.exists?).to be_nil
84         @provider.create
85         expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
86       end
87
88       it 'should raise an error with invalid values' do
89         expect {
90           @resource = Puppet::Type::File_line.new(
91             {
92               :name     => 'foo',
93               :path     => @tmpfile,
94               :line     => 'foo = bar',
95               :match    => '^foo\s*=.*$',
96               :multiple => 'asgadga',
97             }
98           )
99         }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
100       end
101
102       it 'should replace a line that matches' do
103         File.open(@tmpfile, 'w') do |fh|
104           fh.write("foo1\nfoo=blah\nfoo2")
105         end
106         expect(@provider.exists?).to be_nil
107         @provider.create
108         expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
109       end
110       it 'should add a new line if no lines match' do
111         File.open(@tmpfile, 'w') do |fh|
112           fh.write("foo1\nfoo2")
113         end
114         expect(@provider.exists?).to be_nil
115         @provider.create
116         expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
117       end
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")
121         end
122         expect(@provider.exists?).to be_truthy
123         @provider.create
124         expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
125       end
126     end
127
128     describe 'using after' do
129       let :resource do
130         Puppet::Type::File_line.new(
131           {
132             :name  => 'foo',
133             :path  => @tmpfile,
134             :line  => 'inserted = line',
135             :after => '^foo1',
136           }
137         )
138       end
139
140       let :provider do
141         provider_class.new(resource)
142       end
143       context 'match and after set' do
144         shared_context 'resource_create' do
145           let(:match) { '^foo2$' }
146           let(:after) { '^foo1$' }
147           let(:resource) {
148             Puppet::Type::File_line.new(
149               {
150                 :name  => 'foo',
151                 :path  => @tmpfile,
152                 :line  => 'inserted = line',
153                 :after => after,
154                 :match => match,
155               }
156             )
157           }
158         end
159         before :each do
160           File.open(@tmpfile, 'w') do |fh|
161             fh.write("foo1\nfoo2\nfoo = baz")
162           end
163         end
164         describe 'inserts at match' do
165           include_context 'resource_create'
166           it {
167             provider.create
168             expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
169           }
170         end
171         describe 'inserts a new line after when no match' do
172           include_context 'resource_create' do
173             let(:match) { '^nevergoingtomatch$' }
174           end
175           it {
176             provider.create
177             expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
178           }
179         end
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' }
184           end
185           it {
186             provider.create
187             expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
188           }
189         end
190       end
191       context 'with one line matching the after expression' do
192         before :each do
193           File.open(@tmpfile, 'w') do |fh|
194             fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
195           end
196         end
197
198         it 'inserts the specified line after the line matching the "after" expression' do
199           provider.create
200           expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
201         end
202       end
203
204       context 'with two lines matching the after expression' do
205         before :each do
206           File.open(@tmpfile, 'w') do |fh|
207             fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
208           end
209         end
210
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/)
213         end
214       end
215
216       context 'with no lines matching the after expression' do
217         let :content do
218           "foo3\nfoo = blah\nfoo2\nfoo = baz\n"
219         end
220
221         before :each do
222           File.open(@tmpfile, 'w') do |fh|
223             fh.write(content)
224           end
225         end
226
227         it 'appends the specified line to the file' do
228           provider.create
229           expect(File.read(@tmpfile)).to eq(content << resource[:line] << "\n")
230         end
231       end
232     end
233   end
234
235   context "when removing" do
236     before :each 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')
241       @tmpfile = tmp.path
242       tmp.close!
243       @resource = Puppet::Type::File_line.new(
244         {
245           :name   => 'foo',
246           :path   => @tmpfile,
247           :line   => 'foo',
248           :ensure => 'absent',
249         }
250       )
251       @provider = provider_class.new(@resource)
252     end
253     it 'should remove the line if it exists' do
254       File.open(@tmpfile, 'w') do |fh|
255         fh.write("foo1\nfoo\nfoo2")
256       end
257       @provider.destroy
258       expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
259     end
260
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")
264       end
265       @provider.destroy
266       expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
267     end
268
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")
272       end
273       @provider.destroy
274       expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
275     end
276   end
277 end