Skip to content

Commit

Permalink
(PUP-11597) Generate types when any module libs are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
natemccurdy committed Jul 26, 2022
1 parent dfe10b2 commit 10043c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
19 changes: 18 additions & 1 deletion lib/puppet/generate/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,24 @@ def format=(format)
# @return [Boolean] Returns true if the output is up-to-date or false if not.
def up_to_date?(outputdir)
f = effective_output_path(outputdir)
Puppet::FileSystem::exist?(f) && (Puppet::FileSystem::stat(@path) <=> Puppet::FileSystem::stat(f)) <= 0
# Check the fast-path scenarios first.
unless Puppet::FileSystem::exist?(f)
Puppet.debug("#{f} does not exist.")
return false
end
unless (Puppet::FileSystem::stat(@path) <=> Puppet::FileSystem::stat(f)) <= 0
Puppet.debug("#{@path} is newer than #{f}")
return false
end
# Check for updates to any module lib files.
module_lib_files = Dir.glob(@base + "/lib/**/*.rb")
module_lib_files.each do |lib|
unless (Puppet::FileSystem::stat(lib) <=> Puppet::FileSystem::stat(f)) <= 0
Puppet.debug("#{lib} is newer than #{f}")
return false
end
end
return true
end

# Gets the filename of the output file.
Expand Down
27 changes: 18 additions & 9 deletions spec/unit/face/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Puppet
} }
},
},
'm2' => {
'm2' => {
'lib' => { 'puppet' => { 'type' => {
'test2.rb' => <<-EOF
module Puppet
Expand Down Expand Up @@ -184,26 +184,23 @@ module Puppet
# create them (first run)
genface.types
stats_before = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
# fake change in input test1 - sorry about the sleep (which there was a better way to change the modtime
sleep(1)
Puppet::FileSystem.touch(File.join(m1, 'lib', 'puppet', 'type', 'test1.rb'))
Puppet::FileSystem.touch(File.join(m1, 'lib', 'puppet', 'type', 'test1.rb'), :mtime => Time.now + 10)
# generate again
genface.types
# assert that test1 was overwritten (later) but not test2 (same time)
stats_after = [Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp')), Puppet::FileSystem.stat(File.join(outputdir, 'test2.pp'))]
expect(stats_before[1] <=> stats_after[1]).to be(0)
expect(stats_before[0] <=> stats_after[0]).to be(-1)
expect(stats_before[1] <=> stats_after[1]).to eq(0)
expect(stats_before[0] <=> stats_after[0]).to eq(-1)
end

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

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

it 'overwrites if ruby files in lib/puppet_x/ are updated' do
# create them (first run)
puppet_x_lib = File.join(m1, 'lib', 'puppet_x', 'foo', 'library.rb')
Puppet::FileSystem.mkpath(puppet_x_lib)
genface.types
stat_before = Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp'))
Puppet::FileSystem.touch(puppet_x_lib, :mtime => Time.now + 10)
genface.types
stat_after = Puppet::FileSystem.stat(File.join(outputdir, 'test1.pp'))
expect(stat_before <=> stat_after).to eq(-1)
end

end

context "in an environment with a faulty type" do
Expand Down

0 comments on commit 10043c0

Please sign in to comment.