puppet-lint-class_parameter-check 0.0.2 → 0.1.0
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c5361e935c4344c4564501bec96dcadf862bc2e
|
4
|
+
data.tar.gz: 204b0beec1bee3ce66024ebe875c5dd667bf2d49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cbced68e79a4d40abd5c346f94026cccae7587a422313e1cb53496502d3eba3626b21a57e3dd81c1cf4886353ee39171a4f0d992173f326e1bd3b958726ca22
|
7
|
+
data.tar.gz: e5f3c4cfeac458dd3a10003ff6656c6abd2564952af1d3c8a30b8255a32ae09738540f264541dd80ea6ba535a13f54f4652f314c48dd0ec36395cddd71e8ebeb
|
data/README.md
CHANGED
@@ -10,4 +10,4 @@ To use this plugin, add the following like to the Gemfile in your Puppet code ba
|
|
10
10
|
gem 'puppet-lint-class_parameter-check'
|
11
11
|
```
|
12
12
|
## Usage
|
13
|
-
This plugin provides a new check to `puppet-lint`.
|
13
|
+
This plugin provides a new check to `puppet-lint`. It supports the `--fix` option.
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class ClassParameter
|
2
|
+
attr_reader :tokens
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@tokens = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def is_optional?
|
9
|
+
tokens.any? { |token| token.type == :EQUALS }
|
10
|
+
end
|
11
|
+
|
12
|
+
def is_required?
|
13
|
+
!is_optional?
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
tokens.select do |token|
|
18
|
+
token.type == :VARIABLE
|
19
|
+
end.first.value
|
20
|
+
end
|
21
|
+
|
22
|
+
def add(token)
|
23
|
+
tokens << token
|
24
|
+
end
|
25
|
+
|
26
|
+
def line
|
27
|
+
tokens.first.line
|
28
|
+
end
|
29
|
+
|
30
|
+
def column
|
31
|
+
tokens.first.column
|
32
|
+
end
|
33
|
+
|
34
|
+
def <=>(other)
|
35
|
+
if (self.is_optional? && other.is_optional?) || (self.is_required? && other.is_required?)
|
36
|
+
return self.name <=> other.name
|
37
|
+
elsif self.is_optional? && other.is_required?
|
38
|
+
return 1
|
39
|
+
else
|
40
|
+
return -1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'class_parameter'
|
2
|
+
|
3
|
+
class ClassParameterList
|
4
|
+
attr_reader :errors, :start_index, :end_index
|
5
|
+
|
6
|
+
def initialize(tokens, start_index, end_index)
|
7
|
+
@tokens, @start_index, @end_index = tokens, start_index, end_index
|
8
|
+
@errors = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def sort
|
12
|
+
parameters.sort
|
13
|
+
end
|
14
|
+
|
15
|
+
def optional_parameters
|
16
|
+
parameters.select(&:is_optional?)
|
17
|
+
end
|
18
|
+
|
19
|
+
def required_parameters
|
20
|
+
parameters.select(&:is_required?)
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate
|
24
|
+
optional_parameter_found = false
|
25
|
+
|
26
|
+
parameters.each do |parameter|
|
27
|
+
if parameter.is_required? && optional_parameter_found
|
28
|
+
errors << {
|
29
|
+
:message => "Required parameter #{parameter.name} should be specified before optional parameters",
|
30
|
+
:line => parameter.line,
|
31
|
+
:column => parameter.column
|
32
|
+
}
|
33
|
+
elsif parameter.is_optional?
|
34
|
+
optional_parameter_found = true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
validate_alphabetical_order(required_parameters)
|
39
|
+
validate_alphabetical_order(optional_parameters)
|
40
|
+
|
41
|
+
errors.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
private
|
46
|
+
def parameters
|
47
|
+
parameter = ClassParameter.new
|
48
|
+
|
49
|
+
@tokens.inject([]) do |memo, token|
|
50
|
+
if (token.type == :COMMA || token == @tokens.last) && parameter.tokens.any?
|
51
|
+
# always add a comma and a newline token at the end of each parameter
|
52
|
+
parameter.add(PuppetLint::Lexer::Token.new(:COMMA, ",", 0,0))
|
53
|
+
parameter.add(PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0,0))
|
54
|
+
memo << parameter
|
55
|
+
parameter = ClassParameter.new
|
56
|
+
elsif token.type != :NEWLINE
|
57
|
+
parameter.add(token)
|
58
|
+
end
|
59
|
+
memo
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def validate_alphabetical_order(params)
|
64
|
+
if params != params.sort
|
65
|
+
errors << {
|
66
|
+
:message => "Parameter list not in alphabetical order",
|
67
|
+
:line => params.first.line,
|
68
|
+
:column => params.first.column
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -1,41 +1,43 @@
|
|
1
|
+
require_relative '../../model/class_parameter_list'
|
2
|
+
|
1
3
|
PuppetLint.new_check(:class_parameter) do
|
2
4
|
def check
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
class_parameter_lists.each do |class_parameter_list|
|
6
|
+
unless class_parameter_list.validate
|
7
|
+
class_parameter_list.errors.each { |error| notify :error, error }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
def fix(problem)
|
13
|
+
resorted_tokens = []
|
14
|
+
token_index = 0
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
:message => "Required parameter #{param_token.value} should be specified before optional parameters",
|
15
|
-
:line => param_token.line,
|
16
|
-
:column => param_token.column
|
17
|
-
} if optional_params.any?
|
16
|
+
class_parameter_lists.each do |class_parameter_list|
|
17
|
+
resorted_tokens += tokens[token_index..class_parameter_list.start_index]
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
optional_params.push(param_token)
|
22
|
-
end
|
19
|
+
class_parameter_list.sort.each do |parameter|
|
20
|
+
resorted_tokens += parameter.tokens
|
23
21
|
end
|
24
22
|
|
25
|
-
|
26
|
-
check_alphabetical_order(optional_params)
|
23
|
+
token_index = class_parameter_list.end_index
|
27
24
|
end
|
25
|
+
|
26
|
+
resorted_tokens += tokens[token_index..-1]
|
27
|
+
tokens.replace(resorted_tokens)
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
30
|
+
private
|
31
|
+
def class_parameter_lists
|
32
|
+
lists = []
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
}
|
34
|
+
class_indexes.each do |class_index|
|
35
|
+
param_tokens = class_index[:param_tokens]
|
36
|
+
next if param_tokens.nil?
|
37
|
+
|
38
|
+
lists << ClassParameterList.new(param_tokens, tokens.index(param_tokens.first), tokens.index(param_tokens.last))
|
39
39
|
end
|
40
|
+
|
41
|
+
lists
|
40
42
|
end
|
41
43
|
end
|
@@ -134,4 +134,89 @@ describe 'class_parameter' do
|
|
134
134
|
end
|
135
135
|
end
|
136
136
|
end
|
137
|
+
|
138
|
+
context 'with fix enabled' do
|
139
|
+
before do
|
140
|
+
PuppetLint.configuration.fix = true
|
141
|
+
end
|
142
|
+
|
143
|
+
after do
|
144
|
+
PuppetLint.configuration.fix = false
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'class with no parameters' do
|
148
|
+
let(:code) { "class puppet_module() { }" }
|
149
|
+
|
150
|
+
it 'does not change the code' do
|
151
|
+
expect(manifest).to eq(code)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'class sorted alphabetically' do
|
156
|
+
let(:code) { <<-EOF
|
157
|
+
class puppet_module(
|
158
|
+
String $alphabetical,
|
159
|
+
String $non_alphabetical,
|
160
|
+
) { }
|
161
|
+
EOF
|
162
|
+
}
|
163
|
+
it 'does not change the code' do
|
164
|
+
expect(manifest).to eq(code)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context 'multiple classes not sorted alphabetically' do
|
169
|
+
let(:code) { <<-EOF
|
170
|
+
class puppet_module(
|
171
|
+
String $non_alphabetical,
|
172
|
+
String $alphabetical
|
173
|
+
) { }
|
174
|
+
|
175
|
+
class puppet_module2(
|
176
|
+
String $non_alphabetical,
|
177
|
+
String $alphabetical
|
178
|
+
) { }
|
179
|
+
EOF
|
180
|
+
}
|
181
|
+
|
182
|
+
it 'fixes the problem' do
|
183
|
+
expect(manifest).to eq(<<-EOF
|
184
|
+
class puppet_module(
|
185
|
+
String $alphabetical,
|
186
|
+
String $non_alphabetical,
|
187
|
+
) { }
|
188
|
+
|
189
|
+
class puppet_module2(
|
190
|
+
String $alphabetical,
|
191
|
+
String $non_alphabetical,
|
192
|
+
) { }
|
193
|
+
EOF
|
194
|
+
)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'not sorted in groups and not alphabetically' do
|
199
|
+
let(:code) { <<-EOF
|
200
|
+
class puppet_module(
|
201
|
+
String $non_alphabetical,
|
202
|
+
String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional,
|
203
|
+
String $alphabetical,
|
204
|
+
String $alphabetical_optional = "default"
|
205
|
+
) inherits puppet_module::params { }
|
206
|
+
EOF
|
207
|
+
}
|
208
|
+
|
209
|
+
it 'fixes the problem' do
|
210
|
+
expect(manifest).to eq(<<-EOF
|
211
|
+
class puppet_module(
|
212
|
+
String $alphabetical,
|
213
|
+
String $non_alphabetical,
|
214
|
+
String $alphabetical_optional = "default",
|
215
|
+
String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional,
|
216
|
+
) inherits puppet_module::params { }
|
217
|
+
EOF
|
218
|
+
)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
137
222
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puppet-lint-class_parameter-check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roelof Reitsma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: puppet-lint
|
@@ -91,6 +91,8 @@ extra_rdoc_files: []
|
|
91
91
|
files:
|
92
92
|
- README.md
|
93
93
|
- LICENSE
|
94
|
+
- lib/model/class_parameter.rb
|
95
|
+
- lib/model/class_parameter_list.rb
|
94
96
|
- lib/puppet-lint/plugins/check_class_parameter.rb
|
95
97
|
- spec/puppet-lint/plugins/check_class_parameter_spec.rb
|
96
98
|
- spec/spec_helper.rb
|