puppet-lint-unquoted_string-check 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 291b64ffce98ef8d006f793f70313b4e12447a0f
4
- data.tar.gz: 177deb1ca1f4df8bd46eddda2438da2ead2c91d8
3
+ metadata.gz: 40bb27f1ef50991d0fbdf1d76f5e4d5071e3c720
4
+ data.tar.gz: f7ed70c0b3c8e374ba3f42018a2ce43d40606348
5
5
  SHA512:
6
- metadata.gz: 2669575654bb0f7952d7acc64444f104a2b5c1330b5eb0dde5da50a36e665104b1dd58de47c8e24457095c9ed5d2e3259c8cd1089902967741dba2c51dc0c250
7
- data.tar.gz: bcb7f8f6413520d8b0b273b88cf4c1f4f1a06ba635b6418415a0c9dbf4d3186c0e5049899778d3b7e24efce2d4af00823c59da7488fa800a429fe72a37da19c1
6
+ metadata.gz: e2266dcab6a9cfc29b5e9e9b09514106ba8fd52ed7b250b008395de1bb596f3eea96f7a5c90e618d79b161ae68963c2e8bc13e652791e370c0b07a7fe3d8978c
7
+ data.tar.gz: dcbb50f022a8c71a1e093d3cd25c4b39c4f92b5e1d288c1bc1ffeaa9693fd7a8007ec9fec2ac5be4637550d86052eb8ef4ee970db3fd3ab4779c474aad1c4256
@@ -35,6 +35,34 @@ PuppetLint.new_check(:unquoted_string_in_case) do
35
35
  end
36
36
  end
37
37
  end
38
+
39
+ def fix(problem)
40
+ case_indexes = []
41
+
42
+ tokens.each_index do |token_idx|
43
+ if tokens[token_idx].type == :CASE
44
+ depth = 0
45
+ tokens[(token_idx + 1)..-1].each_index do |case_token_idx|
46
+ idx = case_token_idx + token_idx + 1
47
+ if tokens[idx].type == :LBRACE
48
+ depth += 1
49
+ elsif tokens[idx].type == :RBRACE
50
+ depth -= 1
51
+ if depth == 0
52
+ case_indexes << {:start => token_idx, :end => idx}
53
+ break
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ case_indexes.each do |kase|
61
+ case_tokens = tokens[kase[:start]..kase[:end]]
62
+
63
+ case_tokens.index { |r| r.type = :SSTRING if r.type == :NAME || r.type == :CLASSREF }
64
+ end
65
+ end
38
66
  end
39
67
 
40
68
  PuppetLint.new_check(:unquoted_string_in_selector) do
@@ -72,4 +100,32 @@ PuppetLint.new_check(:unquoted_string_in_selector) do
72
100
  end
73
101
  end
74
102
  end
103
+
104
+ def fix(problem)
105
+ qmark_indexes = []
106
+
107
+ tokens.each_index do |token_idx|
108
+ if tokens[token_idx].type == :QMARK
109
+ depth = 0
110
+ tokens[(token_idx + 1)..-1].each_index do |case_token_idx|
111
+ idx = case_token_idx + token_idx + 1
112
+ if tokens[idx].type == :LBRACE
113
+ depth += 1
114
+ elsif tokens[idx].type == :RBRACE
115
+ depth -= 1
116
+ if depth == 0
117
+ qmark_indexes << {:start => token_idx, :end => idx}
118
+ break
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ qmark_indexes.each do |kase|
126
+ qmark_tokens = tokens[kase[:start]..kase[:end]]
127
+
128
+ qmark_tokens.index { |r| r.type = :SSTRING if r.type == :NAME || r.type == :CLASSREF }
129
+ end
130
+ end
75
131
  end
@@ -70,4 +70,128 @@ describe 'unquoted_string_in_case' do
70
70
  end
71
71
  end
72
72
  end
73
+
74
+ context 'with fix enabled' do
75
+ before do
76
+ PuppetLint.configuration.fix = true
77
+ end
78
+
79
+ after do
80
+ PuppetLint.configuration.fix = false
81
+ end
82
+
83
+ context 'quoted case' do
84
+ let(:code) do
85
+ <<-EOS
86
+ case $osfamily {
87
+ 'Solaris': {
88
+ $rootgroup = 'wheel'
89
+ }
90
+ /(Darwin|FreeBSD)/: {
91
+ $rootgroup = 'wheel'
92
+ }
93
+ default: {
94
+ $rootgroup = 'root'
95
+ }
96
+ }
97
+ EOS
98
+ end
99
+
100
+ it 'should not detect any problems' do
101
+ expect(problems).to have(0).problems
102
+ end
103
+
104
+ it 'should not modify the manifest' do
105
+ expect(manifest).to eq(code)
106
+ end
107
+ end
108
+
109
+ context ':NAME in case' do
110
+ let(:code) do
111
+ <<-EOS
112
+ case $osfamily {
113
+ solaris: {
114
+ $rootgroup = 'wheel'
115
+ }
116
+ /(Darwin|FreeBSD)/: {
117
+ $rootgroup = 'wheel'
118
+ }
119
+ default: {
120
+ $rootgroup = 'root'
121
+ }
122
+ }
123
+ EOS
124
+ end
125
+
126
+ it 'should only detect a single problem' do
127
+ expect(problems).to have(1).problem
128
+ end
129
+
130
+ it 'should fix the problem' do
131
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(9)
132
+ end
133
+
134
+ it 'should quote the case statement' do
135
+ expect(manifest).to eq(
136
+ <<-EOS
137
+ case $osfamily {
138
+ 'solaris': {
139
+ $rootgroup = 'wheel'
140
+ }
141
+ /(Darwin|FreeBSD)/: {
142
+ $rootgroup = 'wheel'
143
+ }
144
+ default: {
145
+ $rootgroup = 'root'
146
+ }
147
+ }
148
+ EOS
149
+ )
150
+ end
151
+ end
152
+
153
+ context ':CLASSREF in case' do
154
+ let(:code) do
155
+ <<-EOS
156
+ case $osfamily {
157
+ Solaris: {
158
+ $rootgroup = 'wheel'
159
+ }
160
+ /(Darwin|FreeBSD)/: {
161
+ $rootgroup = 'wheel'
162
+ }
163
+ default: {
164
+ $rootgroup = 'root'
165
+ }
166
+ }
167
+ EOS
168
+ end
169
+
170
+ it 'should only detect a single problem' do
171
+ expect(problems).to have(1).problem
172
+ end
173
+
174
+ it 'should fix the problem' do
175
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(9)
176
+ end
177
+
178
+ it 'should quote the case statement' do
179
+ expect(manifest).to eq(
180
+ <<-EOS
181
+ case $osfamily {
182
+ 'Solaris': {
183
+ $rootgroup = 'wheel'
184
+ }
185
+ /(Darwin|FreeBSD)/: {
186
+ $rootgroup = 'wheel'
187
+ }
188
+ default: {
189
+ $rootgroup = 'root'
190
+ }
191
+ }
192
+ EOS
193
+ )
194
+ end
195
+ end
196
+ end
73
197
  end
@@ -52,4 +52,98 @@ describe 'unquoted_string_in_selector' do
52
52
  end
53
53
  end
54
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
+ context 'quoted case' do
66
+ let(:code) do
67
+ <<-EOS
68
+ $rootgroup = $osfamily ? {
69
+ 'Solaris' => 'wheel',
70
+ /(Darwin|FreeBSD)/ => 'wheel',
71
+ default => 'root',
72
+ }
73
+ EOS
74
+ end
75
+
76
+ it 'should not detect any problems' do
77
+ expect(problems).to have(0).problems
78
+ end
79
+
80
+ it 'should not modify the manifest' do
81
+ expect(manifest).to eq(code)
82
+ end
83
+ end
84
+
85
+ context ':NAME in case' do
86
+ let(:code) do
87
+ <<-EOS
88
+ $rootgroup = $osfamily ? {
89
+ solaris => 'wheel',
90
+ /(Darwin|FreeBSD)/ => 'wheel',
91
+ default => 'root',
92
+ }
93
+ EOS
94
+ end
95
+
96
+ it 'should only detect a single problem' do
97
+ expect(problems).to have(1).problem
98
+ end
99
+
100
+ it 'should fix the problem' do
101
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(32)
102
+ end
103
+
104
+ it 'should quote the case statement' do
105
+ expect(manifest).to eq(
106
+ <<-EOS
107
+ $rootgroup = $osfamily ? {
108
+ 'solaris' => 'wheel',
109
+ /(Darwin|FreeBSD)/ => 'wheel',
110
+ default => 'root',
111
+ }
112
+ EOS
113
+ )
114
+ end
115
+ end
116
+
117
+ context ':CLASSREF in case' do
118
+ let(:code) do
119
+ <<-EOS
120
+ $rootgroup = $osfamily ? {
121
+ Solaris => 'wheel',
122
+ /(Darwin|FreeBSD)/ => 'wheel',
123
+ default => 'root',
124
+ }
125
+ EOS
126
+ end
127
+
128
+ it 'should only detect a single problem' do
129
+ expect(problems).to have(1).problem
130
+ end
131
+
132
+ it 'should fix the problem' do
133
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(32)
134
+ end
135
+
136
+ it 'should quote the case statement' do
137
+ expect(manifest).to eq(
138
+ <<-EOS
139
+ $rootgroup = $osfamily ? {
140
+ 'Solaris' => 'wheel',
141
+ /(Darwin|FreeBSD)/ => 'wheel',
142
+ default => 'root',
143
+ }
144
+ EOS
145
+ )
146
+ end
147
+ end
148
+ end
55
149
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-unquoted_string-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickaël Canévet