|
341 | 341 | expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n") |
342 | 342 | end |
343 | 343 | end |
| 344 | + |
| 345 | + context "when removing with a match" do |
| 346 | + before :each do |
| 347 | + # TODO: these should be ported over to use the PuppetLabs spec_helper |
| 348 | + # file fixtures once the following pull request has been merged: |
| 349 | + # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files |
| 350 | + tmp = Tempfile.new('tmp') |
| 351 | + @tmpfile = tmp.path |
| 352 | + tmp.close! |
| 353 | + @resource = Puppet::Type::File_line.new( |
| 354 | + { |
| 355 | + :name => 'foo', |
| 356 | + :path => @tmpfile, |
| 357 | + :line => 'foo2', |
| 358 | + :ensure => 'absent', |
| 359 | + :match => 'o$', |
| 360 | + :match_for_absence => true, |
| 361 | + } |
| 362 | + ) |
| 363 | + @provider = provider_class.new(@resource) |
| 364 | + end |
| 365 | + |
| 366 | + it 'should remove one line if it matches' do |
| 367 | + File.open(@tmpfile, 'w') do |fh| |
| 368 | + fh.write("foo1\nfoo\nfoo2") |
| 369 | + end |
| 370 | + @provider.destroy |
| 371 | + expect(File.read(@tmpfile)).to eql("foo1\nfoo2") |
| 372 | + end |
| 373 | + |
| 374 | + it 'should raise an error if more than one line matches' do |
| 375 | + File.open(@tmpfile, 'w') do |fh| |
| 376 | + fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") |
| 377 | + end |
| 378 | + expect { @provider.destroy }.to raise_error(Puppet::Error, /More than one line/) |
| 379 | + end |
| 380 | + |
| 381 | + it 'should remove multiple lines if :multiple is true' do |
| 382 | + @resource = Puppet::Type::File_line.new( |
| 383 | + { |
| 384 | + :name => 'foo', |
| 385 | + :path => @tmpfile, |
| 386 | + :line => 'foo2', |
| 387 | + :ensure => 'absent', |
| 388 | + :match => 'o$', |
| 389 | + :multiple => true, |
| 390 | + :match_for_absence => true, |
| 391 | + } |
| 392 | + ) |
| 393 | + @provider = provider_class.new(@resource) |
| 394 | + File.open(@tmpfile, 'w') do |fh| |
| 395 | + fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") |
| 396 | + end |
| 397 | + @provider.destroy |
| 398 | + expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n") |
| 399 | + end |
| 400 | + |
| 401 | + it 'should ignore the match if match_for_absense is not specified' do |
| 402 | + @resource = Puppet::Type::File_line.new( |
| 403 | + { |
| 404 | + :name => 'foo', |
| 405 | + :path => @tmpfile, |
| 406 | + :line => 'foo2', |
| 407 | + :ensure => 'absent', |
| 408 | + :match => 'o$', |
| 409 | + } |
| 410 | + ) |
| 411 | + @provider = provider_class.new(@resource) |
| 412 | + File.open(@tmpfile, 'w') do |fh| |
| 413 | + fh.write("foo1\nfoo\nfoo2") |
| 414 | + end |
| 415 | + @provider.destroy |
| 416 | + expect(File.read(@tmpfile)).to eql("foo1\nfoo\n") |
| 417 | + end |
| 418 | + |
| 419 | + it 'should ignore the match if match_for_absense is false' do |
| 420 | + @resource = Puppet::Type::File_line.new( |
| 421 | + { |
| 422 | + :name => 'foo', |
| 423 | + :path => @tmpfile, |
| 424 | + :line => 'foo2', |
| 425 | + :ensure => 'absent', |
| 426 | + :match => 'o$', |
| 427 | + :match_for_absence => false, |
| 428 | + } |
| 429 | + ) |
| 430 | + @provider = provider_class.new(@resource) |
| 431 | + File.open(@tmpfile, 'w') do |fh| |
| 432 | + fh.write("foo1\nfoo\nfoo2") |
| 433 | + end |
| 434 | + @provider.destroy |
| 435 | + expect(File.read(@tmpfile)).to eql("foo1\nfoo\n") |
| 436 | + end |
| 437 | + |
| 438 | + end |
| 439 | + |
344 | 440 | end |
0 commit comments