puppet-lint-wmf_styleguide-check 1.0.7 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02e709fa4e38aa92929077506d5cacdb8e3f17114bb75596224444f385f18aab
4
- data.tar.gz: a392eead1036bdd3d1bcfd0546291d9c5f154b7a7b3797a30d0a8b7e70efa340
3
+ metadata.gz: 899c8c3526bd03b42700fe80c845fc590fc60876ef10410e1216fcab6e4ac7e5
4
+ data.tar.gz: 4ed16c3cff59cfa2c72b6dca1af26210daca111ff92e984c999087acd5c44b62
5
5
  SHA512:
6
- metadata.gz: 51814c0df70fe7e80edff7c97bd89e9478a4dbc886ddff1d1bf82a536fa4a6a08759fce975826cc1d2fca0b100e3e66378f60ce07fb320cbc7536f6413b1dc8d
7
- data.tar.gz: 18ad1c6221aca98d187ee93898d5aad4e820769a007e77332625bdbbb6418d47816b7002b89035e94f5b26daf3484f340b9f35b76ca6e443f378a6730c128b38
6
+ metadata.gz: b550d1bf27ecffec3b069cf131367232a0b65dc8da3c6296054aa6d1c634b655bd291cece4bf6c71fbb4a2e41939f6455c86a8a594e36d1fe486178f24eec3b8
7
+ data.tar.gz: d0641b8fd0fe54e5430a1fab4f5e0b40d24d97243ceed0d8195d9faeaa2c57700b91597af050c962b959aed2c0fba6edb4600badc66286d91440df31a57e9110
@@ -102,9 +102,14 @@ class PuppetResource
102
102
  class? && (module_name == role_module)
103
103
  end
104
104
 
105
- def hiera_calls
105
+ def lookup_calls
106
+ # Returns an array of all the tokens referencing calls to lookup
107
+ @resource[:tokens].select(&:lookup?)
108
+ end
109
+
110
+ def legacy_hiera_calls
106
111
  # Returns an array of all the tokens referencing calls to hiera
107
- @resource[:tokens].select(&:hiera?)
112
+ @resource[:tokens].select(&:legacy_hiera?)
108
113
  end
109
114
 
110
115
  def legacy_validate_calls
@@ -145,9 +150,9 @@ class PuppetLint
145
150
  [:NAME, :FUNCTION_NAME].include?(@type) && @next_code_token.type == :LPAREN
146
151
  end
147
152
 
148
- def hiera?
149
- # A function call specifically calling hiera
150
- function? && ['hiera', 'hiera_array', 'hiera_hash', 'lookup'].include?(@value)
153
+ def legacy_hiera?
154
+ # Using old hiera call
155
+ function? && ['hiera', 'hiera_array', 'hiera_hash'].include?(@value)
151
156
  end
152
157
 
153
158
  def lookup?
@@ -198,10 +203,10 @@ end
198
203
 
199
204
  # Checks and functions
200
205
  def check_profile(klass)
201
- # All parameters of profiles should have a default value that is a hiera lookup
206
+ # All parameters of profiles should have a default value that is a lookup
202
207
  params_without_lookup_defaults klass
203
- # All hiera lookups should be in parameters
204
- hiera_not_in_params klass
208
+ # All lookup lookups should be in parameters
209
+ lookup_not_in_params klass
205
210
  # Only a few selected classes should be included in a profile
206
211
  profile_illegal_include klass
207
212
  # System::role only goes in roles
@@ -210,7 +215,7 @@ end
210
215
 
211
216
  def check_role(klass)
212
217
  # Hiera lookups within a role are forbidden
213
- hiera klass
218
+ lookup klass
214
219
  # A role should only include profiles
215
220
  include_not_profile klass
216
221
  # A call, and only one, to system::role will be done
@@ -220,8 +225,8 @@ def check_role(klass)
220
225
  end
221
226
 
222
227
  def check_class(klass)
223
- # No hiera lookups allowed in a class.
224
- hiera klass
228
+ # No lookup lookups allowed in a class.
229
+ lookup klass
225
230
  # Cannot include or declare classes from other modules
226
231
  class_illegal_include klass
227
232
  illegal_class_declaration klass
@@ -230,28 +235,42 @@ def check_class(klass)
230
235
  end
231
236
 
232
237
  def check_define(define)
233
- # No hiera calls are admitted in defines. ever.
234
- hiera define
238
+ # No lookup calls are admitted in defines. ever.
239
+ lookup define
235
240
  # No class can be included in defines, like in classes
236
241
  class_illegal_include define
237
242
  # Non-profile defines should respect the rules for classes
238
243
  illegal_class_declaration define unless define.module_name == 'profile'
239
244
  end
240
245
 
241
- def hiera(klass)
242
- # Searches for hiera calls inside classes and defines.
243
- hiera_errors(klass.hiera_calls, klass)
246
+ def lookup(klass)
247
+ # Searches for lookup calls inside classes and defines.
248
+ lookup_errors(klass.lookup_calls, klass)
249
+ end
250
+
251
+ def legacy_hiera(klass)
252
+ # No calls to legacy hiera
253
+ tokens = klass.legacy_hiera_calls
254
+ tokens.each do |token|
255
+ msg = {
256
+ message: "wmf-style: Found deprecated function (#{token.value}) " \
257
+ "in #{klass.type} '#{klass.name}', use lookup instead",
258
+ line: token.line,
259
+ column: token.column
260
+ }
261
+ notify :error, msg
262
+ end
244
263
  end
245
264
 
246
265
  def params_without_lookup_defaults(klass)
247
- # Finds parameters that have no hiera-defined default value.
266
+ # Finds parameters that have no lookup-defined default value.
248
267
  klass.params.each do |name, data|
249
268
  next unless data[:value].select(&:lookup?).empty?
250
269
  common = "wmf-style: Parameter '#{name}' of class '#{klass.name}'"
251
- message = if data[:value].select(&:hiera?).empty?
270
+ message = if data[:value].select(&:legacy_hiera?).empty?
252
271
  "#{common} has no call to lookup"
253
272
  else
254
- "#{common}: hiera is deprecated use lookup"
273
+ "#{common} hiera is deprecated use lookup"
255
274
  end
256
275
  token = data[:param]
257
276
  msg = { message: message, line: token.line, column: token.column }
@@ -259,24 +278,24 @@ def params_without_lookup_defaults(klass)
259
278
  end
260
279
  end
261
280
 
262
- def hiera_not_in_params(klass)
263
- # Checks if a hiera call is not in a parameter declaration. Used to check profiles
281
+ def lookup_not_in_params(klass)
282
+ # Checks if a lookup call is not in a parameter declaration. Used to check profiles
264
283
 
265
- # Any hiera call that is not inside a parameter declaration is a violation
266
- tokens = klass.hiera_calls.reject do |token|
284
+ # Any lookup call that is not inside a parameter declaration is a violation
285
+ tokens = klass.lookup_calls.reject do |token|
267
286
  maybe_param = token.prev_code_token.prev_code_token
268
287
  klass.params.keys.include?(maybe_param.value)
269
288
  end
270
- hiera_errors(tokens, klass)
289
+ lookup_errors(tokens, klass)
271
290
  end
272
291
 
273
- def hiera_errors(tokens, klass)
274
- # Helper for printing hiera errors nicely
292
+ def lookup_errors(tokens, klass)
293
+ # Helper for printing lookup errors nicely
275
294
  tokens.each do |token|
276
- # hiera ( 'some::label' )
295
+ # lookup ( 'some::label' )
277
296
  value = token.next_code_token.next_code_token.value
278
297
  msg = {
279
- message: "wmf-style: Found hiera call in #{klass.type} '#{klass.name}' for '#{value}'",
298
+ message: "wmf-style: Found lookup call in #{klass.type} '#{klass.name}' for '#{value}'",
280
299
  line: token.line,
281
300
  column: token.column
282
301
  }
@@ -402,6 +421,7 @@ end
402
421
  def check_deprecations(resource)
403
422
  # Check the resource for declarations of deprecated defines
404
423
  legacy_validate_errors resource
424
+ legacy_hiera resource
405
425
  deprecated_defines = ['base::service_unit']
406
426
  deprecated_defines.each do |deprecated|
407
427
  resource.resource?(deprecated).each do |token|
@@ -420,13 +440,18 @@ def check_node(node)
420
440
  title = node[:title_tokens].map(&:value).join(', ')
421
441
  node[:tokens].each do |token|
422
442
  msg = nil
423
- if token.hiera?
443
+ if token.lookup?
424
444
  msg = {
425
- message: "wmf-style: Found hiera call in node '#{title}'",
445
+ message: "wmf-style: node '#{title}' calls lookup function",
446
+ line: token.line,
447
+ column: token.column
448
+ }
449
+ elsif token.legacy_hiera?
450
+ msg = {
451
+ message: "wmf-style: node '#{title}' calls legacy #{token.value} function",
426
452
  line: token.line,
427
453
  column: token.column
428
454
  }
429
-
430
455
  elsif token.class_include?
431
456
  msg = {
432
457
  message: "wmf-style: node '#{title}' includes class #{token.included_class.value}",
@@ -445,12 +470,6 @@ def check_node(node)
445
470
  line: token.line,
446
471
  column: token.column
447
472
  }
448
- elsif token.role_keyword? && token.next_code_token.next_code_token.next_code_token.type != :RPAREN
449
- msg = {
450
- message: "wmf-style: node '#{title}' includes multiple roles",
451
- line: token.line,
452
- column: token.column
453
- }
454
473
  end
455
474
  notify :error, msg if msg
456
475
  end
@@ -474,6 +493,15 @@ PuppetLint.new_check(:wmf_styleguide) do
474
493
  # If we're not within a node definition, skip this token
475
494
  next unless in_node_def
476
495
  case token.type
496
+ when :REGEX
497
+ if !token.value.start_with?('^') || !token.value.end_with?('$')
498
+ msg = {
499
+ message: "wmf-style: regex node matching must match the whole string (^...$), got: #{token.value}",
500
+ line: token.line,
501
+ column: token.column
502
+ }
503
+ notify :error, msg
504
+ end
477
505
  when :LBRACE
478
506
  title_tokens = tokens[start + 1..(i - 1)].select(&:node_def?) if braces_level.zero?
479
507
  braces_level += 1
@@ -14,13 +14,16 @@ EOF
14
14
 
15
15
  class_ko = <<-EOF
16
16
  class foo($t=hiera('foo::title')) {
17
- $msg = hiera( "foo::bar")
17
+ $msg = lookup( "foo::bar")
18
18
  notice($msg)
19
19
  notice($t)
20
20
  include ::passwords::redis
21
21
  class { 'bar': }
22
22
  validate_foobar($param)
23
23
  validate_re($param, '^.*$')
24
+ hiera('foobar')
25
+ hiera_hash('foobar')
26
+ hiera_array('foobar')
24
27
  }
25
28
  EOF
26
29
 
@@ -42,7 +45,7 @@ class profile::fixme (
42
45
  $test4=hiera_hash('profile::foobar::foo')
43
46
  ) {
44
47
  include ::apache2::common
45
- $role = hiera('role')
48
+ $role = lookup('role')
46
49
  system::role { $role: }
47
50
  }
48
51
  EOF
@@ -78,23 +81,46 @@ define foo::fixme ($a=hiera('something')) {
78
81
  class { '::bar': }
79
82
  validate_foobar($param)
80
83
  validate_re($param, '^.*$')
84
+ hiera('foobar')
85
+ hiera_hash('foobar')
86
+ hiera_array('foobar')
81
87
  }
82
88
  EOF
83
89
 
84
90
  node_ok = <<-EOF
85
- node /^test1.*\.example\.com$/ {
91
+ node /^test1.*\\.example\\.com$/ {
86
92
  role(spare::system)
87
93
  }
88
94
  EOF
89
95
 
90
96
  node_ko = <<-EOF
91
97
  node 'fixme' {
92
- role(spare::system,
93
- mediawiki::appserver)
94
98
  include base::firewall
95
99
  interface::mapped { 'eth0':
96
100
  foo => 'bar'
97
101
  }
102
+ lookup('foobar')
103
+ hiera('foobar')
104
+ hiera_array('foobar')
105
+ hiera_hash('foobar')
106
+ }
107
+ EOF
108
+
109
+ node_regex_missing_start = <<-EOF
110
+ node /test1.*\\.example\\.com$/ {
111
+ role(spare::system)
112
+ }
113
+ EOF
114
+
115
+ node_regex_missing_end = <<-EOF
116
+ node /^test1.*\\.example\\.com/ {
117
+ role(spare::system)
118
+ }
119
+ EOF
120
+
121
+ node_regex_missing_both = <<-EOF
122
+ node /test1.*\\.example\\.com/ {
123
+ role(spare::system)
98
124
  }
99
125
  EOF
100
126
 
@@ -128,8 +154,8 @@ describe 'wmf_styleguide' do
128
154
  context 'class with errors' do
129
155
  let(:code) { class_ko }
130
156
  it 'should create errors for hiera declarations' do
131
- expect(problems).to contain_error("wmf-style: Found hiera call in class 'foo' for 'foo::title'").on_line(1).in_column(14)
132
- expect(problems).to contain_error("wmf-style: Found hiera call in class 'foo' for 'foo::bar'").on_line(2).in_column(15)
157
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera) in class 'foo', use lookup instead").on_line(1).in_column(14)
158
+ expect(problems).to contain_error("wmf-style: Found lookup call in class 'foo' for 'foo::bar'").on_line(2).in_column(15)
133
159
  end
134
160
  it 'should create errors for included classes' do
135
161
  expect(problems).to contain_error("wmf-style: class 'foo' includes passwords::redis from another module").on_line(5).in_column(16)
@@ -139,18 +165,27 @@ describe 'wmf_styleguide' do
139
165
  expect(problems).to contain_error("wmf-style: Found legacy function (validate_foobar) call in class 'foo'").on_line(7).in_column(8)
140
166
  expect(problems).to contain_error("wmf-style: Found legacy function (validate_re) call in class 'foo'").on_line(8).in_column(8)
141
167
  end
168
+ it 'should create errors for hiera function' do
169
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera) in class 'foo', use lookup instead").on_line(9).in_column(8)
170
+ end
171
+ it 'should create errors for hiera_hash function' do
172
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera_hash) in class 'foo', use lookup instead").on_line(10).in_column(8)
173
+ end
174
+ it 'should create errors for hiera_array function' do
175
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera_array) in class 'foo', use lookup instead").on_line(11).in_column(8)
176
+ end
142
177
  end
143
178
 
144
179
  context 'profile with errors' do
145
180
  let(:code) { profile_ko }
146
181
  it 'should create errors for parameters without hiera defaults' do
147
182
  expect(problems).to contain_error("wmf-style: Parameter 'test1' of class 'profile::fixme' has no call to lookup").on_line(2).in_column(7)
148
- expect(problems).to contain_error("wmf-style: Parameter 'test2' of class 'profile::fixme': hiera is deprecated use lookup").on_line(3).in_column(7)
149
- expect(problems).to contain_error("wmf-style: Parameter 'test3' of class 'profile::fixme': hiera is deprecated use lookup").on_line(4).in_column(7)
150
- expect(problems).to contain_error("wmf-style: Parameter 'test4' of class 'profile::fixme': hiera is deprecated use lookup").on_line(5).in_column(7)
183
+ expect(problems).to contain_error("wmf-style: Parameter 'test2' of class 'profile::fixme' hiera is deprecated use lookup").on_line(3).in_column(7)
184
+ expect(problems).to contain_error("wmf-style: Parameter 'test3' of class 'profile::fixme' hiera is deprecated use lookup").on_line(4).in_column(7)
185
+ expect(problems).to contain_error("wmf-style: Parameter 'test4' of class 'profile::fixme' hiera is deprecated use lookup").on_line(5).in_column(7)
151
186
  end
152
187
  it 'should create errors for hiera calls in body' do
153
- expect(problems).to contain_error("wmf-style: Found hiera call in class 'profile::fixme' for 'role'").on_line(8).in_column(13)
188
+ expect(problems).to contain_error("wmf-style: Found lookup call in class 'profile::fixme' for 'role'").on_line(8).in_column(13)
154
189
  end
155
190
  it 'should create errors for use of system::role' do
156
191
  expect(problems).to contain_error("wmf-style: class 'profile::fixme' declares system::role, which should only be used in roles").on_line(9).in_column(5)
@@ -175,7 +210,7 @@ describe 'wmf_styleguide' do
175
210
  context 'defined type with violations' do
176
211
  let(:code) { define_ko }
177
212
  it 'should not contain hiera calls' do
178
- expect(problems).to contain_error("wmf-style: Found hiera call in defined type 'foo::fixme' for 'something'").on_line(1)
213
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera) in defined type 'foo::fixme', use lookup instead").on_line(1)
179
214
  end
180
215
  it 'should not include or define any class' do
181
216
  expect(problems).to contain_error("wmf-style: defined type 'foo::fixme' declares class bar from another module").on_line(3)
@@ -184,6 +219,15 @@ describe 'wmf_styleguide' do
184
219
  expect(problems).to contain_error("wmf-style: Found legacy function (validate_foobar) call in defined type 'foo::fixme'").on_line(4).in_column(8)
185
220
  expect(problems).to contain_error("wmf-style: Found legacy function (validate_re) call in defined type 'foo::fixme'").on_line(5).in_column(8)
186
221
  end
222
+ it 'should create errors for hiera function' do
223
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera) in defined type 'foo::fixme', use lookup instead").on_line(6).in_column(8)
224
+ end
225
+ it 'should create errors for hiera_hash function' do
226
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera_hash) in defined type 'foo::fixme', use lookup instead").on_line(7).in_column(8)
227
+ end
228
+ it 'should create errors for hiera_array function' do
229
+ expect(problems).to contain_error("wmf-style: Found deprecated function (hiera_array) in defined type 'foo::fixme', use lookup instead").on_line(8).in_column(8)
230
+ end
187
231
  end
188
232
 
189
233
  context 'node with no errors' do
@@ -194,15 +238,43 @@ describe 'wmf_styleguide' do
194
238
  end
195
239
  context 'node with violations' do
196
240
  let(:code) { node_ko }
197
- it 'should not have multiple roles applied' do
198
- expect(problems).to contain_error("wmf-style: node 'fixme' includes multiple roles").on_line(2)
199
- end
200
241
  it 'should not include classes directly' do
201
242
  expect(problems).to contain_error("wmf-style: node 'fixme' includes class base::firewall")
202
243
  end
203
244
  it 'should not declare any defined type' do
204
245
  expect(problems).to contain_error("wmf-style: node 'fixme' declares interface::mapped")
205
246
  end
247
+ it 'should not call lookup' do
248
+ expect(problems).to contain_error("wmf-style: node 'fixme' calls lookup function")
249
+ end
250
+ it 'should not call hiera' do
251
+ expect(problems).to contain_error("wmf-style: node 'fixme' calls legacy hiera function")
252
+ end
253
+ it 'should not call hiera_array' do
254
+ expect(problems).to contain_error("wmf-style: node 'fixme' calls legacy hiera_array function")
255
+ end
256
+ it 'should not call hiera_hash' do
257
+ expect(problems).to contain_error("wmf-style: node 'fixme' calls legacy hiera_hash function")
258
+ end
259
+ end
260
+
261
+ context 'node regex with start violation' do
262
+ let(:code) { node_regex_missing_start }
263
+ it 'should start the regex with ^' do
264
+ expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: test1.*\\.example\\.com$')
265
+ end
266
+ end
267
+ context 'node regex with end violation' do
268
+ let(:code) { node_regex_missing_end }
269
+ it 'should end the regex with $' do
270
+ expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: ^test1.*\\.example\\.com')
271
+ end
272
+ end
273
+ context 'node regex with start and end violations' do
274
+ let(:code) { node_regex_missing_both }
275
+ it 'should start the regex with ^ and end it with $' do
276
+ expect(problems).to contain_error('wmf-style: regex node matching must match the whole string (^...$), got: test1.*\\.example\\.com')
277
+ end
206
278
  end
207
279
 
208
280
  context 'defined type with deprecations' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-wmf_styleguide-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giuseppe Lavagetto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2023-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.49.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.17.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.17.1
111
125
  description: |2
112
126
  A puppet-lint plugin to check that the code adheres to the WMF coding guidelines:
113
127
 
@@ -149,10 +163,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
163
  - !ruby/object:Gem::Version
150
164
  version: '0'
151
165
  requirements: []
152
- rubygems_version: 3.0.3
166
+ rubygems_version: 3.2.5
153
167
  signing_key:
154
168
  specification_version: 4
155
169
  summary: A puppet-lint plugin to check code adheres to the WMF coding guidelines
156
170
  test_files:
157
- - spec/spec_helper.rb
158
171
  - spec/puppet-lint/plugins/check_wmf_styleguide_check_spec.rb
172
+ - spec/spec_helper.rb