puppet-lint-legacy_facts-check 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 547155b0b69aa2fe3a40ac14b997b483920dd7f5b0edd4eef7f9b8e7899d550c
4
- data.tar.gz: 5eba4749445125868c94ee5b191d22f47da29cd0d5750988c356635860245768
3
+ metadata.gz: a85f4c87985f4b955287d5befdddc16d76cbbbb1c5918a48b513ec8644d4533a
4
+ data.tar.gz: 4d2e3991b90db73ed1be5e784d8a736c2a988122f652551e0f201455cf5a71ac
5
5
  SHA512:
6
- metadata.gz: 936988827ee762d8e9206f0393be011e9dbd1162db5301f65fa06e8621b058f27f3850b313829be32da5a9f2ab4af3530829b84a1d53653d79597f67d3702c41
7
- data.tar.gz: 054b6a7e1cc0ef472444a25090ce4f1c5a01d869aa24d1a51b62b216132a7f3657714cd5fb63e0b566a4afa9b6c75a41ed30624061397657a5df111207eca2ca
6
+ metadata.gz: 5f2eeb87cba88ea5082bbf104cb67b707ecdc6579cb710268f13ab13aa50880961301a80343d370e3f2bedd9da61f86a6b81ba4f4a6fb70eea88074a5c343aa5
7
+ data.tar.gz: 7b6f11c69c1d72f6713d983590387896b299f7f681206bf1bcaaac855c7f3152e61fdac7669da0d0c952cc308e6162a2e283d3e22fdc38c408a6834ef1e48a79
data/README.md CHANGED
@@ -79,7 +79,7 @@ PuppetLint.configuration.send('disable_legacy_facts')
79
79
  Alternatively, you can disable it directly in your puppet manifest.
80
80
 
81
81
  ```puppet
82
- # lint:ignore:lint-legacy_facts
82
+ # lint:ignore:legacy_facts
83
83
  $package_name = $facts['operatingsystem'] {
84
84
  'CentOS' => 'httpd',
85
85
  'Debian' => 'apache2',
@@ -96,6 +96,14 @@ PuppetLint.new_check(:legacy_facts) do
96
96
  'xendomains' => "facts['xen']['domains']",
97
97
  'zonename' => "facts['solaris_zones']['current']",
98
98
  }
99
+
100
+ # A list of valid hash key token types
101
+ HASH_KEY_TYPES = Set[
102
+ :STRING, # Double quoted string
103
+ :SSTRING, # Single quoted string
104
+ :NAME, # Unquoted single word
105
+ ].freeze
106
+
99
107
  def check
100
108
  tokens.select { |x| LEGACY_FACTS_VAR_TYPES.include?(x.type) }.each do |token|
101
109
  fact_name = ''
@@ -108,36 +116,50 @@ PuppetLint.new_check(:legacy_facts) do
108
116
  # This matches using legacy facts in a the new structured fact. For
109
117
  # example this would match 'uuid' in $facts['uuid'] so it can be converted
110
118
  # to facts['dmi']['product']['uuid']"
111
- elsif token.value.start_with?("facts['") then
119
+ elsif token.value == 'facts' then
120
+ fact_name = hash_key_for(token)
121
+
122
+ elsif token.value.start_with?("facts['")
112
123
  fact_name = token.value.match(/facts\['(.*)'\]/)[1]
113
124
  end
114
125
 
115
126
  if EASY_FACTS.include?(fact_name) or UNCONVERTIBLE_FACTS.include?(fact_name) or fact_name.match(Regexp.union(REGEX_FACTS)) then
116
127
  notify :warning, {
117
- :message => 'legacy fact',
118
- :line => token.line,
119
- :column => token.column,
120
- :token => token,
128
+ :message => 'legacy fact',
129
+ :line => token.line,
130
+ :column => token.column,
131
+ :token => token,
132
+ :fact_name => fact_name,
121
133
  }
122
134
  end
123
135
  end
124
136
  end
125
137
 
126
- def fix(problem)
127
- # This probably should never occur, but if it does then bail out:
128
- raise PuppetLint::NoFix if problem[:token].raw and problem[:token].value != problem[:token].raw
138
+ # If the variable is using the $facts hash represented internally by multiple
139
+ # tokens, this helper simplifies accessing the hash key.
140
+ def hash_key_for(token)
141
+ lbrack_token = token.next_code_token
142
+ return '' unless lbrack_token && lbrack_token.type == :LBRACK
143
+
144
+ key_token = lbrack_token.next_code_token
145
+ return '' unless key_token && HASH_KEY_TYPES.include?(key_token.type)
129
146
 
130
- # Get rid of the top scope before we do our work. We don't need to
131
- # preserve it because it won't work with the new structured facts.
132
- if problem[:token].value.start_with?('::') then
133
- fact_name = problem[:token].value.sub(/^::/, '')
147
+ key_token.value
148
+ end
134
149
 
135
- # This matches using legacy facts in a the new structured fact. For
136
- # example this would match 'uuid' in $facts['uuid'] so it can be converted
137
- # to facts['dmi']['product']['uuid']"
138
- elsif problem[:token].value.start_with?("facts['") then
139
- fact_name = problem[:token].value.match(/facts\['(.*)'\]/)[1]
150
+ def fix(problem)
151
+ fact_name = problem[:fact_name]
152
+
153
+ # Check if the variable is using the $facts hash represented internally by
154
+ # multiple tokens and remove the tokens for the old legacy key if so.
155
+ if problem[:token].value == 'facts'
156
+ loop do
157
+ t = problem[:token].next_token
158
+ remove_token(t)
159
+ break if t.type == :RBRACK
160
+ end
140
161
  end
162
+
141
163
  if EASY_FACTS.include?(fact_name)
142
164
  problem[:token].value = EASY_FACTS[fact_name]
143
165
  elsif fact_name.match(Regexp.union(REGEX_FACTS))
@@ -157,7 +179,5 @@ PuppetLint.new_check(:legacy_facts) do
157
179
  problem[:token].value = "facts['solaris_zones']['zones']['" << m['name'] << "']['" << m['attribute'] << "']"
158
180
  end
159
181
  end
160
-
161
- problem[:token].raw = problem[:token].value unless problem[:token].raw.nil?
162
182
  end
163
183
  end
@@ -106,6 +106,14 @@ describe 'legacy_facts' do
106
106
  expect(problems).to have(1).problem
107
107
  end
108
108
  end
109
+
110
+ context 'fact variable using legacy facts hash variable in interpolation' do
111
+ let(:code) { %("${facts['osfamily']}") }
112
+
113
+ it 'detects a single problem' do
114
+ expect(problems).to have(1).problem
115
+ end
116
+ end
109
117
  end
110
118
 
111
119
 
@@ -391,5 +399,16 @@ describe 'legacy_facts' do
391
399
  expect(manifest).to eq("\"$facts['os']['distro']['release']['specification']\"")
392
400
  end
393
401
  end
402
+ context "fact variable using facts hash in double quotes \"$facts['lsbrelease']\"" do
403
+ let(:code) { "\"${facts['lsbrelease']}\"" }
404
+
405
+ it 'should only detect a single problem' do
406
+ expect(problems).to have(1).problem
407
+ end
408
+
409
+ it 'should use the facts hash' do
410
+ expect(manifest).to eq("\"${facts['os']['distro']['release']['specification']}\"")
411
+ end
412
+ end
394
413
  end
395
414
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-legacy_facts-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark McKinstry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-27 00:00:00.000000000 Z
11
+ date: 2020-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.3.6
19
+ version: '2.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.3.6
26
+ version: '2.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement