Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / unit / puppet / provider / file_line / ruby_spec.rb
1 require 'spec_helper'
2
3 provider_class = Puppet::Type.type(:file_line).provider(:ruby)
4 #  These tests fail on windows when run as part of the rake task. Individually they pass
5 describe provider_class, :unless => Puppet::Util::Platform.windows? do
6   include PuppetlabsSpec::Files
7
8   let :tmpfile do
9     tmpfilename('file_line_test')
10   end
11   let :content do
12     ''
13   end
14   let :params do
15     {}
16   end
17   let :resource do
18     Puppet::Type::File_line.new({
19       :name => 'foo',
20       :path => tmpfile,
21       :line => 'foo',
22     }.merge(params))
23   end
24   let :provider do
25     provider_class.new(resource)
26   end
27
28   before :each do
29     File.open(tmpfile, 'w') do |fh|
30       fh.write(content)
31     end
32   end
33
34   describe 'line parameter' do
35     context 'when line exists' do
36       let(:content) { 'foo' }
37
38       it 'detects the line' do
39         expect(provider).to be_exists
40       end
41     end
42     context 'when line does not exist' do
43       let(:content) { 'foo bar' }
44
45       it 'requests changes' do
46         expect(provider).not_to be_exists
47       end
48       it 'appends the line' do
49         provider.create
50         expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo")
51       end
52     end
53   end
54
55   describe 'match parameter' do
56     let(:params) { { :match => '^bar' } }
57
58     describe 'does not match line - line does not exist - replacing' do
59       let(:content) { "foo bar\nbar" }
60
61       it 'requests changes' do
62         expect(provider).not_to be_exists
63       end
64       it 'replaces the match' do
65         provider.create
66         expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo")
67       end
68     end
69
70     describe 'does not match line - line does not exist - appending' do
71       let(:params) { super().merge(:replace => false) }
72       let(:content) { "foo bar\nbar" }
73
74       it 'does not request changes' do
75         expect(provider).to be_exists
76       end
77     end
78
79     context 'when does not match line - line exists' do
80       let(:content) { "foo\nbar" }
81
82       it 'detects the line' do
83         expect(provider).to be_exists
84       end
85     end
86
87     context 'when matches line - line exists' do
88       let(:params) { { :match => '^foo' } }
89       let(:content) { "foo\nbar" }
90
91       it 'detects the line' do
92         expect(provider).to be_exists
93       end
94     end
95
96     context 'when matches line - line does not exist' do
97       let(:params) { { :match => '^foo' } }
98       let(:content) { "foo bar\nbar" }
99
100       it 'requests changes' do
101         expect(provider).not_to be_exists
102       end
103       it 'replaces the match' do
104         provider.create
105         expect(File.read(tmpfile).chomp).to eq("foo\nbar")
106       end
107     end
108   end
109
110   describe 'append_on_no_match' do
111     let(:params) do
112       {
113         :append_on_no_match => false,
114         :match => '^foo1$',
115       }
116     end
117
118     context 'when matching' do
119       let(:content) { "foo1\nbar" }
120
121       it 'requests changes' do
122         expect(provider).not_to be_exists
123       end
124       it 'replaces the match' do
125         provider.create
126         expect(File.read(tmpfile).chomp).to eql("foo\nbar")
127       end
128     end
129     context 'when not matching' do
130       let(:content) { "foo3\nbar" }
131
132       it 'does not affect the file' do
133         expect(provider).to be_exists
134       end
135     end
136   end
137
138   describe 'replace_all_matches_not_matching_line' do
139     context 'when replace is false' do
140       let(:params) do
141         {
142           :replace_all_matches_not_matching_line => true,
143           :replace => false,
144         }
145       end
146
147       it 'raises an error' do
148         expect { provider.exists? }.to raise_error(Puppet::Error, %r{replace must be true})
149       end
150     end
151
152     context 'when match matches line - when there are more matches than lines' do
153       let(:params) do
154         {
155           :replace_all_matches_not_matching_line => true,
156           :match => '^foo',
157           :multiple => true,
158         }
159       end
160       let(:content) { "foo\nfoo bar\nbar\nfoo baz" }
161
162       it 'requests changes' do
163         expect(provider).not_to be_exists
164       end
165       it 'replaces the matches' do
166         provider.create
167         expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nbar\nfoo")
168       end
169     end
170
171     context 'when match matches line - when there are the same matches and lines' do
172       let(:params) do
173         {
174           :replace_all_matches_not_matching_line => true,
175           :match => '^foo',
176           :multiple => true,
177         }
178       end
179       let(:content) { "foo\nfoo\nbar" }
180
181       it 'does not request changes' do
182         expect(provider).to be_exists
183       end
184     end
185
186     context 'when match does not match line - when there are more matches than lines' do
187       let(:params) do
188         {
189           :replace_all_matches_not_matching_line => true,
190           :match => '^bar',
191           :multiple => true,
192         }
193       end
194       let(:content) { "foo\nfoo bar\nbar\nbar baz" }
195
196       it 'requests changes' do
197         expect(provider).not_to be_exists
198       end
199       it 'replaces the matches' do
200         provider.create
201         expect(File.read(tmpfile).chomp).to eql("foo\nfoo bar\nfoo\nfoo")
202       end
203     end
204
205     context 'when match does not match line - when there are the same matches and lines' do
206       let(:params) do
207         {
208           :replace_all_matches_not_matching_line => true,
209           :match => '^bar',
210           :multiple => true,
211         }
212       end
213       let(:content) { "foo\nfoo\nbar\nbar baz" }
214
215       it 'requests changes' do
216         expect(provider).not_to be_exists
217       end
218       it 'replaces the matches' do
219         provider.create
220         expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nfoo\nfoo")
221       end
222     end
223   end
224
225   context 'when match does not match line - when there are no matches' do
226     let(:params) do
227       {
228         :replace_all_matches_not_matching_line => true,
229         :match => '^bar',
230         :multiple => true,
231       }
232     end
233     let(:content) { "foo\nfoo bar" }
234
235     it 'does not request changes' do
236       expect(provider).to be_exists
237     end
238   end
239
240   context 'when match does not match line - when there are no matches or lines' do
241     let(:params) do
242       {
243         :replace_all_matches_not_matching_line => true,
244         :match => '^bar',
245         :multiple => true,
246       }
247     end
248     let(:content) { 'foo bar' }
249
250     it 'requests changes' do
251       expect(provider).not_to be_exists
252     end
253     it 'appends the line' do
254       provider.create
255       expect(File.read(tmpfile).chomp).to eql("foo bar\nfoo")
256     end
257   end
258 end