File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
lib/puppet-lint/plugins/top_scope_facts
spec/unit/puppet-lint/plugins/top_scope_facts Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff 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
4957end
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments