From 10043c04f04a3573ad8944ac0f85a17771e17ce6 Mon Sep 17 00:00:00 2001 From: Nate McCurdy Date: Tue, 26 Jul 2022 13:37:39 -0700 Subject: [PATCH] (PUP-11597) Generate types when any module libs are updated --- lib/puppet/generate/type.rb | 19 ++++++++++++++++++- spec/unit/face/generate_spec.rb | 27 ++++++++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/puppet/generate/type.rb b/lib/puppet/generate/type.rb index 746fe06a065..3cc5f0b4f83 100644 --- a/lib/puppet/generate/type.rb +++ b/lib/puppet/generate/type.rb @@ -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. diff --git a/spec/unit/face/generate_spec.rb b/spec/unit/face/generate_spec.rb index 95b78e459ba..ecfc313a860 100644 --- a/spec/unit/face/generate_spec.rb +++ b/spec/unit/face/generate_spec.rb @@ -66,7 +66,7 @@ module Puppet } } }, }, - 'm2' => { + 'm2' => { 'lib' => { 'puppet' => { 'type' => { 'test2.rb' => <<-EOF module Puppet @@ -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 @@ -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