Skip to content

Commit bf25f83

Browse files
(maint) Clarify docs and add new tests
Based on a Stack Overflow question, it was noted that the documentation for `file_line` isn't complete and the underlying behaviour somewhat confusing. https://stackoverflow.com/questions/46468922/how-to-change-the-contents-of-a-file-by-using-puppet/46473458 In this patch I add additional unit tests and better examples and complete documentation.
1 parent 2b2b387 commit bf25f83

File tree

4 files changed

+150
-14
lines changed

4 files changed

+150
-14
lines changed

README.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,55 @@ In the example above, `match` looks for a line beginning with 'export' followed
117117

118118
Match Example:
119119

120-
file_line { 'bashrc_proxy':
121-
ensure => present,
122-
path => '/etc/bashrc',
123-
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
124-
match => '^export\ HTTP_PROXY\=',
125-
append_on_no_match => false,
126-
}
120+
```puppet
121+
file_line { 'bashrc_proxy':
122+
ensure => present,
123+
path => '/etc/bashrc',
124+
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
125+
match => '^export\ HTTP_PROXY\=',
126+
append_on_no_match => false,
127+
}
128+
```
127129

128130
In this code example, `match` looks for a line beginning with export followed by HTTP_PROXY and replaces it with the value in line. If a match is not found, then no changes are made to the file.
129131

130-
Match Example with `ensure => absent`:
132+
Examples With `ensure => absent`:
133+
134+
This type has two behaviors when `ensure => absent` is set.
135+
136+
One possibility is to set `match => ...` and `match_for_absence => true`,
137+
as in the following example:
131138

132139
```puppet
133140
file_line { 'bashrc_proxy':
134141
ensure => absent,
135142
path => '/etc/bashrc',
136-
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
137143
match => '^export\ HTTP_PROXY\=',
138144
match_for_absence => true,
139145
}
140146
```
141147

142-
In the example above, `match` looks for a line beginning with 'export' followed by 'HTTP_PROXY' and deletes it. If multiple lines match, an error is raised, unless the `multiple => true` parameter is set.
148+
In this code example match will look for a line beginning with export
149+
followed by HTTP_PROXY and delete it. If multiple lines match, an
150+
error will be raised unless the `multiple => true` parameter is set.
151+
152+
Note that the `line => ...` parameter would be accepted BUT IGNORED in
153+
the above example.
154+
155+
The second way of using `ensure => absent` is to specify a `line => ...`,
156+
and no match:
157+
158+
```puppet
159+
file_line { 'bashrc_proxy':
160+
ensure => absent,
161+
path => '/etc/bashrc',
162+
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
163+
}
164+
```
165+
166+
Note that when ensuring lines are absent this way, the default behavior
167+
this time is to always remove all lines matching, and this behavior
168+
can't be disabled.
143169

144170
Encoding example:
145171

lib/puppet/type/file_line.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@
3434
In this code example match will look for a line beginning with export
3535
followed by HTTP_PROXY and replace it with the value in line.
3636
37-
Match Example With `ensure => absent`:
37+
Examples With `ensure => absent`:
38+
39+
This type has two behaviors when `ensure => absent` is set.
40+
41+
One possibility is to set `match => ...` and `match_for_absence => true`,
42+
as in the following example:
3843
3944
file_line { 'bashrc_proxy':
4045
ensure => absent,
4146
path => '/etc/bashrc',
42-
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
4347
match => '^export\ HTTP_PROXY\=',
4448
match_for_absence => true,
4549
}
@@ -48,6 +52,22 @@
4852
followed by HTTP_PROXY and delete it. If multiple lines match, an
4953
error will be raised unless the `multiple => true` parameter is set.
5054
55+
Note that the `line => ...` parameter would be accepted BUT IGNORED in
56+
the above example.
57+
58+
The second way of using `ensure => absent` is to specify a `line => ...`,
59+
and no match:
60+
61+
file_line { 'bashrc_proxy':
62+
ensure => absent,
63+
path => '/etc/bashrc',
64+
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
65+
}
66+
67+
Note that when ensuring lines are absent this way, the default behavior
68+
this time is to always remove all lines matching, and this behavior
69+
can't be disabled.
70+
5171
Encoding example:
5272
5373
file_line { "XScreenSaver":

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

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@
337337
end
338338
end
339339

340-
context "when removing" do
340+
context "when removing with a line" do
341341
before :each do
342342
# TODO: these should be ported over to use the PuppetLabs spec_helper
343343
# file fixtures once the following pull request has been merged:
@@ -378,6 +378,23 @@
378378
@provider.destroy
379379
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
380380
end
381+
382+
it 'example in the docs' do
383+
@resource = Puppet::Type::File_line.new(
384+
{
385+
:name => 'bashrc_proxy',
386+
:ensure => 'absent',
387+
:path => @tmpfile,
388+
:line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
389+
}
390+
)
391+
@provider = provider_class.new(@resource)
392+
File.open(@tmpfile, 'w') do |fh|
393+
fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n")
394+
end
395+
@provider.destroy
396+
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
397+
end
381398
end
382399

383400
context "when removing with a match" do
@@ -416,6 +433,41 @@
416433
expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
417434
end
418435

436+
it 'the line parameter is actually not used at all but is silently ignored if here' do
437+
@resource = Puppet::Type::File_line.new(
438+
{
439+
:name => 'foo',
440+
:path => @tmpfile,
441+
:line => 'supercalifragilisticexpialidocious',
442+
:ensure => 'absent',
443+
:match => 'o$',
444+
:match_for_absence => true,
445+
}
446+
)
447+
File.open(@tmpfile, 'w') do |fh|
448+
fh.write("foo1\nfoo\nfoo2")
449+
end
450+
@provider.destroy
451+
expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
452+
end
453+
454+
it 'and may not be here and does not need to be here' do
455+
@resource = Puppet::Type::File_line.new(
456+
{
457+
:name => 'foo',
458+
:path => @tmpfile,
459+
:ensure => 'absent',
460+
:match => 'o$',
461+
:match_for_absence => true,
462+
}
463+
)
464+
File.open(@tmpfile, 'w') do |fh|
465+
fh.write("foo1\nfoo\nfoo2")
466+
end
467+
@provider.destroy
468+
expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
469+
end
470+
419471
it 'should raise an error if more than one line matches' do
420472
File.open(@tmpfile, 'w') do |fh|
421473
fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
@@ -480,6 +532,41 @@
480532
expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
481533
end
482534

483-
end
535+
it 'example in the docs' do
536+
@resource = Puppet::Type::File_line.new(
537+
{
538+
:name => 'bashrc_proxy',
539+
:ensure => 'absent',
540+
:path => @tmpfile,
541+
:line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
542+
:match => '^export\ HTTP_PROXY\=',
543+
:match_for_absence => true,
544+
}
545+
)
546+
@provider = provider_class.new(@resource)
547+
File.open(@tmpfile, 'w') do |fh|
548+
fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
549+
end
550+
@provider.destroy
551+
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
552+
end
484553

554+
it 'example in the docs showing line is redundant' do
555+
@resource = Puppet::Type::File_line.new(
556+
{
557+
:name => 'bashrc_proxy',
558+
:ensure => 'absent',
559+
:path => @tmpfile,
560+
:match => '^export\ HTTP_PROXY\=',
561+
:match_for_absence => true,
562+
}
563+
)
564+
@provider = provider_class.new(@resource)
565+
File.open(@tmpfile, 'w') do |fh|
566+
fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
567+
end
568+
@provider.destroy
569+
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
570+
end
571+
end
485572
end

spec/unit/puppet/type/file_line_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
it 'should not require that a line is specified when matching for absence' do
7979
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
8080
end
81+
it 'although if a line is specified anyway when matching for absence it still works and the line is silently ignored' do
82+
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :line => 'i_am_irrelevant', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
83+
end
8184
it 'should require that a file is specified' do
8285
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /path is a required attribute/)
8386
end

0 commit comments

Comments
 (0)