Skip to content

Commit 4dd6a6d

Browse files
authored
Merge pull request #1253 from binford2k/deferred_epp
deferrable epp function simplifying deferred templates
2 parents 2f3d3f1 + 364589e commit 4dd6a6d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

functions/deferrable_epp.pp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This function returns either a rendered template or a deferred function to render at runtime.
2+
# If any of the values in the variables hash are deferred, then the template will be deferred.
3+
#
4+
# Note: this function requires all parameters to be explicitly passed in. It cannot expect to
5+
# use facts, class variables, and other variables in scope. This is because when deferred, we
6+
# have to explicitly pass the entire scope to the client.
7+
#
8+
function stdlib::deferrable_epp(String $template, Hash $variables) >> Variant[String, Deferred] {
9+
if $variables.any |$key, $value| { $value.is_a(Deferred) } {
10+
Deferred(
11+
'inline_epp',
12+
[find_template($template).file, $variables],
13+
)
14+
}
15+
else {
16+
epp($template, $variables)
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::deferrable_epp' do
4+
context 'call epp on non-deferred input' do
5+
let(:pre_condition) do
6+
'function epp($str, $data) { return "rendered"}'
7+
end
8+
9+
it {
10+
is_expected.to run.with_params('mymod/template.epp', { 'foo' => 'bar' }).and_return('rendered')
11+
}
12+
end
13+
14+
context 'defers rendering with deferred input' do
15+
let(:pre_condition) do
16+
<<~END
17+
function epp($str, $data) { fail("should not have invoked epp()") }
18+
function find_template($str) { return "path" }
19+
function file($path) { return "foo: <%= foo %>" }
20+
END
21+
end
22+
23+
it {
24+
foo = Puppet::Pops::Types::TypeFactory.deferred.create('join', [1, 2, 3])
25+
# This kind_of matcher requires https://github.com/puppetlabs/rspec-puppet/pull/24
26+
is_expected.to run.with_params('mymod/template.epp', { 'foo' => foo }) # .and_return(kind_of Puppet::Pops::Types::PuppetObject)
27+
}
28+
end
29+
end

0 commit comments

Comments
 (0)