puppet-lint-manifest_whitespace-check 0.1.2 → 0.1.4

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.
@@ -0,0 +1,440 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'manifest_whitespace_closing_brace_before' do
6
+ let(:closing_brace_msg) { 'there should be a bracket or a single newline before a closing brace' }
7
+
8
+ context 'with no spaces' do
9
+ let(:code) do
10
+ <<~EOF
11
+ # example
12
+ #
13
+ # Main class, includes all other classes.
14
+ #
15
+
16
+ class example (
17
+ String $content,
18
+ ) {
19
+ $value = [{ 'key' => 'value'}]
20
+ $value2 = [
21
+ {
22
+ 'key' => 'value1',
23
+ },
24
+ {
25
+ 'key' => 'value2',
26
+ },
27
+ ]
28
+ $value3 = myfunc($value1)
29
+ $value4 = ['somekey']
30
+ $value5 = []
31
+ $value6 = {}
32
+ $value7 = "x${server_facts['environment']}y"
33
+
34
+ if someothercondition { include ::otherclass}
35
+ if somecondition {
36
+ class { 'example2':
37
+ param1 => 'value1',
38
+ require => File['somefile'],
39
+ }
40
+ class { 'example3':}
41
+ }
42
+ }
43
+ EOF
44
+ end
45
+
46
+ context 'with fix disabled' do
47
+ it 'should detect 3 problems' do
48
+ expect(problems).to have(3).problem
49
+ end
50
+
51
+ it 'should create a error' do
52
+ expect(problems).to contain_error(closing_brace_msg).on_line(9).in_column(31)
53
+ end
54
+ end
55
+
56
+ context 'with fix enabled' do
57
+ before do
58
+ PuppetLint.configuration.fix = true
59
+ end
60
+
61
+ after do
62
+ PuppetLint.configuration.fix = false
63
+ end
64
+
65
+ it 'should fix a error' do
66
+ expect(problems).to contain_fixed(closing_brace_msg)
67
+ end
68
+
69
+ it 'should add spaces' do
70
+ expect(manifest).to eq(
71
+ <<~EOF,
72
+ # example
73
+ #
74
+ # Main class, includes all other classes.
75
+ #
76
+
77
+ class example (
78
+ String $content,
79
+ ) {
80
+ $value = [{ 'key' => 'value' }]
81
+ $value2 = [
82
+ {
83
+ 'key' => 'value1',
84
+ },
85
+ {
86
+ 'key' => 'value2',
87
+ },
88
+ ]
89
+ $value3 = myfunc($value1)
90
+ $value4 = ['somekey']
91
+ $value5 = []
92
+ $value6 = {}
93
+ $value7 = "x${server_facts['environment']}y"
94
+
95
+ if someothercondition { include ::otherclass }
96
+ if somecondition {
97
+ class { 'example2':
98
+ param1 => 'value1',
99
+ require => File['somefile'],
100
+ }
101
+ class { 'example3': }
102
+ }
103
+ }
104
+ EOF
105
+ )
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'with too many spaces' do
111
+ let(:code) do
112
+ <<~EOF
113
+ # example
114
+ #
115
+ # Main class, includes all other classes.
116
+ #
117
+
118
+ class example (
119
+ String $content,
120
+ ) {
121
+ $value = [{ 'key' => 'value' }]
122
+ $value2 = [
123
+ {
124
+ 'key' => 'value1',
125
+ },
126
+ {
127
+ 'key' => 'value2',
128
+ },
129
+
130
+ ]
131
+ $value3 = myfunc($value1)
132
+ $value4 = ['somekey'
133
+ $value5 = []
134
+ $value6 = { }
135
+ $value7 = "x${server_facts['environment']}y"
136
+
137
+ if someothercondition { include ::otherclass }
138
+ if somecondition {
139
+ class { 'example2':
140
+ param1 => 'value1',
141
+ require => File['somefile'],
142
+ }
143
+ class { 'example3': }
144
+ }
145
+ }
146
+ EOF
147
+ end
148
+
149
+ context 'with fix disabled' do
150
+ it 'should detect 4 problems' do
151
+ expect(problems).to have(4).problems
152
+ end
153
+
154
+ it 'should create a error' do
155
+ expect(problems).to contain_error(closing_brace_msg).on_line(9).in_column(31)
156
+ end
157
+ end
158
+
159
+ context 'with fix enabled' do
160
+ before do
161
+ PuppetLint.configuration.fix = true
162
+ end
163
+
164
+ after do
165
+ PuppetLint.configuration.fix = false
166
+ end
167
+
168
+ it 'should fix a error' do
169
+ expect(problems).to contain_fixed(closing_brace_msg)
170
+ end
171
+
172
+ it 'should add spaces' do
173
+ expect(manifest).to eq(
174
+ <<~EOF,
175
+ # example
176
+ #
177
+ # Main class, includes all other classes.
178
+ #
179
+
180
+ class example (
181
+ String $content,
182
+ ) {
183
+ $value = [{ 'key' => 'value' }]
184
+ $value2 = [
185
+ {
186
+ 'key' => 'value1',
187
+ },
188
+ {
189
+ 'key' => 'value2',
190
+ },
191
+
192
+ ]
193
+ $value3 = myfunc($value1)
194
+ $value4 = ['somekey'
195
+ $value5 = []
196
+ $value6 = {}
197
+ $value7 = "x${server_facts['environment']}y"
198
+
199
+ if someothercondition { include ::otherclass }
200
+ if somecondition {
201
+ class { 'example2':
202
+ param1 => 'value1',
203
+ require => File['somefile'],
204
+ }
205
+ class { 'example3': }
206
+ }
207
+ }
208
+ EOF
209
+ )
210
+ end
211
+ end
212
+ end
213
+
214
+ context 'with too many newlines' do
215
+ let(:code) do
216
+ <<~EOF
217
+ # example
218
+ #
219
+ # Main class, includes all other classes.
220
+ #
221
+
222
+ class example (
223
+ String $content,
224
+ ) {
225
+ $value = [{ 'key' => 'value' }]
226
+ $value2 = [
227
+ {
228
+ 'key' => 'value1',
229
+ },
230
+ {
231
+ 'key' => 'value2',
232
+
233
+ },
234
+ ]
235
+ $value3 = myfunc($value1)
236
+ $value4 = ['somekey'
237
+ $value5 = []
238
+ $value6 = {
239
+
240
+ }
241
+ $value7 = "x${server_facts['environment']}y"
242
+
243
+ if someothercondition { include ::otherclass }
244
+ if somecondition {
245
+ class { 'example2':
246
+ param1 => 'value1',
247
+ require => File['somefile'],
248
+
249
+ }
250
+
251
+ class { 'example3': }
252
+
253
+ }
254
+
255
+ }
256
+ EOF
257
+ end
258
+
259
+ context 'with fix disabled' do
260
+ it 'should detect 5 problems' do
261
+ expect(problems).to have(5).problems
262
+ end
263
+
264
+ it 'should create a error' do
265
+ expect(problems).to contain_error(closing_brace_msg).on_line(15).in_column(25)
266
+ end
267
+ end
268
+
269
+ context 'with fix enabled' do
270
+ before do
271
+ PuppetLint.configuration.fix = true
272
+ end
273
+
274
+ after do
275
+ PuppetLint.configuration.fix = false
276
+ end
277
+
278
+ it 'should fix a error' do
279
+ expect(problems).to contain_fixed(closing_brace_msg)
280
+ end
281
+
282
+ it 'should remove newlines' do
283
+ expect(manifest).to eq(
284
+ <<~EOF,
285
+ # example
286
+ #
287
+ # Main class, includes all other classes.
288
+ #
289
+
290
+ class example (
291
+ String $content,
292
+ ) {
293
+ $value = [{ 'key' => 'value' }]
294
+ $value2 = [
295
+ {
296
+ 'key' => 'value1',
297
+ },
298
+ {
299
+ 'key' => 'value2',
300
+ },
301
+ ]
302
+ $value3 = myfunc($value1)
303
+ $value4 = ['somekey'
304
+ $value5 = []
305
+ $value6 = {
306
+ }
307
+ $value7 = "x${server_facts['environment']}y"
308
+
309
+ if someothercondition { include ::otherclass }
310
+ if somecondition {
311
+ class { 'example2':
312
+ param1 => 'value1',
313
+ require => File['somefile'],
314
+ }
315
+
316
+ class { 'example3': }
317
+ }
318
+ }
319
+ EOF
320
+ )
321
+ end
322
+ end
323
+ end
324
+ end
325
+
326
+ describe 'manifest_whitespace_closing_brace_after' do
327
+ let(:closing_brace_msg) { 'there should be either a bracket, comma, colon, closing quote or a newline after a closing brace, or whitespace and none of the aforementioned' }
328
+
329
+ context 'with spaces' do
330
+ let(:code) do
331
+ <<~EOF
332
+ # example
333
+ #
334
+ # Main class, includes all other classes.
335
+ #
336
+
337
+ class example (
338
+ String $content,
339
+ ) {
340
+ $value = [{ 'key' => 'value' } ]
341
+ $value2 = [
342
+ {
343
+ 'key' => 'value1',
344
+ },
345
+ {
346
+ 'key' => 'value2',
347
+ } ,
348
+ ]
349
+
350
+ $value2bis = {
351
+ 'key' => 'value',
352
+ } # this comment is fine
353
+
354
+ $value3 = myfunc({} )
355
+ $value4 = ['somekey']
356
+ $value5 = []
357
+ $value6 = {}
358
+ $value7 = "x${server_facts['environment']}y"
359
+
360
+ if someothercondition { include ::otherclass }
361
+ if somecondition {
362
+ class { 'example2':
363
+ param1 => 'value1',
364
+ require => File['somefile'],
365
+ }
366
+ class { 'example3': }
367
+ }
368
+ }
369
+ EOF
370
+ end
371
+
372
+ context 'with fix disabled' do
373
+ it 'should detect 3 problems' do
374
+ expect(problems).to have(3).problem
375
+ end
376
+
377
+ it 'should create a error' do
378
+ expect(problems).to contain_error(closing_brace_msg).on_line(9).in_column(33)
379
+ end
380
+ end
381
+
382
+ context 'with fix enabled' do
383
+ before do
384
+ PuppetLint.configuration.fix = true
385
+ end
386
+
387
+ after do
388
+ PuppetLint.configuration.fix = false
389
+ end
390
+
391
+ it 'should fix a error' do
392
+ expect(problems).to contain_fixed(closing_brace_msg)
393
+ end
394
+
395
+ it 'should add spaces' do
396
+ expect(manifest).to eq(
397
+ <<~EOF,
398
+ # example
399
+ #
400
+ # Main class, includes all other classes.
401
+ #
402
+
403
+ class example (
404
+ String $content,
405
+ ) {
406
+ $value = [{ 'key' => 'value' }]
407
+ $value2 = [
408
+ {
409
+ 'key' => 'value1',
410
+ },
411
+ {
412
+ 'key' => 'value2',
413
+ },
414
+ ]
415
+
416
+ $value2bis = {
417
+ 'key' => 'value',
418
+ } # this comment is fine
419
+
420
+ $value3 = myfunc({})
421
+ $value4 = ['somekey']
422
+ $value5 = []
423
+ $value6 = {}
424
+ $value7 = "x${server_facts['environment']}y"
425
+
426
+ if someothercondition { include ::otherclass }
427
+ if somecondition {
428
+ class { 'example2':
429
+ param1 => 'value1',
430
+ require => File['somefile'],
431
+ }
432
+ class { 'example3': }
433
+ }
434
+ }
435
+ EOF
436
+ )
437
+ end
438
+ end
439
+ end
440
+ end
@@ -0,0 +1,277 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'manifest_whitespace_closing_bracket_before' do
6
+ let(:closing_bracket_msg) { 'there should be no whitespace or a single newline before a closing bracket' }
7
+
8
+ context 'with no spaces' do
9
+ let(:code) do
10
+ <<~EOF
11
+ # example
12
+ #
13
+ # Main class, includes all other classes.
14
+ #
15
+
16
+ class example (
17
+ String $content,
18
+ ) {
19
+ $value = [{ 'key' => 'value' }]
20
+ $value2 = [
21
+ {
22
+ 'key' => 'value1',
23
+ },
24
+ {
25
+ 'key' => 'value2',
26
+ },
27
+ ]
28
+ $value3 = myfunc($value1)
29
+ $value4 = ['somekey']
30
+ $value5 = []
31
+ $value6 = {}
32
+ $value7 = "x${server_facts['environment']}y"
33
+
34
+ if somecondition {
35
+ class { 'example2':
36
+ param1 => 'value1',
37
+ require => File['somefile'],
38
+ }
39
+ }
40
+ }
41
+ EOF
42
+ end
43
+
44
+ context 'with fix disabled' do
45
+ it 'should detect 0 problems' do
46
+ expect(problems).to have(0).problem
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'with too many spaces' do
52
+ let(:code) do
53
+ <<~EOF
54
+ # example
55
+ #
56
+ # Main class, includes all other classes.
57
+ #
58
+
59
+ class example (
60
+ String $content,
61
+ ) {
62
+ $value = [{ 'key' => 'value' } ]
63
+ $value2 = [
64
+ {
65
+ 'key' => 'value1',
66
+ },
67
+ {
68
+ 'key' => 'value2',
69
+ },
70
+
71
+ ]
72
+ $value3 = myfunc($value1)
73
+ $value4 = ['somekey' ]
74
+ $value5 = [ ]
75
+ $value6 = {}
76
+ $value7 = "x${server_facts['environment']}y"
77
+
78
+ if somecondition {
79
+ class { 'example2':
80
+ param1 => 'value1',
81
+ require => File['somefile' ],
82
+ }
83
+ }
84
+ }
85
+ EOF
86
+ end
87
+
88
+ context 'with fix disabled' do
89
+ it 'should detect 5 problems' do
90
+ expect(problems).to have(5).problems
91
+ end
92
+
93
+ it 'should create a error' do
94
+ expect(problems).to contain_error(closing_bracket_msg).on_line(9).in_column(33)
95
+ end
96
+ end
97
+
98
+ context 'with fix enabled' do
99
+ before do
100
+ PuppetLint.configuration.fix = true
101
+ end
102
+
103
+ after do
104
+ PuppetLint.configuration.fix = false
105
+ end
106
+
107
+ it 'should detect 5 problems' do
108
+ expect(problems).to have(5).problems
109
+ end
110
+
111
+ it 'should fix a error' do
112
+ expect(problems).to contain_fixed(closing_bracket_msg)
113
+ end
114
+
115
+ it 'should add spaces' do
116
+ expect(manifest).to eq(
117
+ <<~EOF,
118
+ # example
119
+ #
120
+ # Main class, includes all other classes.
121
+ #
122
+
123
+ class example (
124
+ String $content,
125
+ ) {
126
+ $value = [{ 'key' => 'value' }]
127
+ $value2 = [
128
+ {
129
+ 'key' => 'value1',
130
+ },
131
+ {
132
+ 'key' => 'value2',
133
+ },
134
+ ]
135
+ $value3 = myfunc($value1)
136
+ $value4 = ['somekey']
137
+ $value5 = []
138
+ $value6 = {}
139
+ $value7 = "x${server_facts['environment']}y"
140
+
141
+ if somecondition {
142
+ class { 'example2':
143
+ param1 => 'value1',
144
+ require => File['somefile'],
145
+ }
146
+ }
147
+ }
148
+ EOF
149
+ )
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ describe 'manifest_whitespace_closing_bracket_after' do
156
+ let(:closing_bracket_msg) { 'there should be either a bracket, comma, colon, closing quote or a newline after a closing bracket, or whitespace and none of the aforementioned' }
157
+
158
+ context 'with spaces' do
159
+ let(:code) do
160
+ <<~EOF
161
+ # example
162
+ #
163
+ # Main class, includes all other classes.
164
+ #
165
+
166
+ class example (
167
+ String $content,
168
+ ) {
169
+ $value = { 'key' => ['value'] }
170
+ $value2 = [
171
+ {
172
+ 'key' => 'value1',
173
+ },
174
+ {
175
+ 'key' => 'value2',
176
+ } ,
177
+ ]
178
+
179
+ $value2bis = [
180
+ 'value',
181
+ ] # this comment is fine
182
+
183
+ $value3 = myfunc([] )
184
+ $value4 = ['somekey']
185
+ $value5 = []
186
+ $value6 = {}
187
+ $value7 = "x${server_facts['environment']}y"
188
+
189
+ if somecondition {
190
+ class { 'example2':
191
+ param1 => 'value1',
192
+ require => File['somefile'] ,
193
+ }
194
+ class { 'example3': }
195
+ }
196
+ if someothercondition { include ::otherclass }
197
+
198
+ package { ['package1', 'package2']:
199
+ ensure => installed,
200
+ }
201
+ }
202
+ EOF
203
+ end
204
+
205
+ context 'with fix disabled' do
206
+ it 'should detect 3 problems' do
207
+ expect(problems).to have(3).problem
208
+ end
209
+
210
+ it 'should create a error' do
211
+ expect(problems).to contain_error(closing_bracket_msg).on_line(9).in_column(32)
212
+ end
213
+ end
214
+
215
+ context 'with fix enabled' do
216
+ before do
217
+ PuppetLint.configuration.fix = true
218
+ end
219
+
220
+ after do
221
+ PuppetLint.configuration.fix = false
222
+ end
223
+
224
+ it 'should fix a error' do
225
+ expect(problems).to contain_fixed(closing_bracket_msg)
226
+ end
227
+
228
+ it 'should add spaces' do
229
+ expect(manifest).to eq(
230
+ <<~EOF,
231
+ # example
232
+ #
233
+ # Main class, includes all other classes.
234
+ #
235
+
236
+ class example (
237
+ String $content,
238
+ ) {
239
+ $value = { 'key' => ['value']}
240
+ $value2 = [
241
+ {
242
+ 'key' => 'value1',
243
+ },
244
+ {
245
+ 'key' => 'value2',
246
+ } ,
247
+ ]
248
+
249
+ $value2bis = [
250
+ 'value',
251
+ ] # this comment is fine
252
+
253
+ $value3 = myfunc([])
254
+ $value4 = ['somekey']
255
+ $value5 = []
256
+ $value6 = {}
257
+ $value7 = "x${server_facts['environment']}y"
258
+
259
+ if somecondition {
260
+ class { 'example2':
261
+ param1 => 'value1',
262
+ require => File['somefile'],
263
+ }
264
+ class { 'example3': }
265
+ }
266
+ if someothercondition { include ::otherclass }
267
+
268
+ package { ['package1', 'package2']:
269
+ ensure => installed,
270
+ }
271
+ }
272
+ EOF
273
+ )
274
+ end
275
+ end
276
+ end
277
+ end