Skip to content

Commit a316b92

Browse files
committed
(PUP-11937) Skip autorequire if exec's param is deferred and lazily evaluated
If an exec's 'command' parameter is deferred and lazily evaluated, then we can't autorequire the file that the command will execute. This is because we won't know what the command will be until the exec is evaluated. But that means it's already too late to add a relationship from the file resource to the exec resource. In this situation an explicit relationship must be added to ensure the file resource is evaluated before the exec. The same is true for the 'onlyif' and 'unless' exec parameters.
1 parent 1145d44 commit a316b92

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/puppet/type/exec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,17 @@ def check(value)
592592
cmd = self[:command]
593593
cmd = cmd[0] if cmd.is_a? Array
594594

595-
cmd.scan(file_regex) { |str|
596-
reqs << str
597-
}
595+
if cmd.is_a?(Puppet::Pops::Evaluator::DeferredValue)
596+
self.debug("The 'command' parameter is deferred and cannot be autorequired")
597+
else
598+
cmd.scan(file_regex) { |str|
599+
reqs << str
600+
}
598601

599-
cmd.scan(/^"([^"]+)"/) { |str|
600-
reqs << str
601-
}
602+
cmd.scan(/^"([^"]+)"/) { |str|
603+
reqs << str
604+
}
605+
end
602606

603607
[:onlyif, :unless].each { |param|
604608
tmp = self[param]
@@ -613,7 +617,11 @@ def check(value)
613617
# unqualified files, but, well, that's a bit more annoying
614618
# to do.
615619
line = line[0] if line.is_a? Array
616-
reqs += line.scan(file_regex)
620+
if line.is_a?(Puppet::Pops::Evaluator::DeferredValue)
621+
self.debug("The '#{param}' parameter is deferred and cannot be autorequired")
622+
else
623+
reqs += line.scan(file_regex)
624+
end
617625
end
618626
}
619627

spec/unit/type/exec_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ def exec_stub(options = {})
252252
expect(dependencies.collect(&:to_s)).to eq([Puppet::Relationship.new(tmp, execer).to_s])
253253
end
254254

255+
it "skips autorequire for deferred commands" do
256+
foo = make_absolute('/bin/foo')
257+
catalog = Puppet::Resource::Catalog.new
258+
tmp = Puppet::Type.type(:file).new(:name => foo)
259+
execer = Puppet::Type.type(:exec).new(:name => 'test array', :command => Puppet::Pops::Evaluator::DeferredValue.new(nil))
260+
261+
catalog.add_resource tmp
262+
catalog.add_resource execer
263+
dependencies = execer.autorequire(catalog)
264+
265+
expect(dependencies.collect(&:to_s)).to eq([])
266+
end
267+
255268
describe "when handling the path parameter" do
256269
expect = %w{one two three four}
257270
{ "an array" => expect,

0 commit comments

Comments
 (0)