Skip to content

Commit 78246ca

Browse files
committed
(PUP-11314) Guard on_initialize_and_write hooks
Puppet's test helper calls `initialize_app_defaults` before each test, which calls all hooks of type `on_initialize_and_write`[1]. So we were requiring two files unnecessarily about 26k times, which is really slow on Windows. Also we were appending 2 paths to Facter's search path for each test in an unbounded way. Note vardir is different for each test, so it's not possible to only add paths we haven't seen before. Note puppet.rb already required `puppet/node` outside the hook, so this just adds a require for `puppet/node/facts`. For loglevel, made the setter idempotent if the level is unchanged. Also avoid scanning the array of log levels twice if an invalid level is given. [1] https://github.com/puppetlabs/puppet/blob/7.11.0/lib/puppet/settings.rb#L390
1 parent 613a525 commit 78246ca

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/puppet.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,4 @@ def self.runtime
364364
require 'puppet/file_bucket/file'
365365
require 'puppet/plugins/configuration'
366366
require 'puppet/pal/pal_api'
367+
require 'puppet/node/facts'

lib/puppet/defaults.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,9 @@ def self.initialize_default_settings!(settings)
20502050
:call_hook => :on_initialize_and_write, # Call our hook with the default value, so we always get the value added to facter.
20512051
:hook => proc do |value|
20522052
paths = value.split(File::PATH_SEPARATOR)
2053-
Puppet.runtime[:facter].search(*paths)
2053+
facter = Puppet.runtime[:facter]
2054+
facter.reset
2055+
facter.search(*paths)
20542056
end
20552057
}
20562058
)
@@ -2191,8 +2193,6 @@ def self.initialize_default_settings!(settings)
21912193
# Call our hook with the default value, so we always get the libdir set.
21922194
:call_hook => :on_initialize_and_write,
21932195
:hook => proc do |value|
2194-
require 'puppet/node'
2195-
require 'puppet/node/facts'
21962196
if value
21972197
Puppet::Resource::Catalog.indirection.set_global_setting(:cache_class, :store_configs)
21982198
settings.override_default(:catalog_cache_terminus, :store_configs)

lib/puppet/util/log.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ def Log.level
105105
def Log.level=(level)
106106
level = level.intern unless level.is_a?(Symbol)
107107

108-
raise Puppet::DevError, _("Invalid loglevel %{level}") % { level: level } unless @levels.include?(level)
108+
# loglevel is a 0-based index
109+
loglevel = @levels.index(level)
110+
raise Puppet::DevError, _("Invalid loglevel %{level}") % { level: level } unless loglevel
109111

110-
@loglevel = @levels.index(level)
112+
return if @loglevel == loglevel
113+
114+
# loglevel changed
115+
@loglevel = loglevel
111116

112117
# Enable or disable Facter debugging
113118
Puppet.runtime[:facter].debugging(level == :debug)

0 commit comments

Comments
 (0)