Skip to content

Commit b97c657

Browse files
authored
Merge pull request #1251 from silug/MODULES-2892
(MODULES-2892) Handle missing file in file_line
2 parents 3e43cb0 + 2dbe43c commit b97c657

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/puppet/provider/file_line/ruby.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def lines
8383
rescue TypeError => _e
8484
# Ruby 1.8 doesn't support open_args
8585
@lines ||= File.readlines(resource[:path])
86+
rescue Errno::ENOENT
87+
raise unless resource.noop?
88+
@lines ||= []
8689
end
8790

8891
def new_after_regex

spec/acceptance/file_line_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
ensure => present,
2121
content => 'a wild test file has appeared!',
2222
}
23+
file { '#{test_file}.does_not_exist':
24+
ensure => absent,
25+
}
2326
MANIFEST
2427
apply_manifest(pp_test_file)
2528
end
@@ -97,4 +100,55 @@
97100
end
98101
end
99102
end
103+
104+
context 'when file does not exist' do
105+
context 'with ensure => present' do
106+
let(:pp) do
107+
<<~MANIFEST
108+
file_line { 'test_absent_file':
109+
ensure => present,
110+
path => '#{test_file}.does_not_exist',
111+
line => 'this file does not exist',
112+
}
113+
MANIFEST
114+
end
115+
116+
it 'fails to apply manifest' do
117+
apply_manifest(pp, expect_failures: true)
118+
end
119+
end
120+
121+
context 'with ensure => present and noop => true' do
122+
let(:pp) do
123+
<<~MANIFEST
124+
file_line { 'test_absent_file':
125+
ensure => present,
126+
path => '#{test_file}.does_not_exist',
127+
line => 'this file does not exist',
128+
noop => true,
129+
}
130+
MANIFEST
131+
end
132+
133+
it 'would apply manifest' do
134+
apply_manifest(pp, catch_failures: true)
135+
end
136+
end
137+
138+
context 'with ensure => present, in noop mode' do
139+
let(:pp) do
140+
<<~MANIFEST
141+
file_line { 'test_absent_file':
142+
ensure => present,
143+
path => '#{test_file}.does_not_exist',
144+
line => 'this file does not exist',
145+
}
146+
MANIFEST
147+
end
148+
149+
it 'would apply manifest' do
150+
apply_manifest(pp, catch_failures: true, noop: true)
151+
end
152+
end
153+
end
100154
end

0 commit comments

Comments
 (0)