Skip to content

Commit 573fbac

Browse files
committed
(PUP-11597) Generate types when any module libs are updated
1 parent dfe10b2 commit 573fbac

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

lib/puppet/generate/type.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,24 @@ def format=(format)
4646
# @return [Boolean] Returns true if the output is up-to-date or false if not.
4747
def up_to_date?(outputdir)
4848
f = effective_output_path(outputdir)
49-
Puppet::FileSystem::exist?(f) && (Puppet::FileSystem::stat(@path) <=> Puppet::FileSystem::stat(f)) <= 0
49+
# Check the fast-path scenarios first.
50+
unless Puppet::FileSystem::exist?(f)
51+
Puppet.debug("#{f} does not exist.")
52+
return false
53+
end
54+
unless (Puppet::FileSystem::stat(@path) <=> Puppet::FileSystem::stat(f)) <= 0
55+
Puppet.debug("#{@path} is newer than #{f}")
56+
return false
57+
end
58+
# Check for updates to any module lib files.
59+
module_lib_files = Dir.glob(File.join(@base, "lib", "**", "*.rb"))
60+
module_lib_files.each do |lib|
61+
unless (Puppet::FileSystem::stat(lib) <=> Puppet::FileSystem::stat(f)) <= 0
62+
Puppet.debug("#{lib} is newer than #{f}")
63+
return false
64+
end
65+
end
66+
return true
5067
end
5168

5269
# Gets the filename of the output file.

spec/unit/face/generate_spec.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module Puppet
6666
} }
6767
},
6868
},
69-
'm2' => {
69+
'm2' => {
7070
'lib' => { 'puppet' => { 'type' => {
7171
'test2.rb' => <<-EOF
7272
module Puppet
@@ -184,26 +184,23 @@ module Puppet
184184
# create them (first run)
185185
genface.types
186186
stats_before = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
187-
# fake change in input test1 - sorry about the sleep (which there was a better way to change the modtime
188-
sleep(1)
189-
Puppet::FileSystem.touch(File.join(m1, 'lib', 'puppet', 'type', 'test1.rb'))
187+
Puppet::FileSystem.touch(File.join(m1, 'lib', 'puppet', 'type', 'test1.rb'), :mtime => Time.now + 10)
190188
# generate again
191189
genface.types
192190
# assert that test1 was overwritten (later) but not test2 (same time)
193191
stats_after = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
194-
expect(stats_before[1] <=> stats_after[1]).to be(0)
195-
expect(stats_before[0] <=> stats_after[0]).to be(-1)
192+
expect(stats_before[1] <=> stats_after[1]).to eq(0)
193+
expect(stats_before[0] <=> stats_after[0]).to eq(-1)
196194
end
197195

198196
it 'overwrites all files when called with --force' do
199197
# create them (first run)
200198
genface.types
201199
stats_before = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
202-
# generate again
203-
sleep(1) # sorry, if there is no delay the stats will be the same
200+
Puppet::FileSystem.touch(File.join(outputdir, 'test2.pp'), :mtime => Time.now + 10)
204201
genface.types(:force => true)
205202
stats_after = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
206-
expect(stats_before <=> stats_after).to be(-1)
203+
expect(stats_before <=> stats_after).to eq(-1)
207204
end
208205

209206
it 'removes previously generated files from output when there is no input for it' do
@@ -220,6 +217,18 @@ module Puppet
220217
expect(stat_before <=> stats_after).to be(0)
221218
end
222219

220+
it 'overwrites all files if ruby files in lib/puppet_x/ are updated' do
221+
# create them (first run)
222+
puppet_x_lib = File.join(m1, 'lib', 'puppet_x', 'foo', 'library.rb')
223+
Puppet::FileSystem.mkpath(puppet_x_lib)
224+
genface.types
225+
stats_before = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
226+
Puppet::FileSystem.touch(puppet_x_lib, :mtime => Time.now + 10)
227+
genface.types
228+
stats_after = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
229+
expect(stats_before <=> stats_after).to eq(-1)
230+
end
231+
223232
end
224233

225234
context "in an environment with a faulty type" do

0 commit comments

Comments
 (0)