|
28 | 28 | end
|
29 | 29 |
|
30 | 30 | before :each do
|
31 |
| - File.open(tmpfile, 'w') do |fh| |
32 |
| - fh.write(content) |
33 |
| - end |
| 31 | + File.write(tmpfile, content) |
34 | 32 | end
|
35 | 33 |
|
36 | 34 | describe '#create' do
|
|
54 | 52 | end
|
55 | 53 |
|
56 | 54 | it 'appends the line if no matches are found' do
|
57 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } |
| 55 | + File.write(tmpfile, "foo1\nfoo2") |
58 | 56 | expect(provider.exists?).to be false
|
59 | 57 | provider.create
|
60 | 58 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
|
|
84 | 82 |
|
85 | 83 | describe 'using match' do
|
86 | 84 | it 'raises an error if more than one line matches, and should not have modified the file' do
|
87 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } |
| 85 | + File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz") |
88 | 86 | expect { @provider.create }.to raise_error(Puppet::Error, %r{More than one line.*matches})
|
89 | 87 | expect(File.read(tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
|
90 | 88 | end
|
91 | 89 |
|
92 | 90 | it 'replaces all lines that matches' do
|
93 | 91 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', multiple: true)
|
94 | 92 | @provider = provider_class.new(@resource)
|
95 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } |
| 93 | + File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz") |
96 | 94 | @provider.create
|
97 | 95 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
|
98 | 96 | end
|
99 | 97 |
|
100 | 98 | it 'replaces all lines that match, even when some lines are correct' do
|
101 | 99 | @resource = Puppet::Type::File_line.new(name: 'neil', path: tmpfile, line: "\thard\tcore\t0\n", match: '^[ \t]hard[ \t]+core[ \t]+.*', multiple: true)
|
102 | 100 | @provider = provider_class.new(@resource)
|
103 |
| - File.open(tmpfile, 'w') { |fh| fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") } |
| 101 | + File.write(tmpfile, "\thard\tcore\t90\n\thard\tcore\t0\n") |
104 | 102 | @provider.create
|
105 | 103 | expect(File.read(tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
|
106 | 104 | end
|
|
114 | 112 | end
|
115 | 113 |
|
116 | 114 | it 'replaces a line that matches' do
|
117 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2") } |
| 115 | + File.write(tmpfile, "foo1\nfoo=blah\nfoo2") |
118 | 116 | @provider.create
|
119 | 117 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
|
120 | 118 | end
|
121 | 119 |
|
122 | 120 | it 'adds a new line if no lines match' do
|
123 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } |
| 121 | + File.write(tmpfile, "foo1\nfoo2") |
124 | 122 | @provider.create
|
125 | 123 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
|
126 | 124 | end
|
127 | 125 |
|
128 | 126 | it 'does nothing if the exact line already exists' do
|
129 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") } |
| 127 | + File.write(tmpfile, "foo1\nfoo = bar\nfoo2") |
130 | 128 | @provider.create
|
131 | 129 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
|
132 | 130 | end
|
|
136 | 134 | it 'replaces line' do
|
137 | 135 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false)
|
138 | 136 | @provider = provider_class.new(@resource)
|
139 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
| 137 | + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") |
140 | 138 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
|
141 | 139 | end
|
142 | 140 | end
|
|
145 | 143 | it 'does not add line after no matches found' do
|
146 | 144 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false)
|
147 | 145 | @provider = provider_class.new(@resource)
|
148 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
| 146 | + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") |
149 | 147 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
|
150 | 148 | end
|
151 | 149 | end
|
|
181 | 179 | end
|
182 | 180 |
|
183 | 181 | before :each do
|
184 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nfoo = baz") } |
| 182 | + File.write(tmpfile, "foo1\nfoo2\nfoo = baz") |
185 | 183 | end
|
186 | 184 |
|
187 | 185 | describe 'inserts at match' do
|
|
216 | 214 |
|
217 | 215 | context 'with one line matching the after expression' do
|
218 | 216 | before :each do
|
219 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
| 217 | + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") |
220 | 218 | end
|
221 | 219 |
|
222 | 220 | it 'inserts the specified line after the line matching the "after" expression' do
|
|
227 | 225 |
|
228 | 226 | context 'with multiple lines matching the after expression' do
|
229 | 227 | before :each do
|
230 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") } |
| 228 | + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") |
231 | 229 | end
|
232 | 230 |
|
233 | 231 | it 'errors out stating "One or no line must match the pattern"' do
|
|
248 | 246 | end
|
249 | 247 |
|
250 | 248 | before :each do
|
251 |
| - File.open(tmpfile, 'w') { |fh| fh.write(content) } |
| 249 | + File.write(tmpfile, content) |
252 | 250 | end
|
253 | 251 |
|
254 | 252 | it 'appends the specified line to the file' do
|
|
273 | 271 | end
|
274 | 272 |
|
275 | 273 | it 'removes the line if it exists' do
|
276 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 274 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
277 | 275 | @provider.destroy
|
278 | 276 | expect(File.read(tmpfile)).to eql("foo1\nfoo2")
|
279 | 277 | end
|
280 | 278 |
|
281 | 279 | it 'removes the line without touching the last new line' do
|
282 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\n") } |
| 280 | + File.write(tmpfile, "foo1\nfoo\nfoo2\n") |
283 | 281 | @provider.destroy
|
284 | 282 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
|
285 | 283 | end
|
286 | 284 |
|
287 | 285 | it 'removes any occurence of the line' do
|
288 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } |
| 286 | + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") |
289 | 287 | @provider.destroy
|
290 | 288 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
|
291 | 289 | end
|
292 | 290 |
|
293 | 291 | it 'example in the docs' do
|
294 | 292 | @resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, line: 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128')
|
295 | 293 | @provider = provider_class.new(@resource)
|
296 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") } |
| 294 | + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") |
297 | 295 | @provider.destroy
|
298 | 296 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
|
299 | 297 | end
|
|
313 | 311 | end
|
314 | 312 |
|
315 | 313 | it 'finds a line to match' do
|
316 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 314 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
317 | 315 | expect(@provider.exists?).to be true
|
318 | 316 | end
|
319 | 317 |
|
320 | 318 | it 'removes one line if it matches' do
|
321 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 319 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
322 | 320 | @provider.destroy
|
323 | 321 | expect(File.read(tmpfile)).to eql("foo1\nfoo2")
|
324 | 322 | end
|
325 | 323 |
|
326 | 324 | it 'the line parameter is actually not used at all but is silently ignored if here' do
|
327 | 325 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'supercalifragilisticexpialidocious', ensure: 'absent', match: 'o$', match_for_absence: true)
|
328 | 326 | @provider = provider_class.new(@resource)
|
329 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 327 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
330 | 328 | @provider.destroy
|
331 | 329 | expect(File.read(tmpfile)).to eql("foo1\nfoo2")
|
332 | 330 | end
|
333 | 331 |
|
334 | 332 | it 'and may not be here and does not need to be here' do
|
335 | 333 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, ensure: 'absent', match: 'o$', match_for_absence: true)
|
336 | 334 | @provider = provider_class.new(@resource)
|
337 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 335 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
338 | 336 | @provider.destroy
|
339 | 337 | expect(File.read(tmpfile)).to eql("foo1\nfoo2")
|
340 | 338 | end
|
341 | 339 |
|
342 | 340 | it 'raises an error if more than one line matches' do
|
343 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } |
| 341 | + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") |
344 | 342 | expect { @provider.destroy }.to raise_error(Puppet::Error, %r{More than one line})
|
345 | 343 | end
|
346 | 344 |
|
347 | 345 | it 'removes multiple lines if :multiple is true' do
|
348 | 346 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', multiple: true, match_for_absence: true)
|
349 | 347 | @provider = provider_class.new(@resource)
|
350 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } |
| 348 | + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") |
351 | 349 | @provider.destroy
|
352 | 350 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
|
353 | 351 | end
|
354 | 352 |
|
355 | 353 | it 'ignores the match if match_for_absence is not specified' do
|
356 | 354 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$')
|
357 | 355 | @provider = provider_class.new(@resource)
|
358 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 356 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
359 | 357 | @provider.destroy
|
360 | 358 | expect(File.read(tmpfile)).to eql("foo1\nfoo\n")
|
361 | 359 | end
|
362 | 360 |
|
363 | 361 | it 'ignores the match if match_for_absence is false' do
|
364 | 362 | @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', match_for_absence: false)
|
365 | 363 | @provider = provider_class.new(@resource)
|
366 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } |
| 364 | + File.write(tmpfile, "foo1\nfoo\nfoo2") |
367 | 365 | @provider.destroy
|
368 | 366 | expect(File.read(tmpfile)).to eql("foo1\nfoo\n")
|
369 | 367 | end
|
|
374 | 372 | match: '^export\ HTTP_PROXY\=', match_for_absence: true
|
375 | 373 | )
|
376 | 374 | @provider = provider_class.new(@resource)
|
377 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } |
| 375 | + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") |
378 | 376 | @provider.destroy
|
379 | 377 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
|
380 | 378 | end
|
381 | 379 |
|
382 | 380 | it 'example in the docs showing line is redundant' do
|
383 | 381 | @resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, match: '^export\ HTTP_PROXY\=', match_for_absence: true)
|
384 | 382 | @provider = provider_class.new(@resource)
|
385 |
| - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } |
| 383 | + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") |
386 | 384 | @provider.destroy
|
387 | 385 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
|
388 | 386 | end
|
|
0 commit comments