Skip to content

Commit c56c13f

Browse files
Travis Fieldsbmjen
authored andcommitted
Add spec tests and pulled in PR #427
Changed append line to open in 'w' mode and have to rewrite lines in order to append new line
1 parent 69623c9 commit c56c13f

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

lib/puppet/provider/file_line/ruby.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def count_matches(regex)
8787
#
8888
# @api private
8989
def append_line
90-
File.open(resource[:path], 'a') do |fh|
90+
File.open(resource[:path], 'w') do |fh|
91+
lines.each do |l|
92+
fh.puts(l)
93+
end
9194
fh.puts resource[:line]
9295
end
9396
end

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
@tmpfile = tmp.path
4747
tmp.close!
4848
@resource = Puppet::Type::File_line.new(
49-
{
50-
:name => 'foo',
51-
:path => @tmpfile,
52-
:line => 'foo = bar',
53-
:match => '^foo\s*=.*$',
54-
}
49+
{
50+
:name => 'foo',
51+
:path => @tmpfile,
52+
:line => 'foo = bar',
53+
:match => '^foo\s*=.*$',
54+
}
5555
)
5656
@provider = provider_class.new(@resource)
5757
end
@@ -129,9 +129,9 @@
129129
let :resource do
130130
Puppet::Type::File_line.new(
131131
{
132-
:name => 'foo',
133-
:path => @tmpfile,
134-
:line => 'inserted = line',
132+
:name => 'foo',
133+
:path => @tmpfile,
134+
:line => 'inserted = line',
135135
:after => '^foo1',
136136
}
137137
)
@@ -140,7 +140,54 @@
140140
let :provider do
141141
provider_class.new(resource)
142142
end
143-
143+
context 'match and after set' do
144+
shared_context 'resource_create' do
145+
let(:match) { '^foo2$' }
146+
let(:after) { '^foo1$' }
147+
let(:resource) {
148+
Puppet::Type::File_line.new(
149+
{
150+
:name => 'foo',
151+
:path => @tmpfile,
152+
:line => 'inserted = line',
153+
:after => after,
154+
:match => match,
155+
}
156+
)
157+
}
158+
end
159+
before :each do
160+
File.open(@tmpfile, 'w') do |fh|
161+
fh.write("foo1\nfoo2\nfoo = baz")
162+
end
163+
end
164+
describe 'inserts at match' do
165+
include_context 'resource_create'
166+
it {
167+
provider.create
168+
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
169+
}
170+
end
171+
describe 'inserts a new line after when no match' do
172+
include_context 'resource_create' do
173+
let(:match) { '^nevergoingtomatch$' }
174+
end
175+
it {
176+
provider.create
177+
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
178+
}
179+
end
180+
describe 'append to end of file if no match for both after and match' do
181+
include_context 'resource_create' do
182+
let(:match) { '^nevergoingtomatch$' }
183+
let(:after) { '^stillneverafter' }
184+
end
185+
it {
186+
provider.create
187+
expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
188+
}
189+
end
190+
end
144191
context 'with one line matching the after expression' do
145192
before :each do
146193
File.open(@tmpfile, 'w') do |fh|
@@ -194,7 +241,7 @@
194241
@tmpfile = tmp.path
195242
tmp.close!
196243
@resource = Puppet::Type::File_line.new(
197-
{:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' }
244+
{:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent'}
198245
)
199246
@provider = provider_class.new(@resource)
200247
end

0 commit comments

Comments
 (0)