diff --git a/manifests/fragment.pp b/manifests/fragment.pp index c73ec0ce..fa26badc 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -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. @@ -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' @@ -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, } } diff --git a/spec/defines/concat_fragment_spec.rb b/spec/defines/concat_fragment_spec.rb index a020a299..223912fc 100644 --- a/spec/defines/concat_fragment_spec.rb +++ b/spec/defines/concat_fragment_spec.rb @@ -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' }