Skip to content

Commit 81d7d35

Browse files
committed
(MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches
The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension.
1 parent 54d4937 commit 81d7d35

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

lib/puppet/provider/file_line/ruby.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
Puppet::Type.type(:file_line).provide(:ruby) do
22
def exists?
3-
found = true
4-
if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
5-
found = true
3+
found = false
4+
lines_count = 0
5+
lines.each do |line|
6+
found = line.chomp == resource[:line]
7+
if found
8+
lines_count += 1
9+
end
10+
end
11+
if resource[:match] == nil
12+
found = lines_count > 0
613
else
7-
lines.find do |line|
8-
if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true'
9-
found = line.chomp =~ Regexp.new(resource[:match])
10-
else
11-
found = line.chomp == resource[:line].chomp
12-
end
13-
if found == false then
14-
break
15-
end
14+
match_count = count_matches(match_regex)
15+
if resource[:replace].to_s == 'true'
16+
found = lines_count > 0 && lines_count == match_count
17+
else
18+
found = match_count > 0
1619
end
1720
end
1821
found

spec/unit/puppet/provider/file_line/ruby_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
File.open(@tmpfile, 'w') do |fh|
191191
fh.write("foo1\nfoo = bar\nfoo2")
192192
end
193-
expect(@provider.exists?).to eql(false)
193+
expect(@provider.exists?).to eql(true)
194194
@provider.create
195195
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
196196
end
@@ -387,7 +387,7 @@
387387
File.open(@tmpfile, 'w') do |fh|
388388
fh.write("foo1\nfoo\nfoo2")
389389
end
390-
expect(@provider.exists?).to be_nil
390+
expect(@provider.exists?).to eql (true)
391391
end
392392

393393
it 'should remove one line if it matches' do

0 commit comments

Comments
 (0)