Skip to content

Commit 578220e

Browse files
authored
Merge pull request #788 from tphoney/MODULES-5003
(MODULES-5003) file_line fix all broken lines
2 parents 4c1b51c + 58131d8 commit 578220e

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

lib/puppet/provider/file_line/ruby.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
Puppet::Type.type(:file_line).provide(:ruby) do
22
def exists?
3+
found = true
34
if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
4-
true
5+
found = true
56
else
67
lines.find do |line|
78
if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true'
8-
line.chomp =~ Regexp.new(resource[:match])
9+
found = line.chomp =~ Regexp.new(resource[:match])
910
else
10-
line.chomp == resource[:line].chomp
11+
found = line.chomp == resource[:line].chomp
12+
end
13+
if found == false then
14+
break
1115
end
1216
end
1317
end
18+
found
1419
end
1520

1621
def create

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
File.open(tmpfile, 'w') do |fh|
3131
fh.write('foo1')
3232
end
33-
expect(provider.exists?).to be_nil
33+
expect(provider.exists?).to eql (false)
3434
end
3535
it 'should append to an existing file when creating' do
3636
provider.create
@@ -70,7 +70,7 @@
7070
File.open(@tmpfile, 'w') do |fh|
7171
fh.write("foo1\nfoo2")
7272
end
73-
expect(@provider.exists?).to be_nil
73+
expect(@provider.exists?).to eql (false)
7474
@provider.create
7575
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
7676
end
@@ -113,7 +113,7 @@
113113
File.open(@tmpfile, 'w') do |fh|
114114
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
115115
end
116-
expect(@provider.exists?).to be_nil
116+
expect(@provider.exists?).to eql(false)
117117
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
118118
expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
119119
end
@@ -132,11 +132,30 @@
132132
File.open(@tmpfile, 'w') do |fh|
133133
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
134134
end
135-
expect(@provider.exists?).to be_nil
135+
expect(@provider.exists?).to eql(false)
136136
@provider.create
137137
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
138138
end
139139

140+
it 'should replace all lines that match, even when some lines are correct' do
141+
@resource = Puppet::Type::File_line.new(
142+
{
143+
:name => 'neil',
144+
:path => @tmpfile,
145+
:line => "\thard\tcore\t0\n",
146+
:match => '^[ \t]hard[ \t]+core[ \t]+.*',
147+
:multiple => true,
148+
}
149+
)
150+
@provider = provider_class.new(@resource)
151+
File.open(@tmpfile, 'w') do |fh|
152+
fh.write("\thard\tcore\t90\n\thard\tcore\t0\n")
153+
end
154+
expect(@provider.exists?).to eql(false)
155+
@provider.create
156+
expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
157+
end
158+
140159
it 'should raise an error with invalid values' do
141160
expect {
142161
@resource = Puppet::Type::File_line.new(
@@ -155,23 +174,23 @@
155174
File.open(@tmpfile, 'w') do |fh|
156175
fh.write("foo1\nfoo=blah\nfoo2")
157176
end
158-
expect(@provider.exists?).to be_nil
177+
expect(@provider.exists?).to eql(false)
159178
@provider.create
160179
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
161180
end
162181
it 'should add a new line if no lines match' do
163182
File.open(@tmpfile, 'w') do |fh|
164183
fh.write("foo1\nfoo2")
165184
end
166-
expect(@provider.exists?).to be_nil
185+
expect(@provider.exists?).to eql(false)
167186
@provider.create
168187
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
169188
end
170189
it 'should do nothing if the exact line already exists' do
171190
File.open(@tmpfile, 'w') do |fh|
172191
fh.write("foo1\nfoo = bar\nfoo2")
173192
end
174-
expect(@provider.exists?).to be_truthy
193+
expect(@provider.exists?).to eql(false)
175194
@provider.create
176195
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
177196
end
@@ -275,7 +294,7 @@
275294
}
276295
)
277296
@provider = provider_class.new(@resource)
278-
expect(@provider.exists?).to be_nil
297+
expect(@provider.exists?).to eql (false)
279298
@provider.create
280299
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
281300
end
@@ -368,7 +387,7 @@
368387
File.open(@tmpfile, 'w') do |fh|
369388
fh.write("foo1\nfoo\nfoo2")
370389
end
371-
expect(@provider.exists?).to be_truthy
390+
expect(@provider.exists?).to be_nil
372391
end
373392

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

0 commit comments

Comments
 (0)