Skip to content

(MODULES-5003) file_line fix all broken lines #788

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 1 commit into from
Jul 7, 2017
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
11 changes: 8 additions & 3 deletions lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
Puppet::Type.type(:file_line).provide(:ruby) do
def exists?
found = true
if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
true
found = true
else
lines.find do |line|
if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true'
line.chomp =~ Regexp.new(resource[:match])
found = line.chomp =~ Regexp.new(resource[:match])
else
line.chomp == resource[:line].chomp
found = line.chomp == resource[:line].chomp
end
if found == false then
break
end
end
end
found
end

def create
Expand Down
37 changes: 28 additions & 9 deletions spec/unit/puppet/provider/file_line/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
File.open(tmpfile, 'w') do |fh|
fh.write('foo1')
end
expect(provider.exists?).to be_nil
expect(provider.exists?).to eql (false)
end
it 'should append to an existing file when creating' do
provider.create
Expand Down Expand Up @@ -69,7 +69,7 @@
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2")
end
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql (false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
end
Expand Down Expand Up @@ -112,7 +112,7 @@
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql(false)
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
Expand All @@ -131,11 +131,30 @@
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
end

it 'should replace all lines that match, even when some lines are correct' do
@resource = Puppet::Type::File_line.new(
{
:name => 'neil',
:path => @tmpfile,
:line => "\thard\tcore\t0\n",
:match => '^[ \t]hard[ \t]+core[ \t]+.*',
:multiple => true,
}
)
@provider = provider_class.new(@resource)
File.open(@tmpfile, 'w') do |fh|
fh.write("\thard\tcore\t90\n\thard\tcore\t0\n")
end
expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
end

it 'should raise an error with invalid values' do
expect {
@resource = Puppet::Type::File_line.new(
Expand All @@ -154,23 +173,23 @@
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2")
end
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
end
it 'should add a new line if no lines match' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2")
end
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
end
it 'should do nothing if the exact line already exists' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo = bar\nfoo2")
end
expect(@provider.exists?).to be_truthy
expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
end
Expand Down Expand Up @@ -274,7 +293,7 @@
}
)
@provider = provider_class.new(@resource)
expect(@provider.exists?).to be_nil
expect(@provider.exists?).to eql (false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
end
Expand Down Expand Up @@ -367,7 +386,7 @@
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2")
end
expect(@provider.exists?).to be_truthy
expect(@provider.exists?).to be_nil
end

it 'should remove one line if it matches' do
Expand Down