Skip to content

Commit

Permalink
(PUP-11597) A working, but very likely not ideal fix for PUP-11597
Browse files Browse the repository at this point in the history
  • Loading branch information
natemccurdy committed Jul 26, 2022
1 parent dfe10b2 commit ac6d139
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 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
21 changes: 15 additions & 6 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,9 +184,7 @@ 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)
Expand All @@ -199,8 +197,7 @@ 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'))]
# 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)
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 be(-1)
end

end

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

0 comments on commit ac6d139

Please sign in to comment.