From 047c27583d3eeba6716c245ba2bbf351479c37b2 Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Wed, 15 Jun 2022 14:50:56 -0500 Subject: [PATCH 1/2] (MODULES-2892) Handle missing file in file_line In noop mode, file_line should not fail if the file does not exist. MODULES-2892 #close --- lib/puppet/provider/file_line/ruby.rb | 3 ++ spec/acceptance/file_line_spec.rb | 54 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 41e053168..714c96c8b 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 => _e + raise _e 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 From 2dbe43ceb2e38155889ec806dccdd7bb287af8fc Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Thu, 16 Jun 2022 09:28:49 -0500 Subject: [PATCH 2/2] Minor coding style change based on feedback --- lib/puppet/provider/file_line/ruby.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 714c96c8b..e84060303 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -83,8 +83,8 @@ def lines rescue TypeError => _e # Ruby 1.8 doesn't support open_args @lines ||= File.readlines(resource[:path]) - rescue Errno::ENOENT => _e - raise _e unless resource.noop? + rescue Errno::ENOENT + raise unless resource.noop? @lines ||= [] end