Skip to content

Commit a59e682

Browse files
authored
Merge pull request #190 from puppetlabs/gh-189-fix_top_scope_facts_fix
(GH-189) - invalid fact correction for top scope structured fact
2 parents 6d5b00e + e4863b0 commit a59e682

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def check
4444
end
4545

4646
def fix(problem)
47-
problem[:token].value = "facts['" + problem[:token].value.sub(%r{^::}, '') + "']"
47+
problem[:token].value.sub!(%r{^::}, '')
48+
# checks if the fact is a top level structured fact e.g. ::my_structured_fact['foo']['bar']
49+
if %r{\[.*}.match?(problem[:token].value)
50+
fact_name = problem[:token].value.sub(%r{\[.*}, '')
51+
nested_facts = problem[:token].value.scan(%r{\[.*}).first
52+
problem[:token].value = "facts['" + fact_name + "']" + nested_facts
53+
else
54+
problem[:token].value = "facts['" + problem[:token].value + "']"
55+
end
4856
end
4957
end

spec/unit/puppet-lint/plugins/top_scope_facts/top_scope_facts_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@
102102
end
103103
end
104104

105+
context 'top scope structured fact not present on allowlist' do
106+
let(:code) { "$::my_structured_fact['foo']['test']" }
107+
108+
it 'detects a problem' do
109+
expect(problems).to contain_fixed('top scope fact instead of facts hash').on_line(1).in_column(1)
110+
end
111+
112+
it 'fixes the problem' do
113+
expect(manifest).to eq("$facts['my_structured_fact']['foo']['test']")
114+
end
115+
end
116+
105117
context 'top scope $::trusted hash' do
106118
let(:code) { "$::trusted['certname']" }
107119

0 commit comments

Comments
 (0)