puppet-lint-nine-check 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,257 @@
1
+ require "spec_helper"
2
+
3
+ describe "class_alignment_params_newline" do
4
+ let(:msg) { "`%s` should be in a new line (expected in line %d, but found it in line %d)" }
5
+
6
+ context "with fix disabled" do
7
+ context "tidy code" do
8
+ let(:code) do
9
+ <<-END
10
+ class bar(
11
+ $someparam = 'somevalue',
12
+ $another,
13
+ String $nope,
14
+ $foo,
15
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
16
+ # $aaa,
17
+ String $third = 'meh',
18
+ Array[Int, Int] $asdf = 'asdf',
19
+ ) { }
20
+ END
21
+ end
22
+
23
+ it "should detect 0 problems" do
24
+ expect(problems).to have(0).problems
25
+ end
26
+ end
27
+
28
+ context "messy code" do
29
+ let(:code) do
30
+ <<-END
31
+ class bar( $someparam = 'somevalue',
32
+ $another, String $nope, $foo,
33
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
34
+ # $aaa,
35
+ String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
36
+ ) { }
37
+ END
38
+ end
39
+
40
+ it "should detect 4 problems" do
41
+ expect(problems).to have(4).problems
42
+ end
43
+
44
+ it "should create four warnings" do
45
+ expect(problems).to contain_warning(format(msg, "$someparam", 2, 1)).on_line(1).in_column(77)
46
+ expect(problems).to contain_warning(format(msg, "$nope", 3, 2)).on_line(2).in_column(30)
47
+ expect(problems).to contain_warning(format(msg, "$foo", 3, 2)).on_line(2).in_column(37)
48
+ expect(problems).to contain_warning(format(msg, "$asdf", 6, 5)).on_line(5).in_column(101)
49
+ end
50
+ end
51
+ end
52
+
53
+ context "with fix enabled" do
54
+ before do
55
+ PuppetLint.configuration.fix = true
56
+ end
57
+
58
+ after do
59
+ PuppetLint.configuration.fix = false
60
+ end
61
+
62
+ context "messy code" do
63
+ let(:code) do
64
+ <<-END
65
+ class bar( $someparam = 'somevalue',
66
+ $another, String $nope, $foo,
67
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
68
+ # $aaa,
69
+ String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
70
+ ) { }
71
+ END
72
+ end
73
+
74
+ let(:fixed) do
75
+ <<-END
76
+ class bar(
77
+ $someparam = 'somevalue',
78
+ $another,
79
+ String $nope,
80
+ $foo,
81
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
82
+ # $aaa,
83
+ String $third = 'meh',
84
+ Array[Int, Int] $asdf = 'asdf',
85
+ ) { }
86
+ END
87
+ end
88
+
89
+ it "should fix the problems" do
90
+ expect(manifest).to eq(fixed)
91
+ end
92
+ end
93
+
94
+ context "in a single line" do
95
+ let(:code) do
96
+ <<-END
97
+ class foo ($foo = 1, $bar = $a) {}
98
+ class foo ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a) {}
99
+
100
+ class bar ($foo = 1, $bar = $a,) {}
101
+ class bar ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
102
+
103
+ class aaa ( $foo = 1, $bar = $a,) {}
104
+ class aaa ( Integer $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
105
+
106
+ class bbb ( $foo = 1, $bar = $a, ) {}
107
+ class bbb ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a, ) {}
108
+
109
+ class ccc ($foo = 1) {}
110
+
111
+ class ddd {}
112
+
113
+ class eee (
114
+ $foo = 1,
115
+ $workers = max($::processorcount, 1),
116
+ $database_path = $aaa,) inherits sap_zabbix::params { }
117
+ { }
118
+
119
+ class fff ($foo, $bar=[], $foo, $bar=[], $foo, $bar=[], $listen_ips = [$::ipaddress]) {}
120
+
121
+ define ggg ($foo, $bar=[]) {}
122
+
123
+ define long_ggg ($foo, $bar=[], $foo, $bar=[], $foo, $bar=[], $foo, $bar=[]) {}
124
+
125
+ define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern, $expire, $port) { }
126
+
127
+ define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern=$a and $b, $epe=-$foo, $port=!$foo) { }
128
+
129
+ class aaa (
130
+ $aaa = func('bbb', $ccc), $bar = $a, $foo = 1, $bar = $a,
131
+ ) {}
132
+
133
+ class name (
134
+ Boolean $foo,
135
+ Optional[String[1]] $bar,
136
+ ) { }
137
+ END
138
+ end
139
+
140
+ let(:fixed) do
141
+ <<-END
142
+ class foo ($foo = 1, $bar = $a) {}
143
+ class foo (
144
+ $foo = 1,
145
+ $bar = $a,
146
+ $foo = 1,
147
+ $bar = $a,
148
+ $foo = 1,
149
+ $bar = $a
150
+ ) {}
151
+
152
+ class bar ($foo = 1, $bar = $a,) {}
153
+ class bar (
154
+ $foo = 1,
155
+ $bar = $a,
156
+ $foo = 1,
157
+ $bar = $a,
158
+ $foo = 1,
159
+ $bar = $a,
160
+ ) {}
161
+
162
+ class aaa ( $foo = 1, $bar = $a,) {}
163
+ class aaa (
164
+ Integer $foo = 1,
165
+ $bar = $a,
166
+ $foo = 1,
167
+ $bar = $a,
168
+ $foo = 1,
169
+ $bar = $a,
170
+ ) {}
171
+
172
+ class bbb ( $foo = 1, $bar = $a, ) {}
173
+ class bbb (
174
+ $foo = 1,
175
+ $bar = $a,
176
+ $foo = 1,
177
+ $bar = $a,
178
+ $foo = 1,
179
+ $bar = $a,
180
+ ) {}
181
+
182
+ class ccc ($foo = 1) {}
183
+
184
+ class ddd {}
185
+
186
+ class eee (
187
+ $foo = 1,
188
+ $workers = max($::processorcount, 1),
189
+ $database_path = $aaa,
190
+ ) inherits sap_zabbix::params { }
191
+ { }
192
+
193
+ class fff (
194
+ $foo,
195
+ $bar=[],
196
+ $foo,
197
+ $bar=[],
198
+ $foo,
199
+ $bar=[],
200
+ $listen_ips = [$::ipaddress]
201
+ ) {}
202
+
203
+ define ggg ($foo, $bar=[]) {}
204
+
205
+ define long_ggg (
206
+ $foo,
207
+ $bar=[],
208
+ $foo,
209
+ $bar=[],
210
+ $foo,
211
+ $bar=[],
212
+ $foo,
213
+ $bar=[]
214
+ ) {}
215
+
216
+ define asdf (
217
+ $prefix,
218
+ $pattern,
219
+ $expire,
220
+ $port,
221
+ $prefix,
222
+ $pattern,
223
+ $expire,
224
+ $port
225
+ ) { }
226
+
227
+ define asdf (
228
+ $prefix,
229
+ $pattern,
230
+ $expire,
231
+ $port,
232
+ $prefix,
233
+ $pattern=$a and $b,
234
+ $epe=-$foo,
235
+ $port=!$foo
236
+ ) { }
237
+
238
+ class aaa (
239
+ $aaa = func('bbb', $ccc),
240
+ $bar = $a,
241
+ $foo = 1,
242
+ $bar = $a,
243
+ ) {}
244
+
245
+ class name (
246
+ Boolean $foo,
247
+ Optional[String[1]] $bar,
248
+ ) { }
249
+ END
250
+ end
251
+
252
+ it "should fix the problems" do
253
+ expect(manifest).to eq(fixed)
254
+ end
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,321 @@
1
+ require "spec_helper"
2
+
3
+ describe "class_alignment_params" do
4
+ let(:msg) { "indentation of $ is not properly aligned (expected in column %d, but found it in column %d)" }
5
+
6
+ context "with fix disabled" do
7
+ context "with correct indentation" do
8
+ let(:code) do
9
+ <<-END
10
+ class foo (
11
+ $foo = 1,
12
+ $bar = 2,
13
+ $gronk = 3,
14
+ $baz = 4,
15
+ $meh = 5,
16
+ ) {}
17
+ END
18
+ end
19
+
20
+ it "should not detect any problems" do
21
+ expect(problems).to have(0).problems
22
+ end
23
+ end
24
+
25
+ context "single resource with a misaligned $" do
26
+ let(:code) do
27
+ <<-END
28
+ class foo (
29
+ $foo = 1,
30
+ $bar = 2,
31
+ $gronk = 3,
32
+ $baz = 4,
33
+ $meh = 5,
34
+ ) {}
35
+ END
36
+ end
37
+
38
+ it "should detect four problems" do
39
+ expect(problems).to have(4).problems
40
+ end
41
+
42
+ it "should create four warnings" do
43
+ expect(problems).to contain_warning(format(msg, 13, 11)).on_line(3).in_column(11)
44
+ expect(problems).to contain_warning(format(msg, 13, 11)).on_line(4).in_column(11)
45
+ expect(problems).to contain_warning(format(msg, 13, 11)).on_line(5).in_column(11)
46
+ expect(problems).to contain_warning(format(msg, 13, 12)).on_line(6).in_column(12)
47
+ end
48
+ end
49
+
50
+ context "complex resource with a misaligned $" do
51
+ let(:code) do
52
+ <<-END
53
+ class foo (
54
+ $foo = 1,
55
+ $bar = $baz ? {
56
+ gronk => 2,
57
+ meh => 3,
58
+ },
59
+ $meep = 4,
60
+ $bah= 5,
61
+ ) {}
62
+ END
63
+ end
64
+
65
+ it "should detect three problems" do
66
+ expect(problems).to have(3).problems
67
+ end
68
+
69
+ it "should create three warnings" do
70
+ expect(problems).to contain_warning(format(msg, 13, 11)).on_line(3).in_column(11)
71
+ expect(problems).to contain_warning(format(msg, 13, 15)).on_line(7).in_column(15)
72
+ expect(problems).to contain_warning(format(msg, 13, 17)).on_line(8).in_column(17)
73
+ end
74
+ end
75
+
76
+ context "resource with unaligned $ in commented line" do
77
+ let(:code) do
78
+ <<-END
79
+ class foo (
80
+ $ensure = directory,
81
+ # $purge = true,
82
+ ) {}
83
+ END
84
+ end
85
+
86
+ it "should not detect any problems" do
87
+ expect(problems).to have(0).problems
88
+ end
89
+ end
90
+
91
+ context "multiline resource with a single line of params" do
92
+ let(:code) do
93
+ <<-END
94
+ class mymodule::do_thing (
95
+ $whatever = 'bar', $one = 'two',
96
+ ) {}
97
+ END
98
+ end
99
+
100
+ it "should not detect any problems" do
101
+ expect(problems).to have(0).problems
102
+ end
103
+ end
104
+
105
+ context "resource with aligned $ too far out" do
106
+ let(:code) do
107
+ <<-END
108
+ class foo (
109
+ $ensure = file,
110
+ $mode = '0444',
111
+ ) {}
112
+ END
113
+ end
114
+
115
+ it "should detect 0 problems" do
116
+ expect(problems).to have(0).problems
117
+ end
118
+ end
119
+
120
+ context "resource with multiple params where one is an empty hash" do
121
+ let(:code) do
122
+ <<-END
123
+ class foo (
124
+ $a = true,
125
+ $b = {
126
+ }
127
+ ) {}
128
+ END
129
+ end
130
+
131
+ it "should not detect any problems" do
132
+ expect(problems).to have(0).problems
133
+ end
134
+ end
135
+
136
+ context "multiline resource with multiple params on a line" do
137
+ let(:code) do
138
+ <<-END
139
+ class test (
140
+ $a = 'foo', $bb = 'bar',
141
+ $ccc = 'baz',
142
+ ) {}
143
+ END
144
+ end
145
+
146
+ it "should detect 0 problems" do
147
+ expect(problems).to have(0).problems
148
+ end
149
+ end
150
+ end
151
+
152
+ context "with fix enabled" do
153
+ before do
154
+ PuppetLint.configuration.fix = true
155
+ end
156
+
157
+ after do
158
+ PuppetLint.configuration.fix = false
159
+ end
160
+
161
+ context "single resource with a misaligned $" do
162
+ let(:code) do
163
+ <<-END
164
+ class foo (
165
+ $foo = 1,
166
+ $bar = 2,
167
+ $gronk = 3,
168
+ $baz = 4,
169
+ $meh = 5,
170
+ ) {}
171
+ END
172
+ end
173
+
174
+ let(:fixed) do
175
+ <<-END
176
+ class foo (
177
+ $foo = 1,
178
+ $bar = 2,
179
+ $gronk = 3,
180
+ $baz = 4,
181
+ $meh = 5,
182
+ ) {}
183
+ END
184
+ end
185
+
186
+ it "should detect four problems" do
187
+ expect(problems).to have(4).problems
188
+ end
189
+
190
+ it "should fix the manifest" do
191
+ expect(problems).to contain_fixed(format(msg, 13, 15)).on_line(3).in_column(15)
192
+ expect(problems).to contain_fixed(format(msg, 13, 15)).on_line(4).in_column(15)
193
+ expect(problems).to contain_fixed(format(msg, 13, 17)).on_line(5).in_column(17)
194
+ expect(problems).to contain_fixed(format(msg, 13, 11)).on_line(6).in_column(11)
195
+ end
196
+
197
+ it "should align the class_paramss" do
198
+ expect(manifest).to eq(fixed)
199
+ end
200
+ end
201
+
202
+ context "complex resource with a misaligned =" do
203
+ let(:code) do
204
+ <<-END
205
+ class foo (
206
+ $foo = 1,
207
+ $bar = $baz ? {
208
+ gronk => $a,
209
+ meh => $b,
210
+ },
211
+ $meep= 4,
212
+ $bah = 5,
213
+ ) {}
214
+ END
215
+ end
216
+
217
+ let(:fixed) do
218
+ <<-END
219
+ class foo (
220
+ $foo = 1,
221
+ $bar = $baz ? {
222
+ gronk => $a,
223
+ meh => $b,
224
+ },
225
+ $meep= 4,
226
+ $bah = 5,
227
+ ) {}
228
+ END
229
+ end
230
+
231
+ it "should detect three problems" do
232
+ expect(problems).to have(3).problems
233
+ end
234
+
235
+ it "should fix the manifest" do
236
+ expect(problems).to contain_fixed(format(msg, 13, 11)).on_line(3).in_column(11)
237
+ expect(problems).to contain_fixed(format(msg, 13, 11)).on_line(7).in_column(11)
238
+ expect(problems).to contain_fixed(format(msg, 13, 11)).on_line(8).in_column(11)
239
+ end
240
+
241
+ it "should align the class_paramss" do
242
+ expect(manifest).to eq(fixed)
243
+ end
244
+ end
245
+
246
+ context "multiline resource with multiple params on a line" do
247
+ let(:code) do
248
+ <<-END
249
+ class test (
250
+ $a = 'foo', $bb = 'bar',
251
+ $ccc = 'baz',
252
+ ) {}
253
+ END
254
+ end
255
+
256
+ let(:fixed) do
257
+ <<-END
258
+ class test (
259
+ $a = 'foo', $bb = 'bar',
260
+ $ccc = 'baz',
261
+ ) {}
262
+ END
263
+ end
264
+
265
+ it "should detect 1 problem" do
266
+ expect(problems).to have(1).problems
267
+ end
268
+
269
+ it "should fix 1 problem" do
270
+ expect(problems).to contain_fixed(format(msg, 13, 11)).on_line(3).in_column(11)
271
+ end
272
+
273
+ it "should move the extra param onto its own line and realign" do
274
+ expect(manifest).to eq(fixed)
275
+ end
276
+ end
277
+
278
+ context "multiline resource with inline variables" do
279
+ let(:code) do
280
+ <<-END
281
+ class name (
282
+ $aaa = function('foo', 'bar'),
283
+ $bbb = function('foo', 'bar'),
284
+ Boolean $key1 = false,
285
+ Enum['never', 'allow', 'try', 'demand'] $key2,
286
+ $materializer_version = $foo ? {
287
+ default => "foo ${bar} baz ${gronk} qux 0.4.1-1.el${::facts['operatingsystemmajrelease']}"
288
+ }) { }
289
+ END
290
+ end
291
+
292
+ let(:fixed) do
293
+ <<-END
294
+ class name (
295
+ $aaa = function('foo', 'bar'),
296
+ $bbb = function('foo', 'bar'),
297
+ Boolean $key1 = false,
298
+ Enum['never', 'allow', 'try', 'demand'] $key2,
299
+ $materializer_version = $foo ? {
300
+ default => "foo ${bar} baz ${gronk} qux 0.4.1-1.el${::facts['operatingsystemmajrelease']}"
301
+ }) { }
302
+ END
303
+ end
304
+
305
+ it "should detect 4 problems" do
306
+ expect(problems).to have(4).problems
307
+ end
308
+
309
+ it "should fix 4 problems" do
310
+ expect(problems).to contain_fixed(format(msg, 53, 13)).on_line(2).in_column(13)
311
+ expect(problems).to contain_fixed(format(msg, 53, 15)).on_line(3).in_column(15)
312
+ expect(problems).to contain_fixed(format(msg, 53, 21)).on_line(4).in_column(21)
313
+ expect(problems).to contain_fixed(format(msg, 53, 11)).on_line(6).in_column(11)
314
+ end
315
+
316
+ it "should move the extra param onto its own line and realign" do
317
+ expect(manifest).to eq(fixed)
318
+ end
319
+ end
320
+ end
321
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rspec/collection_matchers"
3
4
  require "puppet-lint"
4
5
 
5
6
  PuppetLint::Plugins.load_spec_helper
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-nine-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nine Internet Solutions AG
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-26 00:00:00.000000000 Z
11
+ date: 2024-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -122,10 +122,7 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: |2
126
- A pupet-lint to check to ensure you are not using Nine legacy facts like `$::nine_location`
127
- or `$facts['nine_location']`. You should use the new structured facts like
128
- `$facts['nine_metadata']['location']` instead.
125
+ description: " A collection of pupet-lint checks used at Nine.\n"
129
126
  email:
130
127
  - support@nine.ch
131
128
  executables: []
@@ -134,6 +131,9 @@ extra_rdoc_files: []
134
131
  files:
135
132
  - LICENSE
136
133
  - README.md
134
+ - lib/puppet-lint/plugins/class_alignment.rb
135
+ - lib/puppet-lint/plugins/class_alignment/helper.rb
136
+ - lib/puppet-lint/plugins/class_alignment_params_newline.rb
137
137
  - lib/puppet-lint/plugins/concatenated_template_files.rb
138
138
  - lib/puppet-lint/plugins/explicit_hiera_class_param_lookup.rb
139
139
  - lib/puppet-lint/plugins/nine_legacy_facts.rb
@@ -141,6 +141,9 @@ files:
141
141
  - lib/puppet-lint/plugins/template_file_extension.rb
142
142
  - lib/puppet-lint/plugins/trailing_newline.rb
143
143
  - lib/puppet-lint/plugins/world_writable_files.rb
144
+ - spec/puppet-lint/plugins/class_alignment_equals_spec.rb
145
+ - spec/puppet-lint/plugins/class_alignment_params_newline_spec.rb
146
+ - spec/puppet-lint/plugins/class_alignment_params_spec.rb
144
147
  - spec/puppet-lint/plugins/concatenated_template_files_spec.rb
145
148
  - spec/puppet-lint/plugins/explicit_hiera_class_param_lookup_spec.rb
146
149
  - spec/puppet-lint/plugins/nine_legacy_facts_spec.rb
@@ -168,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
171
  - !ruby/object:Gem::Version
169
172
  version: '0'
170
173
  requirements: []
171
- rubygems_version: 3.4.10
174
+ rubygems_version: 3.4.19
172
175
  signing_key:
173
176
  specification_version: 4
174
177
  summary: A puppet-lint plugin for different checks used at Nine.