Skip to content

Commit

Permalink
(GH-189) - invalid fact correction for top scope structured fact
Browse files Browse the repository at this point in the history
Prior to this commit, if you had a top scope structured fact like this
in your manifest `$::my_structured_fact['foo']['test']`, lint would
return an invalid correction when passed the fix flag.

Now, we have altered the top_scope_facts plugin, to first check if it is
a structured fact, and if one is found using regex, apply different
logic to fix.
  • Loading branch information
jordanbreen28 committed Feb 6, 2024
1 parent 6d5b00e commit 2521343
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)
top_scope_variable = problem[:token].value.sub(%r{\[.*}, '')
facts_hash = problem[:token].value.scan(%r{\[.+}).first
problem[:token].value = "facts['%s']%s" % [top_scope_variable, facts_hash]

Check failure on line 52 in lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/FormatStringToken: Prefer template tokens (like `%{foo}`) over unannotated tokens (like `%s`).

Check failure on line 52 in lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/FormatStringToken: Prefer template tokens (like `%{foo}`) over unannotated tokens (like `%s`).

Check failure on line 52 in lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/FormatStringToken: Prefer template tokens (like `%{foo}`) over unannotated tokens (like `%s`).

Check failure on line 52 in lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/FormatStringToken: Prefer template tokens (like `%{foo}`) over unannotated tokens (like `%s`).
else
problem[:token].value = "facts['%s']" % [top_scope_variable]
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 2521343

Please sign in to comment.