Skip to content

Commit

Permalink
Add spec tests and pulled in PR #427
Browse files Browse the repository at this point in the history
Changed append line to open in 'w' mode and have to rewrite lines in order to append new line
  • Loading branch information
Travis Fields authored and bmjen committed Apr 9, 2015
1 parent 35303ce commit 0af0d7e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ def count_matches(regex)
#
# @api private
def append_line
File.open(resource[:path], 'a') do |fh|
File.open(resource[:path], 'w') do |fh|
lines.each do |l|
fh.puts(l)
end
fh.puts resource[:line]
end
end
Expand Down
88 changes: 70 additions & 18 deletions spec/unit/puppet/provider/file_line/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
}
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
}
)
@provider = provider_class.new(@resource)
end
Expand All @@ -69,11 +69,11 @@
it 'should replace all lines that matches' do
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => true
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => true,
}
)
@provider = provider_class.new(@resource)
Expand All @@ -89,11 +89,11 @@
expect {
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => 'asgadga'
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => 'asgadga',
}
)
}.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
Expand Down Expand Up @@ -140,7 +140,54 @@
let :provider do
provider_class.new(resource)
end

context 'match and after set' do
shared_context 'resource_create' do
let(:match) { '^foo2$' }
let(:after) { '^foo1$' }
let(:resource) {
Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'inserted = line',
:after => after,
:match => match,
}
)
}
end
before :each do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2\nfoo = baz")
end
end
describe 'inserts at match' do
include_context 'resource_create'
it {
provider.create
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
}
end
describe 'inserts a new line after when no match' do
include_context 'resource_create' do
let(:match) { '^nevergoingtomatch$' }
end
it {
provider.create
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
}
end
describe 'append to end of file if no match for both after and match' do
include_context 'resource_create' do
let(:match) { '^nevergoingtomatch$' }
let(:after) { '^stillneverafter' }
end
it {
provider.create
expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
}
end
end
context 'with one line matching the after expression' do
before :each do
File.open(@tmpfile, 'w') do |fh|
Expand Down Expand Up @@ -194,7 +241,12 @@
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
{:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' }
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo',
:ensure => 'absent',
}
)
@provider = provider_class.new(@resource)
end
Expand Down

0 comments on commit 0af0d7e

Please sign in to comment.