puppet-lint-manifest_whitespace-check 0.0.1 → 0.1.6

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