puppet-lint-extended 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b52caed1cfd754a66dd8657f64364b630a08db3
4
+ data.tar.gz: 0f1a8757f542bf87a77b0625164caa2ab4cad93e
5
+ SHA512:
6
+ metadata.gz: 05e2977d293da5e2e95f7827027ad611c7168f169d77ab92eea3230a26f62beceef5f2cec02c3b4eb5938b6b5a0f04d936f23bf87eba01ffc09c2380b747df1f
7
+ data.tar.gz: 9260397fb170e0cc5a64293bbd67aba30b96094927692ac55aad729f101bc8a7a0b493b9d06ed82a1742705cf075aacd404bf4b65eb929602e5dae6b225368f8
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # puppet-lint-extended
2
+ Provides additional checks for [puppet-lint](https://github.com/rodjek/puppet-lint) that are not part of the puppet style guide but still make for good code.
3
+
4
+ ## List of the checks:
5
+
6
+ ### leading_comment_space
7
+ ```
8
+ # bad
9
+ #comment
10
+
11
+ # good
12
+ # comment
13
+
14
+ # allows
15
+ ########
16
+ ```
17
+
18
+
19
+ ### space_after_comma
20
+ ```
21
+ # bad
22
+ [foo,bar]
23
+
24
+ # good
25
+ [foo, bar]
26
+
27
+ # allows
28
+ [foo, bar,]
29
+ foo,;
30
+ ```
31
+ ### space_around_operator
32
+ ```
33
+ # bad
34
+ $foo='bar'
35
+ if $baz<1
36
+
37
+ # good
38
+ $foo = 'bar'
39
+ if $baz < 1
40
+ ```
41
+
42
+ ### space_inside_braces
43
+ ```
44
+ # bad
45
+ {foo:'bar'}
46
+
47
+ # good
48
+ { foo:'bar' }
49
+
50
+ # allows
51
+ {}
52
+ ```
@@ -0,0 +1,20 @@
1
+ # Checks that there is a space after comma
2
+ PuppetLint.new_check(:leading_comment_space) do
3
+ def check
4
+ tokens.select { |r| r.type == :COMMENT }.each do |token|
5
+ next if token.value =~ /^\#+$/
6
+
7
+ unless token.value =~ /^\s/
8
+ notify :warning,
9
+ message: "Add space after #",
10
+ line: token.line,
11
+ column: token.column,
12
+ token: token
13
+ end
14
+ end
15
+ end
16
+
17
+ def fix(problem)
18
+ problem[:token].value = ' ' + problem[:token].value
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ # Checks that there is a space after comma
2
+ PuppetLint.new_check(:space_after_comma) do
3
+ def check
4
+ tokens.select { |r| r.type == :COMMA }.each do |token|
5
+ if token.next_token && !allowed?(token.next_token.type)
6
+ next if last_comma_in_list?(token)
7
+ notify :warning,
8
+ message: "Add space after the comma",
9
+ line: token.line,
10
+ column: token.column,
11
+ token: token
12
+ end
13
+ end
14
+ end
15
+
16
+ def fix(problem)
17
+ index = tokens.index(problem[:token])
18
+ add_token(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
19
+ end
20
+
21
+ def allowed?(type)
22
+ [:WHITESPACE, :NEWLINE, :SEMIC].include?(type)
23
+ end
24
+
25
+ def last_comma_in_list?(token)
26
+ [:RBRACK, :RBRACE, :RPAREN].include?(token.next_token.type)
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ # Checks that operators have space around them
2
+ PuppetLint.new_check(:space_around_operators) do
3
+ OPERATORS = %i(EQUALS ISEQUAL NOTEQUAL MATCH APPENDS PLUS GREATEREQUAL
4
+ LESSEQUAL GREATERTHAN LESSTHAN NOMATCH).freeze
5
+
6
+ def check
7
+ tokens.select { |r| OPERATORS.include? r.type }.each do |token|
8
+ missing_space_on_left(token) do
9
+ display_error(token, "Surrounding space missing for operator #{token.value}")
10
+ return
11
+ end
12
+
13
+ missing_space_on_right(token) do
14
+ display_error(token, "Surrounding space missing for operator #{token.value}")
15
+ return
16
+ end
17
+ end
18
+ end
19
+
20
+ def fix(problem)
21
+ missing_space_on_left(problem[:token]) do
22
+ index = tokens.index(problem[:token])
23
+ add_token(index, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
24
+ end
25
+
26
+ missing_space_on_right(problem[:token]) do
27
+ index = tokens.index(problem[:token])
28
+ add_token(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
29
+ end
30
+ end
31
+
32
+ def missing_space_on_left(token)
33
+ yield if token.prev_token.type != :WHITESPACE
34
+ end
35
+
36
+ def missing_space_on_right(token)
37
+ yield unless [:WHITESPACE, :NEWLINE].include?(token.next_token.type)
38
+ end
39
+
40
+ def display_error(token, message)
41
+ notify :warning,
42
+ message: message,
43
+ line: token.line,
44
+ column: token.column,
45
+ token: token
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ # Checks that operators have space around them
2
+ PuppetLint.new_check(:space_inside_braces) do
3
+
4
+ def check
5
+ tokens.select { |r| [:LBRACE, :RBRACE].include? r.type }.each do |token|
6
+ missing_space_after_opening(token) do
7
+ display_error(token, "Add space after opening brace")
8
+ end
9
+
10
+ missing_space_before_closing(token) do
11
+ display_error(token, "Add space before closing brace")
12
+ end
13
+ end
14
+ end
15
+
16
+ def fix(problem)
17
+ missing_space_after_opening(problem[:token]) do
18
+ index = tokens.index(problem[:token])
19
+ add_token(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
20
+ end
21
+
22
+ missing_space_before_closing(problem[:token]) do
23
+ index = tokens.index(problem[:token])
24
+ add_token(index, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
25
+ end
26
+ end
27
+
28
+ def missing_space_after_opening(token)
29
+ return unless token.type == :LBRACE
30
+ return if token.next_token.nil? || token.next_token.type == :RBRACE
31
+ yield unless [:WHITESPACE, :NEWLINE, :INDENT].include?(token.next_token.type)
32
+ end
33
+
34
+ def missing_space_before_closing(token)
35
+ return unless token.type == :RBRACE
36
+ return if token.prev_token.nil? || token.prev_token.type == :LBRACE
37
+ yield unless [:WHITESPACE, :NEWLINE, :INDENT].include?(token.prev_token.type)
38
+ end
39
+
40
+ def display_error(token, message)
41
+ notify :warning,
42
+ message: message,
43
+ line: token.line,
44
+ column: token.column,
45
+ token: token
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'leading_comment_space' do
4
+ let(:message) { 'Add space after #' }
5
+
6
+ context 'with missing leading space in the comment' do
7
+ let(:code) { "#this should have a whitespace in the beginning" }
8
+
9
+ it { expect(problems).to contain_warning(message).on_line(1).in_column(1) }
10
+ end
11
+
12
+ context 'with leading space in the comment' do
13
+ let(:code) { "# this should have a whitespace in the beginning" }
14
+
15
+ it { expect(problems).to have(0).problems }
16
+ end
17
+
18
+ context 'with only #' do
19
+ let(:code) { "######" }
20
+
21
+ it { expect(problems).to have(0).problems }
22
+ end
23
+
24
+ context 'with fix enabled' do
25
+ before do
26
+ PuppetLint.configuration.fix = true
27
+ end
28
+
29
+ after do
30
+ PuppetLint.configuration.fix = false
31
+ end
32
+
33
+ let(:code) { "#this should have a whitespace in the beginning" }
34
+ let(:fixed) { "# this should have a whitespace in the beginning" }
35
+
36
+ it { expect(manifest).to eq(fixed) }
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'space_after_comma' do
4
+ let(:message) { 'Add space after the comma' }
5
+
6
+ context 'with missing space after single comma' do
7
+ let(:code) { "'vivid','wily'" }
8
+
9
+ it { expect(problems).to contain_warning(message).on_line(1).in_column(8) }
10
+ end
11
+
12
+ context 'with missing space after two commas' do
13
+ let(:code) { "'vivid','wily', 'xenial','yakketi'" }
14
+
15
+ it { expect(problems).to have(2).problems }
16
+ it { expect(problems).to contain_warning(message).on_line(1).in_column(8) }
17
+ it { expect(problems).to contain_warning(message).on_line(1).in_column(25) }
18
+ end
19
+
20
+ context 'with space after comma' do
21
+ let(:code) { "'vivid', 'wily'" }
22
+
23
+ it { expect(problems).to have(0).problems }
24
+ end
25
+
26
+ context 'with trailing comma' do
27
+ let(:code) { "'vivid', 'wily'," }
28
+
29
+ it { expect(problems).to have(0).problems }
30
+ end
31
+
32
+ context 'with new line after comma' do
33
+ let(:code) { "'vivid',\n'wily',\n'xenial'" }
34
+
35
+ it { expect(problems).to have(0).problems }
36
+ end
37
+
38
+ context 'with semicolon after comma' do
39
+ let(:code) { "'vivid', 'wily', 'xenial',;" }
40
+
41
+ it { expect(problems).to have(0).problems }
42
+ end
43
+
44
+ context 'with missing comma after the last element of a list' do
45
+ let(:code) { "['vivid', 'wily', 'xenial',]" }
46
+
47
+ it { expect(problems).to have(0).problems }
48
+ end
49
+
50
+ context 'with fix enabled' do
51
+ before do
52
+ PuppetLint.configuration.fix = true
53
+ end
54
+
55
+ after do
56
+ PuppetLint.configuration.fix = false
57
+ end
58
+
59
+ let(:code) { "'vivid','wily', 'xenial','yakketi'" }
60
+ let(:fixed) { "'vivid', 'wily', 'xenial', 'yakketi'" }
61
+
62
+ it { expect(manifest).to eq(fixed) }
63
+ end
64
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'space_around_operators' do
4
+
5
+ shared_examples_for 'missing space' do |operator|
6
+ let(:message) { "Surrounding space missing for operator #{operator}" }
7
+
8
+ context 'with missing space on the left side of the operator' do
9
+ let(:code) { "a#{operator} b" }
10
+ it { expect(problems).to contain_warning(message).on_line(1) }
11
+ end
12
+
13
+ context 'with missing space on the right side of the operator' do
14
+ let(:code) { "a #{operator}b" }
15
+ it { expect(problems).to contain_warning(message).on_line(1) }
16
+ end
17
+
18
+ context 'with new line after the operator' do
19
+ let(:code) { "a #{operator}\nb" }
20
+ it { expect(problems).to have(0).problems }
21
+ end
22
+
23
+ context 'with space on both side of operator' do
24
+ let(:code) { "a #{operator} b" }
25
+ it { expect(problems).to have(0).problems }
26
+ end
27
+
28
+ context 'with fix enabled' do
29
+ before do
30
+ PuppetLint.configuration.fix = true
31
+ end
32
+
33
+ after do
34
+ PuppetLint.configuration.fix = false
35
+ end
36
+
37
+ let(:code) { "a#{operator}b" }
38
+ let(:fixed) { "a #{operator} b" }
39
+
40
+ it { expect(manifest).to eq(fixed) }
41
+ end
42
+ end
43
+
44
+ it_behaves_like('missing space', '=')
45
+ it_behaves_like('missing space', '==')
46
+ it_behaves_like('missing space', '!=')
47
+ it_behaves_like('missing space', '=~')
48
+ it_behaves_like('missing space', '!~')
49
+ it_behaves_like('missing space', '+=')
50
+ it_behaves_like('missing space', '+')
51
+ it_behaves_like('missing space', '>=')
52
+ it_behaves_like('missing space', '>')
53
+ it_behaves_like('missing space', '<=')
54
+ it_behaves_like('missing space', '<')
55
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'space_inside_braces' do
4
+ let(:message_for_opening_brace) { 'Add space after opening brace' }
5
+ let(:message_for_closing_brace) { 'Add space before closing brace' }
6
+
7
+ context 'with missing space after opening brace' do
8
+ let(:code) { "if $url {do_something }" }
9
+
10
+ it { expect(problems).to contain_warning(message_for_opening_brace).on_line(1).in_column(9) }
11
+ end
12
+
13
+ context 'with empty braces' do
14
+ let(:code) { "if $url {}" }
15
+
16
+ it { expect(problems).to have(0).problems }
17
+ end
18
+
19
+ context 'with new line after opening brace' do
20
+ let(:code) do <<-RUBY
21
+ if $url {
22
+
23
+ }
24
+ RUBY
25
+ end
26
+
27
+ it { expect(problems).to have(0).problems }
28
+ end
29
+
30
+ context 'with missing space before closing brace' do
31
+ let(:code) { "if $url { do_something}" }
32
+
33
+ it { expect(problems).to contain_warning(message_for_closing_brace).on_line(1).in_column(23) }
34
+ end
35
+
36
+
37
+ context 'with fix enabled' do
38
+ before do
39
+ PuppetLint.configuration.fix = true
40
+ end
41
+
42
+ after do
43
+ PuppetLint.configuration.fix = false
44
+ end
45
+
46
+ let(:code) { "if $url {do_something}" }
47
+ let(:fixed) { "if $url { do_something }" }
48
+
49
+ it { expect(manifest).to eq(fixed) }
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-extended
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Tradeo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puppet-lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-json_expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: " Extends puppet-lint with additional checks.\n"
112
+ email: opensource@tradeo.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - README.md
118
+ - lib/puppet-lint/plugins/leading_comment_space.rb
119
+ - lib/puppet-lint/plugins/space_after_comma.rb
120
+ - lib/puppet-lint/plugins/space_around_operators.rb
121
+ - lib/puppet-lint/plugins/space_inside_braces.rb
122
+ - spec/puppet-lint/plugins/leading_comment_space.rb
123
+ - spec/puppet-lint/plugins/space_after_comma_spec.rb
124
+ - spec/puppet-lint/plugins/space_around_operators_spec.rb
125
+ - spec/puppet-lint/plugins/space_inside_braces_spec.rb
126
+ - spec/spec_helper.rb
127
+ homepage: https://github.com/tradeo/puppet-lint-extended
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.6.11
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Additional checks for puppet-lint
151
+ test_files:
152
+ - spec/puppet-lint/plugins/leading_comment_space.rb
153
+ - spec/puppet-lint/plugins/space_after_comma_spec.rb
154
+ - spec/puppet-lint/plugins/space_around_operators_spec.rb
155
+ - spec/puppet-lint/plugins/space_inside_braces_spec.rb
156
+ - spec/spec_helper.rb