puppet-lint-strict_indent-check 2.0.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: feb8dee2777ebf6abdfc11c8d5369963d316b7aa
4
+ data.tar.gz: 972df005087cfd211b697829aeee989906113543
5
+ SHA512:
6
+ metadata.gz: 51b9ceeef1fc715949bba52f2533783b5b4f286383621a4c43922ea048b83bbf6d962aadf91fb867e9699ec21daa9751150d9ac40770fc4efa1e69004a2c34a2
7
+ data.tar.gz: 3c8b2a98562412ea40ca184ccfe27890c073cf642bb5e19c2c0ad5c18a2fea375abc1af98c8adce906b600425c4a035518987f1445fe4293fcce2a44ed4b735c
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ This Source Code Form is subject to the terms of the Mozilla Public
2
+ License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ file, You can obtain one at http://mozilla.org/MPL/2.0/
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ Installation
2
+ ===
3
+
4
+ <pre>
5
+ gem install puppet-lint-strict_indent-check
6
+ </pre>
7
+
8
+ Usage
9
+ ===
10
+
11
+ This plugin provides an indentation check for puppet-lint `strict_indent`. The check has a strict expectation for all indentation, except comments and whitespace/blank lines.
12
+
13
+ supports --fix flag
14
+
15
+ Check will raise a warning for any files that don't have 2-space indent, and follow specific guidelines for increase/decrease of indent.
16
+
17
+ Alternate Indentation
18
+ ===
19
+
20
+ Indentation defaults to 2 spaces. To use a different number of spaces, use the `-l` option to load ruby file changing the `chars_per_indent` config.
21
+
22
+ config file `chars_per_indent_4.rb`:
23
+ <pre>
24
+ # This could also be set in your Rakefile
25
+ PuppetLint.configuration.chars_per_indent = 4
26
+ </pre>
27
+
28
+ lint command:
29
+ <pre>
30
+ puppet-lint -l chars_per_indent_4.rb puppet-mysql/manifests/init.pp
31
+ </pre>
32
+
33
+ Acceptable Identation
34
+ ===
35
+
36
+ * indent should increase by one step the line after each opened brace (square, curly, paren)
37
+ * indent should decrease by one step the line of every closed brace.
38
+ * indent should increase by one step the line after a resource title, unless it already increased due to a bracket.
39
+ * indent should decrease by one step the line after the end of a resource due to semicolon, if it was indented for resource title.
40
+ * indent should decrease by one step the line of the end of a resource due to right curly brace, if it was indented for resource title, and not ended by a semicolon.
41
+ * indent should increase by one step the line after an equals `=` or farrow `=>` that ends a line, but only for that one line.
42
+
43
+ see `spec/fixtures/pass/` for good indentation examples
44
+ see `spec/fixtures/fail/` for bad indentation examples
@@ -0,0 +1,154 @@
1
+ # Public: Check the manifest tokens for correct indent levels and
2
+ # record a warning for each instance found.
3
+
4
+ PuppetLint.new_check(:'strict_indent') do
5
+ def match(tokens)
6
+ open = {
7
+ :LBRACE => [],
8
+ :LBRACK => [],
9
+ :LPAREN => [],
10
+ }
11
+
12
+ matches = {}
13
+
14
+ tokens.each do |token|
15
+ if [:LBRACE, :LBRACK, :LPAREN].include?(token.type)
16
+ open[token.type] << token
17
+ elsif [:RBRACE, :RBRACK, :RPAREN].include?(token.type)
18
+ match = open[("L" + token.type[1..-1]).to_sym].pop
19
+ if not match.nil?
20
+ matches[token] = match
21
+ matches[match] = token
22
+ end
23
+ end
24
+ end
25
+
26
+ matches
27
+ end
28
+
29
+ def check
30
+ chars_per_indent = PuppetLint.configuration.chars_per_indent || 2
31
+ indent = 0
32
+ colon_indent = nil
33
+
34
+ matches = match(tokens)
35
+
36
+ tokens.select { |token|
37
+ token.type == :NEWLINE
38
+ }.reject { |token|
39
+ # ignore newline at end of code
40
+ token.next_token.nil?
41
+ }.each do |token|
42
+ temp_indent = 0
43
+
44
+ # indent for open groups in the previous line
45
+ open_groups = 0
46
+ prev_token = token.prev_token
47
+ while not prev_token.nil? and prev_token.type != :NEWLINE
48
+ if [:LBRACE, :LBRACK, :LPAREN].include?(prev_token.type)
49
+ if matches[prev_token].nil? or matches[prev_token].line > prev_token.line
50
+ # left braces not matched in the same line increase indent
51
+ open_groups += 1
52
+ end
53
+ end
54
+ prev_token = prev_token.prev_token
55
+ end
56
+ indent += open_groups
57
+
58
+ # reset prev_token to last non-whitespace token on previous line
59
+ prev_token = token.prev_token
60
+ while not prev_token.nil? and prev_token.type == :WHITESPACE
61
+ prev_token = prev_token.prev_token
62
+ end
63
+
64
+ # get type if available
65
+ prev_type = prev_token.nil? ? nil : prev_token.type
66
+
67
+ # handle change in indent based on last token
68
+ case prev_type
69
+ when :COLON
70
+ if open_groups == 0
71
+ if colon_indent.nil?
72
+ # only indent for a colon when you haven't indented yet
73
+ colon_indent = prev_token.line
74
+ indent += 1
75
+ else
76
+ # you probably missed a semicolon two lines ago
77
+ end
78
+ end
79
+ when :SEMIC
80
+ if not colon_indent.nil?
81
+ # only unindent for a semicolon when we've indented for a colon
82
+ colon_indent = nil
83
+ indent -= 1
84
+ end
85
+ when :EQUALS, :FARROW
86
+ temp_indent += 1
87
+ end
88
+
89
+ # unindent for closing brackets in the current line
90
+ next_token = token.next_token
91
+ while not next_token.nil? and next_token.type != :NEWLINE
92
+ if [:RBRACE, :RBRACK, :RPAREN].include?(next_token.type)
93
+ if not matches[next_token].nil? and matches[next_token].line < next_token.line
94
+ # right braces matched in a previous line decrease indent
95
+ indent -= 1
96
+ end
97
+ if next_token.type == :RBRACE and not colon_indent.nil?
98
+ if not matches[next_token].nil? and matches[next_token].line < colon_indent
99
+ # unindent at the end of resources if needed
100
+ indent -= 1
101
+ colon_indent = nil
102
+ end
103
+ end
104
+ end
105
+ next_token = next_token.next_token
106
+ end
107
+
108
+ # obviously we have a problem
109
+ if indent < 0
110
+ notify :error, {
111
+ :message => 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues',
112
+ :line => token.next_token.line,
113
+ :column => token.next_token.column,
114
+ }
115
+ # stop parsing indent
116
+ break
117
+ end
118
+
119
+ # get actual indent
120
+ actual = 0
121
+ if token.next_token.type == :INDENT
122
+ actual = token.next_token.value.length
123
+ else
124
+ actual = 0
125
+ end
126
+
127
+ # expected indent
128
+ expected = (indent + temp_indent) * chars_per_indent
129
+
130
+ # oh no! incorrect indent!
131
+ if actual != expected
132
+ # no one cares if blank lines and comments are indented correctly
133
+ if not [:COMMENT, :NEWLINE].include?(token.next_token.type)
134
+ notify :warning, {
135
+ :message => "indent should be #{expected} chars and is #{actual}",
136
+ :line => token.line,
137
+ :column => token.column,
138
+ :token => token,
139
+ :indent => expected,
140
+ }
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ def fix(problem)
147
+ char_for_indent = ' '
148
+ if problem[:token].next_token.type == :INDENT
149
+ problem[:token].next_token.value = char_for_indent * problem[:indent]
150
+ else
151
+ problem[:token].next_token.value = char_for_indent * problem[:indent] + problem[:token].next_token.value
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,9 @@
1
+ # failing manifest
2
+ class 1 (
3
+ ) {
4
+ file { 'this':
5
+ ensure => 'present',
6
+ require => [ 'abc',
7
+ 'def', ],
8
+ }
9
+ }
@@ -0,0 +1,55 @@
1
+ # passing manifest 1
2
+ class 1 (
3
+ $arg1 = 1,
4
+ $arg2 =
5
+ 2,
6
+ $arg3 = [
7
+ 1,
8
+ 2,
9
+ ],
10
+ $arg4 = {
11
+ 1 => 2,
12
+ 3 => 4,
13
+ },
14
+ $arg5 = (
15
+ 1 + 1
16
+ ),
17
+ ) {
18
+ file { '/abc':
19
+ ensure => 'file',
20
+ content =>
21
+ file('abc'),
22
+ require => [
23
+ File['def'],
24
+ ],
25
+ }
26
+
27
+ file {
28
+ [
29
+ '/def',
30
+ ]:
31
+ ensure => 'directory';
32
+
33
+ 'ghi':
34
+ ensure => 'file',
35
+ content => file('ghi');
36
+
37
+ 'jkl':
38
+ ensure => 'file'
39
+ content => $::osfamily ? {
40
+ 'RedHat' => 'jkl',
41
+ default => 'jklol',
42
+ };
43
+
44
+ [
45
+ 'mno',
46
+ 'pqr',
47
+ ]:
48
+ ensure => (
49
+ 'file'
50
+ ),
51
+ content => (
52
+ 'stuff'
53
+ );
54
+ }
55
+ }
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'strict_indent' do
4
+ before do
5
+ # disable all other checks
6
+ PuppetLint.configuration.checks.reject{ |check|
7
+ check == :indent
8
+ }.each do |check|
9
+ PuppetLint.configuration.send("disable_#{check}")
10
+ end
11
+ end
12
+
13
+ after do
14
+ # re-enable other checks
15
+ PuppetLint.configuration.checks.reject{ |check|
16
+ check == :indent
17
+ }.each do |check|
18
+ PuppetLint.configuration.send("enable_#{check}")
19
+ end
20
+ end
21
+
22
+ context 'on manifest' do
23
+ Dir['spec/fixtures/pass/*'].each do |manifest|
24
+ context manifest do
25
+ let(:code) { File.read(manifest) }
26
+
27
+ it 'should detect no problems' do
28
+ expect(problems).to have(0).problems
29
+ end
30
+ end
31
+ end
32
+
33
+ Dir['spec/fixtures/fail/*'].each do |manifest|
34
+ context manifest do
35
+ let(:code) { File.read(manifest) }
36
+
37
+ it 'should detect problems' do
38
+ expect(problems).to have(1).problems
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ context 'invalid array indent' do
45
+ let(:code) {
46
+ <<-EOF.gsub(/^ {8}/, '')
47
+ class (
48
+ ) {
49
+ file { 'this':
50
+ ensure => 'present',
51
+ require => [ 'abc',
52
+ 'def', ],
53
+ }
54
+ }
55
+ EOF
56
+ }
57
+
58
+ it 'should detect 1 problems' do
59
+ expect(problems).to have(1).problems
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-strict_indent-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Thornton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 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: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.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: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.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: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
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
+ description: |2
84
+ Extends puppet-lint to ensure that your manifests follow a strict indentation pattern.
85
+ email: daniel@relud.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - lib/puppet-lint/plugins/check_strict_indent.rb
93
+ - spec/fixtures/fail/1.pp
94
+ - spec/fixtures/pass/1.pp
95
+ - spec/puppet-lint/plugins/check_strict_indent_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: https://github.com/relud/puppet-lint-strict_indent-check
98
+ licenses:
99
+ - Mozilla 2.0
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: puppet-lint strict indent check
121
+ test_files:
122
+ - spec/puppet-lint/plugins/check_strict_indent_spec.rb
123
+ - spec/fixtures/fail/1.pp
124
+ - spec/fixtures/pass/1.pp
125
+ - spec/spec_helper.rb