Skip to content

File_line checks provided after param if no match is found #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ def lines

def handle_create_with_match()
regex = resource[:match] ? Regexp.new(resource[:match]) : nil
regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil
match_count = count_matches(regex)

if match_count > 1 && resource[:multiple].to_s != 'true'
raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
end

File.open(resource[:path], 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? resource[:line] : l)
if (match_count == 0 and regex_after)
if regex_after.match(l)
fh.puts(resource[:line])
match_count += 1 #Increment match_count to indicate that the new line has been inserted.
end
end
end

if (match_count == 0)
Expand Down Expand Up @@ -78,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*=.*$',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

}
)
@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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is nitpicky, but the misalignment hurts my eyes. :(

}
)
}
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