Skip to content

Commit 58131d8

Browse files
author
tphoney
committed
(MODULES-5003) file_line fix all broken lines
1 parent 517a133 commit 58131d8

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
@@ -29,7 +29,7 @@
2929
File.open(tmpfile, 'w') do |fh|
3030
fh.write('foo1')
3131
end
32-
expect(provider.exists?).to be_nil
32+
expect(provider.exists?).to eql (false)
3333
end
3434
it 'should append to an existing file when creating' do
3535
provider.create
@@ -69,7 +69,7 @@
6969
File.open(@tmpfile, 'w') do |fh|
7070
fh.write("foo1\nfoo2")
7171
end
72-
expect(@provider.exists?).to be_nil
72+
expect(@provider.exists?).to eql (false)
7373
@provider.create
7474
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
7575
end
@@ -112,7 +112,7 @@
112112
File.open(@tmpfile, 'w') do |fh|
113113
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
114114
end
115-
expect(@provider.exists?).to be_nil
115+
expect(@provider.exists?).to eql(false)
116116
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
117117
expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
118118
end
@@ -131,11 +131,30 @@
131131
File.open(@tmpfile, 'w') do |fh|
132132
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
133133
end
134-
expect(@provider.exists?).to be_nil
134+
expect(@provider.exists?).to eql(false)
135135
@provider.create
136136
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
137137
end
138138

139+
it 'should replace all lines that match, even when some lines are correct' do
140+
@resource = Puppet::Type::File_line.new(
141+
{
142+
:name => 'neil',
143+
:path => @tmpfile,
144+
:line => "\thard\tcore\t0\n",
145+
:match => '^[ \t]hard[ \t]+core[ \t]+.*',
146+
:multiple => true,
147+
}
148+
)
149+
@provider = provider_class.new(@resource)
150+
File.open(@tmpfile, 'w') do |fh|
151+
fh.write("\thard\tcore\t90\n\thard\tcore\t0\n")
152+
end
153+
expect(@provider.exists?).to eql(false)
154+
@provider.create
155+
expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
156+
end
157+
139158
it 'should raise an error with invalid values' do
140159
expect {
141160
@resource = Puppet::Type::File_line.new(
@@ -154,23 +173,23 @@
154173
File.open(@tmpfile, 'w') do |fh|
155174
fh.write("foo1\nfoo=blah\nfoo2")
156175
end
157-
expect(@provider.exists?).to be_nil
176+
expect(@provider.exists?).to eql(false)
158177
@provider.create
159178
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
160179
end
161180
it 'should add a new line if no lines match' do
162181
File.open(@tmpfile, 'w') do |fh|
163182
fh.write("foo1\nfoo2")
164183
end
165-
expect(@provider.exists?).to be_nil
184+
expect(@provider.exists?).to eql(false)
166185
@provider.create
167186
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
168187
end
169188
it 'should do nothing if the exact line already exists' do
170189
File.open(@tmpfile, 'w') do |fh|
171190
fh.write("foo1\nfoo = bar\nfoo2")
172191
end
173-
expect(@provider.exists?).to be_truthy
192+
expect(@provider.exists?).to eql(false)
174193
@provider.create
175194
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
176195
end
@@ -274,7 +293,7 @@
274293
}
275294
)
276295
@provider = provider_class.new(@resource)
277-
expect(@provider.exists?).to be_nil
296+
expect(@provider.exists?).to eql (false)
278297
@provider.create
279298
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
280299
end
@@ -367,7 +386,7 @@
367386
File.open(@tmpfile, 'w') do |fh|
368387
fh.write("foo1\nfoo\nfoo2")
369388
end
370-
expect(@provider.exists?).to be_truthy
389+
expect(@provider.exists?).to be_nil
371390
end
372391

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

0 commit comments

Comments
 (0)