File tree 2 files changed +21
-1
lines changed
lib/puppet-lint/plugins/top_scope_facts
spec/unit/puppet-lint/plugins/top_scope_facts
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,14 @@ def check
44
44
end
45
45
46
46
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
48
56
end
49
57
end
Original file line number Diff line number Diff line change 102
102
end
103
103
end
104
104
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
+
105
117
context 'top scope $::trusted hash' do
106
118
let ( :code ) { "$::trusted['certname']" }
107
119
You can’t perform that action at this time.
0 commit comments