(maint) Lazy-init host and token_file in PuppetlabsInfluxdb#129
Open
actowery wants to merge 2 commits into
Open
(maint) Lazy-init host and token_file in PuppetlabsInfluxdb#129actowery wants to merge 2 commits into
actowery wants to merge 2 commits into
Conversation
Defer the Facter.value(:networking) and Facter.value('identity') calls
that set default host and token_file until first read, rather than
running them at module-load time.
Loading lib/puppet_x/puppetlabs/influxdb/influxdb.rb previously raised
NoMethodError under rspec when Facter.value(:networking) returned nil,
which prevented spec/unit/puppet/provider/**_spec.rb from loading at
all. Lazy readers compute the default on first access, by which time
Facter is populated in both agent and test contexts. Behavior on a
real agent run is unchanged.
After this change: spec/unit provider specs load and pass (52 examples,
0 failures); manifest specs unchanged (83 examples, 0 failures, 95%
resource coverage).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Wrap the equality check in parens so the cop accepts the ternary, and cache the user lookup in a local to avoid calling Facter twice. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR prevents PuppetX::Puppetlabs::PuppetlabsInfluxdb from querying Facter at file load time by lazily computing the default host and token_file on first access, which unblocks unit specs (and any other contexts where facts aren’t populated yet when the library is required).
Changes:
- Replace class-level
attr_accessorreaders forhost/token_filewith lazy reader methods and keep writer methods for overrides. - Remove module-load-time initialization of
self.hostandself.token_filethat previously triggered Facter too early.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Facter.value(:networking)['fqdn']andFacter.value('identity')['user'](which set the defaulthostandtoken_filefor the providers) from module-load time to first read.lib/puppet_x/puppetlabs/influxdb/influxdb.rbraisesNoMethodError: undefined method '[]' for nilwhenever Facter hasn't been populated yet — which is exactly the case under rspec. That blocked every spec underspec/unit/puppet/provider/**from loading at all.self.host,self.token_file) compute the default on first access. By that point Facter is populated in both the agent runtime and test contexts, so production behavior is identical.Test plan
bundle exec rspec spec/classes spec/defines spec/functions spec/hosts— 83 examples, 0 failures, 95% rspec-puppet resource coverage (unchanged from before).bundle exec rspec spec/unit— 52 examples, 0 failures. Previously these specs would not even load due to the load-time Facter call.🤖 Generated with Claude Code