puppet-lint-array_formatting-check 0.9.0 → 0.11.0

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: c130a6b549e23b5a62141163214c8dc669e2ec4cf4e2969773a853bf490d60e7
4
- data.tar.gz: d4233449808f9190aa3683639a99586ad89417280c370c256262d8af40a3e8aa
3
+ metadata.gz: 253a737a788c7d2d46e0452562fa67d5177f096204039fb294f98633c5435b70
4
+ data.tar.gz: 44a81890888cd86b28cd8ba156b311f2e370b6a02f6a5ba3a1d8db74e87652aa
5
5
  SHA512:
6
- metadata.gz: fee2c7bccde0fcf12cd2e1187556cc5ba2531ce7063f266da5fc309de07e01236f5d9e696991d3af19e824b54b2b7892a9e4c44b8f9a21f0b7379a086c876805
7
- data.tar.gz: 6e198fc7fa4705566971abe7a486af1653cf43e3c85ffb0794c61e003528655358a444b64f727cbf5cf7da67a6bbf46571b4524d3b4aa1d7794406d1e7d41f70
6
+ metadata.gz: 927806a62b811ac503f789a3f9a7eca73307bf162dc16c42eb78f5368983732dcf3ebf1d9df8c7aec2261a2b374c032e09c9364c85871732415a14f48e97f8a5
7
+ data.tar.gz: 13c068cb3b5a180d3f0c42dc799686343bed9eae54c450e4ed8d307859f21efb50ac8ce2ed343070032bb2bd73a7d0746807cb20db8726e42754e98a9dec9a20
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # puppet-lint array formatting check
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/puppet-lint-array_formatting-check.svg)](https://badge.fury.io/rb/puppet-lint-array_formatting-check)
4
+
3
5
  A new check for puppet-lint that ensures array formatting conforms to the [Puppet Style Guide](https://puppet.com/docs/puppet/6.0/style_guide.html#arrays-and-hashes) on arrays; namely that each element should be on a separate line.
4
6
 
5
7
  It will take an array formatted as
@@ -57,13 +59,13 @@ This plugin provides a new check to `puppet-lint`.
57
59
  This check will raise warnings for every element in an array that should be on its own line
58
60
 
59
61
  ```
60
- WARNING: expected newline before element
62
+ WARNING: expected newline before element on line 3
61
63
  ```
62
64
 
63
- It will also flag instances of extra newlines in arrays
65
+ It will also raise warnings on extra (unnecessary) newlines in arrays
64
66
 
65
67
  ```
66
- WARNING: unexpected newline
68
+ WARNING: unexpected newline on line 4
67
69
  ```
68
70
 
69
71
  ### Author
@@ -1,3 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ EXPECTED_NEWLINE_BEFORE_ELEMENT = 'expected newline before element'
4
+ EXPECTED_NEWLINE_BEFORE_CLOSE = 'expected newline before close'
5
+ UNEXPECTED_NEWLINE = 'unexpected newline'
6
+ PARSING_ERROR = 'parsing error'
1
7
  PuppetLint.new_check(:array_formatting) do
2
8
  def process_array
3
9
  # When we arrive here, the @manifest enumerator is "pointing" to
@@ -18,9 +24,19 @@ PuppetLint.new_check(:array_formatting) do
18
24
  # Recursively process this nested array.
19
25
  # Store return so we know if the child array was processed or not.
20
26
  len_of_nested_array = process_array
21
- # If we encounter something _other_ than COMMA, WHITESPACE, or INDENT, push it.
22
- # These don't include NEWLINE(s), which is what we need to keep track of.
23
- elsif !%i[COMMA WHITESPACE INDENT].include?(token.type)
27
+ # If we encounter a DQPRE, we need to scan ahead until we find the matching DQPOST
28
+ elsif [:DQPRE].include?(token.type)
29
+ # Tight loop until we find a DQPOST, or a Nil in case of an unterminated string
30
+ until [:DQPOST].include?(token.type) || token.nil?
31
+ # Advance. We're still looking for that DQPOST
32
+ token = @manifest.next
33
+ end
34
+ # Store the DQPOST
35
+ manifest_array.push token
36
+ # We're really only keeping track of array *items* here for the sake of the length calculation.
37
+ # We're going to ignore everything that doesn't count as an *item*, like commas, comments, and whitespaces
38
+ # We need to push NEWLINE(s) though because there might be too many
39
+ elsif !%i[COMMA COMMENT WHITESPACE INDENT].include?(token.type)
24
40
  manifest_array.push token
25
41
  end
26
42
  # Advance. (We're still looking for that RBRACK)
@@ -50,7 +66,7 @@ PuppetLint.new_check(:array_formatting) do
50
66
  elsif !prev_token_was_newline && ![:NEWLINE].include?(manifest_array[x].type)
51
67
  # We were expecting a newline. :-(
52
68
  notify :warning,
53
- message: 'expected newline before element',
69
+ message: EXPECTED_NEWLINE_BEFORE_ELEMENT,
54
70
  line: manifest_array[x].line,
55
71
  column: manifest_array[x].column,
56
72
  token: manifest_array[x]
@@ -59,7 +75,7 @@ PuppetLint.new_check(:array_formatting) do
59
75
  elsif prev_token_was_newline && [:NEWLINE].include?(manifest_array[x].type)
60
76
  # We got an extra newline. Let's fix that up while we're at it.
61
77
  notify :warning,
62
- message: 'unexpected newline',
78
+ message: UNEXPECTED_NEWLINE,
63
79
  line: manifest_array[x].line,
64
80
  column: manifest_array[x].column,
65
81
  token: manifest_array[x]
@@ -68,7 +84,7 @@ PuppetLint.new_check(:array_formatting) do
68
84
  else
69
85
  # Something has gone horribly wrong. The truth table says we should never be here.
70
86
  notify :error,
71
- message: 'parsing error',
87
+ message: PARSING_ERROR,
72
88
  line: manifest_array[x].line,
73
89
  column: manifest_array[x].column
74
90
  end
@@ -79,9 +95,9 @@ PuppetLint.new_check(:array_formatting) do
79
95
  # If the last token (not including the RBRACK) wasn't a newline, throw a warning.
80
96
  # Since we previously used token to scan forward to the ],
81
97
  # token is already pointing to the offending ].
82
- if !prev_token_was_newline # rubocop:disable NegatedIf "I find this clearer than `unless prev_token_was_newline`"
98
+ if !prev_token_was_newline # rubocop:disable Style/NegatedIf "I find this clearer than `unless prev_token_was_newline`"
83
99
  notify :warning,
84
- message: 'expected newline before close',
100
+ message: EXPECTED_NEWLINE_BEFORE_CLOSE,
85
101
  line: token.line,
86
102
  column: token.column,
87
103
  token: token
@@ -108,16 +124,16 @@ PuppetLint.new_check(:array_formatting) do
108
124
  def fix(problem)
109
125
  token_index = tokens.find_index(problem[:token])
110
126
  case problem[:message]
111
- when 'parsing error'
127
+ when PARSING_ERROR
112
128
  # Truth table says we should never get here
113
129
  raise PuppetLint::NoFix
114
- when 'unexpected newline'
130
+ when UNEXPECTED_NEWLINE
115
131
  # If we delete the INDENT first, we change the index of the offending token,
116
132
  # so we need to do the token first, then the INDENT
117
133
  tokens.delete_at(token_index)
118
134
  # If the previous token is an Indent, we'll want to delete that too
119
135
  tokens.delete_at(token_index - 1) if [:INDENT].include?(problem[:token].prev_token.type)
120
- when 'expected newline before element', 'expected newline before close'
136
+ when EXPECTED_NEWLINE_BEFORE_ELEMENT, EXPECTED_NEWLINE_BEFORE_CLOSE
121
137
  # If the preceeding token is a whitespace, replace it with a newline instead
122
138
  # so that we don't end up with trailing whitespaces
123
139
  if %i[WHITESPACE INDENT].include?(problem[:token].prev_token.type)
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'array_formatting' do
6
+ let(:msg) { 'expected newline before element' }
7
+ let(:msg_end) { 'expected newline before close' }
8
+ let(:msg_newline) { 'unexpected newline' }
9
+
10
+ context 'with fix disabled' do
11
+ context 'on code with an empty array' do
12
+ let(:code) { '$services = []' }
13
+
14
+ it 'should detect no problem' do
15
+ expect(problems).to have(0).problems
16
+ end
17
+ end
18
+ end
19
+ context 'with fix enabled' do
20
+ before do
21
+ PuppetLint.configuration.fix = true
22
+ end
23
+
24
+ after do
25
+ PuppetLint.configuration.fix = false
26
+ end
27
+ context 'on code with an empty array' do
28
+ let(:code) { '$services = []' }
29
+
30
+ it 'should detect no problem' do
31
+ expect(problems).to have(0).problems
32
+ end
33
+
34
+ it 'should not modify the manifest' do
35
+ expect(manifest).to eq(code)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,5 @@
1
- # Rubocop has a problem with some of my examples.
2
- # Since they're Puppet code, inside a ruby file,
3
- # I'm going to blame different syntaxes.
4
- # rubocop:disable TrailingCommaInArguments
1
+ # frozen_string_literal: true
2
+
5
3
  require 'spec_helper'
6
4
 
7
5
  describe 'array_formatting' do
@@ -112,7 +110,7 @@ describe 'array_formatting' do
112
110
  end
113
111
  context 'on code with an array with extra newlines' do
114
112
  let(:code) do
115
- # rubocop:disable TrailingWhitespace
113
+ # rubocop:disable Layout/TrailingWhitespace
116
114
  <<-ARRAY.gsub(/^ {10}/, '')
117
115
  file{[
118
116
  '/etc',
@@ -125,7 +123,7 @@ describe 'array_formatting' do
125
123
  ensure => directory,
126
124
  }
127
125
  ARRAY
128
- # rubocop:enable TrailingWhitespace
126
+ # rubocop:enable Layout/TrailingWhitespace
129
127
  end
130
128
 
131
129
  it 'should detect 2 problems' do
@@ -383,6 +381,68 @@ describe 'array_formatting' do
383
381
  expect(problems).to contain_warning(msg_end).on_line(14).in_column(42)
384
382
  end
385
383
  end
384
+ context 'on a string with a properly formatted multidimensional hash (Issue #18)' do
385
+ let(:code) do
386
+ <<-CODE.gsub(/^ {10}/, '')
387
+ Apt::Source["yubihsm-${facts['os']['distro']['codename']}"],
388
+ CODE
389
+ end
390
+ it 'should detect no problem' do
391
+ expect(problems).to have(0).problems
392
+ end
393
+ end
394
+ context 'on a code snippet with a properly formatted multidimensional hash (Issue #18)' do
395
+ let(:code) do
396
+ <<-CODE.gsub(/^ {10}/, '')
397
+ package { $packages:
398
+ ensure => $version,
399
+ require => [
400
+ Class['apt::update'],
401
+ Apt::Source["yubihsm-${facts['os']['distro']['codename']}"],
402
+ ],
403
+ }
404
+ CODE
405
+ end
406
+ it 'should detect no problem' do
407
+ expect(problems).to have(0).problems
408
+ end
409
+ end
410
+ context 'on a string with a properly formatted variable (Issue #19)' do
411
+ let(:code) do
412
+ <<-CODE.gsub(/^ {10}/, '')
413
+ file { [
414
+ "${root_dir}/index.txt",
415
+ "${root_dir}/index.txt.attr"
416
+ ]:
417
+ ensure => file,
418
+ owner => 0,
419
+ group => 0,
420
+ mode => '0400',
421
+ }
422
+ CODE
423
+ end
424
+ it 'should detect no problem' do
425
+ expect(problems).to have(0).problems
426
+ end
427
+ end
428
+ context 'on an array with comments after some values (Issue #20)' do
429
+ let(:code) do
430
+ <<-CODE.gsub(/^ {10}/, '')
431
+ proxy_set_header => [
432
+ 'Host $http_host:8043',
433
+ 'X-Real-IP $remote_addr',
434
+ 'X-Forwarded-For $proxy_add_x_forwarded_for',
435
+ 'Cookie $http_cookie',
436
+ 'X-Forwarded-Proto $scheme',
437
+ 'Upgrade $http_upgrade', #websockets
438
+ 'Connection "Upgrade"', #websockets
439
+ ],
440
+ CODE
441
+ end
442
+ it 'should detect no problem' do
443
+ expect(problems).to have(0).problems
444
+ end
445
+ end
386
446
  end
387
447
  context 'with fix enabled' do
388
448
  before do
@@ -540,7 +600,7 @@ describe 'array_formatting' do
540
600
  end
541
601
  context 'on code with an array with extra newlines' do
542
602
  let(:code) do
543
- # rubocop:disable TrailingWhitespace
603
+ # rubocop:disable Layout/TrailingWhitespace
544
604
  <<-ARRAY.gsub(/^ {10}/, '')
545
605
  file{[
546
606
  '/etc',
@@ -553,7 +613,7 @@ describe 'array_formatting' do
553
613
  ensure => directory,
554
614
  }
555
615
  ARRAY
556
- # rubocop:enable TrailingWhitespace
616
+ # rubocop:enable Layout/TrailingWhitespace
557
617
  end
558
618
 
559
619
  it 'should detect 2 problems' do
@@ -1018,6 +1078,83 @@ describe 'array_formatting' do
1018
1078
  )
1019
1079
  end
1020
1080
  end
1081
+ context 'on a string with a properly formatted multidimensional hash (Issue #18)' do
1082
+ let(:code) do
1083
+ <<-CODE.gsub(/^ {10}/, '')
1084
+ Apt::Source["yubihsm-${facts['os']['distro']['codename']}"],
1085
+ CODE
1086
+ end
1087
+ it 'should detect no problem' do
1088
+ expect(problems).to have(0).problems
1089
+ end
1090
+
1091
+ it 'should not modify the manifest' do
1092
+ expect(manifest).to eq(code)
1093
+ end
1094
+ end
1095
+ context 'on a code snippet with a properly formatted multidimensional hash (Issue #18)' do
1096
+ let(:code) do
1097
+ <<-CODE.gsub(/^ {10}/, '')
1098
+ package { $packages:
1099
+ ensure => $version,
1100
+ require => [
1101
+ Class['apt::update'],
1102
+ Apt::Source["yubihsm-${facts['os']['distro']['codename']}"],
1103
+ ],
1104
+ }
1105
+ CODE
1106
+ end
1107
+ it 'should detect no problem' do
1108
+ expect(problems).to have(0).problems
1109
+ end
1110
+
1111
+ it 'should not modify the manifest' do
1112
+ expect(manifest).to eq(code)
1113
+ end
1114
+ end
1115
+ context 'on a string with a properly formatted variable (Issue #19)' do
1116
+ let(:code) do
1117
+ <<-CODE.gsub(/^ {10}/, '')
1118
+ file { [
1119
+ "${root_dir}/index.txt",
1120
+ "${root_dir}/index.txt.attr"
1121
+ ]:
1122
+ ensure => file,
1123
+ owner => 0,
1124
+ group => 0,
1125
+ mode => '0400',
1126
+ }
1127
+ CODE
1128
+ end
1129
+ it 'should detect no problem' do
1130
+ expect(problems).to have(0).problems
1131
+ end
1132
+
1133
+ it 'should not modify the manifest' do
1134
+ expect(manifest).to eq(code)
1135
+ end
1136
+ end
1137
+ context 'on an array with comments after some values (Issue #20)' do
1138
+ let(:code) do
1139
+ <<-CODE.gsub(/^ {10}/, '')
1140
+ proxy_set_header => [
1141
+ 'Host $http_host:8043',
1142
+ 'X-Real-IP $remote_addr',
1143
+ 'X-Forwarded-For $proxy_add_x_forwarded_for',
1144
+ 'Cookie $http_cookie',
1145
+ 'X-Forwarded-Proto $scheme',
1146
+ 'Upgrade $http_upgrade', #websockets
1147
+ 'Connection "Upgrade"', #websockets
1148
+ ],
1149
+ CODE
1150
+ end
1151
+ it 'should detect no problem' do
1152
+ expect(problems).to have(0).problems
1153
+ end
1154
+
1155
+ it 'should not modify the manifest' do
1156
+ expect(manifest).to eq(code)
1157
+ end
1158
+ end
1021
1159
  end
1022
1160
  end
1023
- # rubocop:enable TrailingCommaInArguments
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simplecov'
2
- SimpleCov.start 'rails' do
4
+
5
+ SimpleCov.start do
3
6
  add_filter '/bin/'
4
7
  add_filter '/db/'
5
8
  add_filter '/spec/' # for rspec
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-array_formatting-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Manzer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-21 00:00:00.000000000 Z
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -16,104 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: 3.1.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '3'
22
+ version: '5'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '1.0'
29
+ version: 3.1.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '3'
33
- - !ruby/object:Gem::Dependency
34
- name: rake
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '12.0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '12.0'
47
- - !ruby/object:Gem::Dependency
48
- name: rspec
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '3.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '3.0'
61
- - !ruby/object:Gem::Dependency
62
- name: rspec-collection_matchers
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '1.0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '1.0'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec-its
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '1.0'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '1.0'
89
- - !ruby/object:Gem::Dependency
90
- name: rspec-json_expectations
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '2.0'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '2.0'
103
- - !ruby/object:Gem::Dependency
104
- name: simplecov
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '0.16'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '0.16'
32
+ version: '5'
117
33
  description: " A puppet-lint plugin to check that arrays comform to the style guideline
118
34
  of having one element per line.\n"
119
35
  email: amanzer@gmail.com
@@ -124,13 +40,15 @@ files:
124
40
  - LICENSE
125
41
  - README.md
126
42
  - lib/puppet-lint/plugins/check_array_formatting.rb
43
+ - spec/puppet-lint/plugins/check_array_formatting_selected_spec.rb
127
44
  - spec/puppet-lint/plugins/check_array_formatting_spec.rb
128
45
  - spec/spec_helper.rb
129
46
  homepage: https://gitlab.com/amanzer/puppet-lint-array_formatting-check
130
47
  licenses:
131
48
  - MIT
132
- metadata: {}
133
- post_install_message:
49
+ metadata:
50
+ rubygems_mfa_required: 'true'
51
+ post_install_message:
134
52
  rdoc_options: []
135
53
  require_paths:
136
54
  - lib
@@ -138,17 +56,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
56
  requirements:
139
57
  - - ">="
140
58
  - !ruby/object:Gem::Version
141
- version: '0'
59
+ version: '2.5'
142
60
  required_rubygems_version: !ruby/object:Gem::Requirement
143
61
  requirements:
144
62
  - - ">="
145
63
  - !ruby/object:Gem::Version
146
64
  version: '0'
147
65
  requirements: []
148
- rubygems_version: 3.0.3
149
- signing_key:
66
+ rubygems_version: 3.3.26
67
+ signing_key:
150
68
  specification_version: 4
151
69
  summary: A puppet-lint plugin to check array formatting.
152
- test_files:
153
- - spec/spec_helper.rb
154
- - spec/puppet-lint/plugins/check_array_formatting_spec.rb
70
+ test_files: []