Skip to content

Commit

Permalink
Merge pull request #190 from puppetlabs/gh-189-fix_top_scope_facts_fix
Browse files Browse the repository at this point in the history
(GH-189) - invalid fact correction for top scope structured fact
  • Loading branch information
bastelfreak authored Feb 9, 2024
2 parents 6d5b00e + e4863b0 commit a59e682
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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']" }

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

0 comments on commit a59e682

Please sign in to comment.