Skip to content

Commit

Permalink
Merge pull request voxpupuli#44 from puppet-community/fix/empty
Browse files Browse the repository at this point in the history
fix(default_content) don't call template() on empty template_name
  • Loading branch information
rnelson0 committed Dec 31, 2015
2 parents 50d7632 + d5475d1 commit 7752635
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Takes an optional content and an optional template name and returns the contents
*Examples:*

```puppet
$config_file_content = default_content($config_file_string, $config_file_template)
$config_file_content = default_content($file_content, $template_location)
file { '/tmp/x':
ensure => 'file',
content => $config_file_content,
Expand Down
44 changes: 22 additions & 22 deletions lib/puppet/parser/functions/default_content.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module Puppet::Parser::Functions
Puppet::Parser::Functions.newfunction(:default_content,
:type => :rvalue,
:doc => <<-'ENDOFDOC'
Takes an optional content and an optional template name and returns the
contents of a file.
Puppet::Parser::Functions.newfunction(:default_content,
:type => :rvalue,
:doc => <<-'ENDOFDOC'
Takes an optional content and an optional template name and returns the
contents of a file.
Examples:
Examples:
$config_file_content = default_content($config_file_string, $config_file_template)
file { '/tmp/x':
ensure => 'file',
content => $config_file_content,
}
ENDOFDOC
) do |args|
content = args[0]
template_name = args[1]
$config_file_content = default_content($file_content, $template_location)
file { '/tmp/x':
ensure => 'file',
content => $config_file_content,
}
ENDOFDOC
) do |args|
content = args[0]
template_name = args[1]

# Load template function from puppet
Puppet::Parser::Functions.function('template')
# Load template function from puppet
Puppet::Parser::Functions.function('template')

return content if content != ''
return function_template([template_name]) if template_name
emptyish = ->(x) { return (x.nil? || x.empty? || x == :undef) }

return nil
end
return content unless emptyish[content]
return function_template([template_name]) unless emptyish[template_name]

return :undef
end
25 changes: 25 additions & 0 deletions spec/functions/default_content_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe 'default_content' do
describe 'signature validation' do
it 'should exist' do
is_expected.not_to be_nil
end
end

context 'should return the correct string for' do
it 'a string' do
is_expected.to run.with_params('a string', :undef).and_return 'a string'
end
it 'an empty string and a template' do
tw = 'a template string'
Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw)
tw.stubs(:file=).with('/tmp/foo.erb')
tw.stubs(:result).returns('a template string')
is_expected.to run.with_params(:undef, '/tmp/foo.erb').and_return 'a template string'
end
it 'an empty string and an empty template' do
is_expected.to run.with_params(:undef, :undef).and_return :undef
end
end
end

0 comments on commit 7752635

Please sign in to comment.