Skip to content

Commit 118e802

Browse files
committed
readd ensure_newline param and tests for backwards compatibility
1 parent b880a99 commit 118e802

4 files changed

Lines changed: 138 additions & 17 deletions

File tree

lib/puppet/type/concat_file.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
example:
1111
Concat_fragment <<| tag == 'unique_tag' |>>
1212
13-
concat_file { '/tmp/file:
14-
tag => 'unique_tag', # Mandatory
15-
path => '/tmp/file', # Optional. If given it overrides the resource name
16-
owner => 'root', # Optional. Default to undef
17-
group => 'root', # Optional. Default to undef
18-
mode => '0644' # Optional. Default to undef
19-
order => 'numeric' # Optional, Default to 'numeric'
13+
concat_file { '/tmp/file':
14+
tag => 'unique_tag', # Mandatory
15+
path => '/tmp/file', # Optional. If given it overrides the resource name
16+
owner => 'root', # Optional. Default to undef
17+
group => 'root', # Optional. Default to undef
18+
mode => '0644' # Optional. Default to undef
19+
order => 'numeric' # Optional, Default to 'numeric'
20+
ensure_newline => false # Optional, Defaults to false
2021
}
2122
"
2223
ensurable do
@@ -75,6 +76,11 @@ def exists?
7576
desc "Validates file."
7677
end
7778

79+
newparam(:ensure_newline) do
80+
desc "Whether to ensure there is a newline after each fragment."
81+
defaultto false
82+
end
83+
7884
autorequire(:concat_fragment) do
7985
catalog.resources.collect do |r|
8086
if r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag]
@@ -134,6 +140,11 @@ def fragment_content(r)
134140
tmp = Puppet::FileServing::Content.indirection.find(@source, :environment => catalog.environment)
135141
fragment_content = tmp.content unless tmp.nil?
136142
end
143+
144+
if self[:ensure_newline]
145+
fragment_content<<"\n"
146+
end
147+
137148
fragment_content
138149
end
139150

manifests/init.pp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Who will own the file
1717
# [*mode*]
1818
# The mode of the final file
19-
# [*warn_header*]
19+
# [*warn*]
2020
# Adds a normal shell style comment top of the file indicating that it is
2121
# built by puppet
2222
# [*backup*]
@@ -27,6 +27,13 @@
2727
# [*order*]
2828
# Select whether to order associated fragments by 'alpha' or 'numeric'.
2929
# Defaults to 'alpha'.
30+
# [*ensure_newline*]
31+
# Specifies whether to ensure there's a new line at the end of each fragment.
32+
# Valid options: 'true' and 'false'. Default value: 'false'.
33+
# [*validate_cmd*]
34+
# Specifies a validation command to apply to the destination file.
35+
# Requires Puppet version 3.5 or newer. Valid options: a string to be passed
36+
# to a file resource. Default value: undefined.
3037
#
3138

3239
define concat(
@@ -40,6 +47,7 @@
4047
$backup = 'puppet',
4148
$replace = true,
4249
$order = 'alpha',
50+
$ensure_newline = false,
4351
$validate_cmd = undef,
4452
) {
4553
validate_re($ensure, '^present$|^absent$')
@@ -55,6 +63,8 @@
5563
}
5664
validate_bool($replace)
5765
validate_re($order, '^alpha$|^numeric$')
66+
validate_bool($ensure_newline)
67+
5868
if $validate_cmd and ! is_string($validate_cmd) {
5969
fail('$validate_cmd must be a string')
6070
}
@@ -83,15 +93,16 @@
8393

8494
if $ensure == 'present' {
8595
concat_file { $name:
86-
tag => $safe_name,
87-
path => $path,
88-
owner => $owner,
89-
group => $group,
90-
mode => $mode,
91-
replace => $replace,
92-
backup => $backup,
93-
order => $order,
94-
validate_cmd => $validate_cmd,
96+
tag => $safe_name,
97+
path => $path,
98+
owner => $owner,
99+
group => $group,
100+
mode => $mode,
101+
replace => $replace,
102+
backup => $backup,
103+
order => $order,
104+
ensure_newline => $ensure_newline,
105+
validate_cmd => $validate_cmd,
95106
}
96107

97108
if $_append_header {

spec/acceptance/newline_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'concat ensure_newline parameter' do
4+
basedir = default.tmpdir('concat')
5+
context '=> false' do
6+
before(:all) do
7+
pp = <<-EOS
8+
file { '#{basedir}':
9+
ensure => directory
10+
}
11+
EOS
12+
13+
apply_manifest(pp)
14+
end
15+
pp = <<-EOS
16+
concat { '#{basedir}/file':
17+
ensure_newline => false,
18+
}
19+
concat::fragment { '1':
20+
target => '#{basedir}/file',
21+
content => '1',
22+
}
23+
concat::fragment { '2':
24+
target => '#{basedir}/file',
25+
content => '2',
26+
}
27+
EOS
28+
29+
it 'applies the manifest twice with no stderr' do
30+
apply_manifest(pp, :catch_failures => true)
31+
apply_manifest(pp, :catch_changes => true)
32+
end
33+
34+
describe file("#{basedir}/file") do
35+
it { should be_file }
36+
its(:content) { should match '12' }
37+
end
38+
end
39+
40+
context '=> true' do
41+
pp = <<-EOS
42+
concat { '#{basedir}/file':
43+
ensure_newline => true,
44+
}
45+
concat::fragment { '1':
46+
target => '#{basedir}/file',
47+
content => '1',
48+
}
49+
concat::fragment { '2':
50+
target => '#{basedir}/file',
51+
content => '2',
52+
}
53+
EOS
54+
55+
it 'applies the manifest twice with no stderr' do
56+
apply_manifest(pp, :catch_failures => true)
57+
apply_manifest(pp, :catch_changes => true)
58+
end
59+
60+
describe file("#{basedir}/file") do
61+
it { should be_file }
62+
its(:content) {
63+
should match /1\n2\n/
64+
}
65+
end
66+
end
67+
end

spec/unit/defines/concat_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,36 @@
248248
end
249249
end
250250
end # order =>
251+
252+
context 'ensure_newline =>' do
253+
[true, false].each do |ensure_newline|
254+
context 'true' do
255+
it_behaves_like 'concat', '/etc/foo.bar', { :ensure_newline => ensure_newline}
256+
end
257+
end
258+
259+
context '123' do
260+
let(:title) { '/etc/foo.bar' }
261+
let(:params) {{ :ensure_newline => 123 }}
262+
it 'should fail' do
263+
expect { catalogue }.to raise_error(Puppet::Error, /is not a boolean/)
264+
end
265+
end
266+
end # ensure_newline =>
267+
268+
context 'validate_cmd =>' do
269+
context '/usr/bin/test -e %' do
270+
it_behaves_like 'concat', '/etc/foo.bar', { :validate_cmd => '/usr/bin/test -e %' }
271+
end
272+
273+
[ 1234, true ].each do |cmd|
274+
context cmd do
275+
let(:title) { '/etc/foo.bar' }
276+
let(:params) {{ :validate_cmd => cmd }}
277+
it 'should fail' do
278+
expect { catalogue }.to raise_error(Puppet::Error, /\$validate_cmd must be a string/)
279+
end
280+
end
281+
end
282+
end # validate_cmd =>
251283
end

0 commit comments

Comments
 (0)