Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-189) - invalid fact correction for top scope structured fact #190

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def check
end

def fix(problem)
problem[:token].value = "facts['" + problem[:token].value.sub(%r{^::}, '') + "']"
problem[:token].value.sub!(%r{^::}, '')
# checks if the fact is a top level structured fact e.g. ::my_structured_fact['foo']['bar']
if %r{\[.*}.match?(problem[:token].value)
fact_name = problem[:token].value.sub(%r{\[.*}, '')
nested_facts = problem[:token].value.scan(%r{\[.*}).first
problem[:token].value = "facts['" + fact_name + "']" + nested_facts
else
problem[:token].value = "facts['" + problem[:token].value + "']"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@
end
end

context 'top scope structured fact not present on allowlist' do
let(:code) { "$::my_structured_fact['foo']['test']" }
bastelfreak marked this conversation as resolved.
Show resolved Hide resolved

it 'detects a problem' do
expect(problems).to contain_fixed('top scope fact instead of facts hash').on_line(1).in_column(1)
end

it 'fixes the problem' do
expect(manifest).to eq("$facts['my_structured_fact']['foo']['test']")
end
end

context 'top scope $::trusted hash' do
let(:code) { "$::trusted['certname']" }

Expand Down