diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 41e053168..e84060303 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -83,6 +83,9 @@ def lines rescue TypeError => _e # Ruby 1.8 doesn't support open_args @lines ||= File.readlines(resource[:path]) + rescue Errno::ENOENT + raise unless resource.noop? + @lines ||= [] end def new_after_regex diff --git a/spec/acceptance/file_line_spec.rb b/spec/acceptance/file_line_spec.rb index a322ab111..6971c2eff 100644 --- a/spec/acceptance/file_line_spec.rb +++ b/spec/acceptance/file_line_spec.rb @@ -20,6 +20,9 @@ ensure => present, content => 'a wild test file has appeared!', } + file { '#{test_file}.does_not_exist': + ensure => absent, + } MANIFEST apply_manifest(pp_test_file) end @@ -97,4 +100,55 @@ end end end + + context 'when file does not exist' do + context 'with ensure => present' do + let(:pp) do + <<~MANIFEST + file_line { 'test_absent_file': + ensure => present, + path => '#{test_file}.does_not_exist', + line => 'this file does not exist', + } + MANIFEST + end + + it 'fails to apply manifest' do + apply_manifest(pp, expect_failures: true) + end + end + + context 'with ensure => present and noop => true' do + let(:pp) do + <<~MANIFEST + file_line { 'test_absent_file': + ensure => present, + path => '#{test_file}.does_not_exist', + line => 'this file does not exist', + noop => true, + } + MANIFEST + end + + it 'would apply manifest' do + apply_manifest(pp, catch_failures: true) + end + end + + context 'with ensure => present, in noop mode' do + let(:pp) do + <<~MANIFEST + file_line { 'test_absent_file': + ensure => present, + path => '#{test_file}.does_not_exist', + line => 'this file does not exist', + } + MANIFEST + end + + it 'would apply manifest' do + apply_manifest(pp, catch_failures: true, noop: true) + end + end + end end