Skip to content
Closed
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
18 changes: 17 additions & 1 deletion manifests/fragment.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# @summary
# Manages a fragment of text to be compiled into a file.
#
# @param comment
# An optional comment to prepend to the fragment content. Each line is prefixed with '# '.
# Only supported with the content parameter, not with source.
#
# @param content
# Supplies the content of the fragment. Note: You must supply either a content parameter or a source parameter.
# Allows a String or a Deferred function which returns a String.
Expand All @@ -21,6 +25,7 @@
Optional[Variant[Sensitive[String], String, Deferred]] $content = undef,
Optional[Variant[String, Array]] $source = undef,
Variant[String, Integer] $order = '10',
Optional[String] $comment = undef,
) {
$resource = 'Concat::Fragment'

Expand All @@ -34,13 +39,24 @@
fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.")
}

if $comment != undef and $source != undef {
fail("${resource}['${title}']: Can't use 'comment' with 'source', use 'content' instead.")
}

if $comment != undef and $content != undef {
$_comment_lines = $comment.split('\n').map |$line| { "# ${line}" }.join("\n")
$_content = "${_comment_lines}\n${content}"
} else {
$_content = $content
}

$safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM')

concat_fragment { $name:
target => $target,
tag => $safe_target_name,
order => $order,
content => $content,
content => $_content,
source => $source,
}
}
61 changes: 61 additions & 0 deletions spec/defines/concat_fragment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,67 @@
end
# order =>

context 'when comment =>' do
context 'with content' do
let(:title) { 'motd_header' }
let(:params) do
{
target: '/etc/motd',
content: 'hello world',
comment: 'This is a header fragment'
}
end

it do
expect(subject).to contain_concat_fragment('motd_header').with(
content: "# This is a header fragment\nhello world",
)
end
end

context 'with multiline comment' do
let(:title) { 'motd_header' }
let(:params) do
{
target: '/etc/motd',
content: 'hello world',
comment: "line one\nline two"
}
end

it do
expect(subject).to contain_concat_fragment('motd_header').with(
content: "# line one\n# line two\nhello world",
)
end
end

context 'with source' do
let(:title) { 'motd_header' }
let(:params) do
{
target: '/etc/motd',
source: '/foo/bar',
comment: 'This should fail'
}
end

it 'fails' do
expect { catalogue }.to raise_error(Puppet::Error, %r{Can't use 'comment' with 'source'})
end
end

context 'when false' do
let(:title) { 'motd_header' }
let(:params) { { comment: false, target: '/etc/motd', content: 'hello' } }

it 'fails' do
expect { catalogue }.to raise_error(Puppet::Error, %r{parameter 'comment' expects a .*String.*})
end
end
end
# comment =>

context 'with more than one content source' do
context 'with source and content' do
let(:title) { 'motd_header' }
Expand Down
Loading