Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not crash when referring to a fact key without quoting it #119

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,9 @@ def check
tokens.select { |x| LEGACY_FACTS_VAR_TYPES.include?(x.type) }.each do |token|
fact_name = ''

# This matches legacy facts defined in the fact hash that use the top scope
# fact assignment.
if token.value.start_with?('::facts[')
fact_name = token.value.match(%r{::facts\['(.*)'\]})[1]

# This matches legacy facts defined in the fact hash.
elsif token.value.start_with?("facts['")
fact_name = token.value.match(%r{facts\['(.*)'\]})[1]
if (match = (token.value.match(%r{(::)?facts\['(.*)'\]}) || token.value.match(%r{(::)?facts\[(.*)\]})))
fact_name = match[2]

# This matches using legacy facts in a the new structured fact. For
# example this would match 'uuid' in $facts['uuid'] so it can be converted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@
expect(problems).to have(1).problem
end
end

context 'top scoped fact variable using unquoted legacy facts hash variable in interpolation' do
let(:code) { '$::facts[osfamily]' }

it 'detects a single problem' do
expect(problems).to have(1).problem
end
end
end

context 'with fix enabled' do
Expand Down