puppet-lint-param-types 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26acacc6412f383e06f34f5ccea0bbd06cb294ee
4
+ data.tar.gz: 4623def5816195954c9fff6989a6e007567236be
5
+ SHA512:
6
+ metadata.gz: 62175cae22a41add32ca935a00aaeb14017fabbe724843523559642620d9c8c361a3a60cc31b5ee773bb2bc44dd1dfa2a8a4be812f58c90357d27f72be02477c
7
+ data.tar.gz: 7e789a73b47386476657605cae8e9d78da1bc5752f94b14986a4791b5f363a52cba36c2c149bf2fa5650920af8c3dfb44f735b14e0d9808d2571a0e36682ff6f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tim Sharpe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # puppet-lint-param-types
2
+
3
+ This plugin validates that all class and defined type parameter are typed.
4
+
5
+ ## Installation
6
+
7
+ To use this plugin, add the following line to the Gemfile in your Puppet code
8
+ base and run `bundle install`.
9
+
10
+ ```ruby
11
+ gem 'puppet-lint-param-types
12
+ ```
13
+
14
+ Verify that plugin is installed correctly.
15
+
16
+ ```bash
17
+ $ puppet-lint -h | grep parameter_types
18
+ --no-parameter_types-check Skip the parameter_types check.
19
+ ```
20
+
21
+ ## Usage
22
+
23
+
24
+ ### parameter_types
25
+
26
+ **--fix-support: No**
27
+
28
+ Will raise a warning if a class parameter without data type specification is
29
+ found.
30
+
31
+ ```
32
+ WARNING: missing datatype for parameter mysql::service_ensure on line 5
33
+ ```
34
+
35
+ What you did:
36
+
37
+ ```
38
+ class foo (
39
+ $bar,
40
+ $baz,
41
+ ) { ... }
42
+ ```
43
+
44
+ What you should have done:
45
+
46
+ ```
47
+ class foo (
48
+ Integer $bar,
49
+ Array[String] $baz,
50
+ ) { ... }
51
+ ```
52
+
53
+ ## References
54
+
55
+ https://puppet.com/docs/puppet/latest/lang_data_type.html
@@ -0,0 +1,53 @@
1
+ PuppetLint.new_check(:parameter_types) do
2
+ def check
3
+ (class_indexes + defined_type_indexes).each do |idx|
4
+ next if idx[:param_tokens].nil?
5
+ # https://github.com/puppetlabs/puppet-specifications/blob/master/language/catalog_expressions.md
6
+ # Each individual parameter in the parameter list must start with
7
+ # either a datatype or a variable name, so testing whether the parameter is typed
8
+ # is easy.
9
+ state = :ST_BEGIN
10
+ paren_stack = []
11
+ data_stack = []
12
+ idx[:param_tokens].each do |token|
13
+ next if [ :NEWLINE, :WHITESPACE, :INDENT, :COMMENT, :MLCOMMENT, :SLASH_COMMENT].include?(token.type)
14
+
15
+ case state
16
+ when :ST_BEGIN
17
+ paren_stack = []
18
+ data_stack = []
19
+ if (token.type == :TYPE) or (token.value =~ /^[A-Z]/)
20
+ state = :ST_SKIP_TYPE
21
+ elsif token.type == :VARIABLE
22
+ notify :warning, {
23
+ :message => "missing datatype for parameter #{idx[:name_token].value}::#{token.value}",
24
+ :line => token.line,
25
+ :column => token.column
26
+ }
27
+ state = :ST_SKIP
28
+ end
29
+ # skip over the parameter default which can contain complex data types with variable references
30
+ # so a simple comma check isn't enough, brackets must be counted.
31
+ when :ST_SKIP
32
+ if token.type == :LPAREN
33
+ paren_stack.push(true)
34
+ elsif token.type == :RPAREN
35
+ paren_stack.pop
36
+ elsif token.type == :LBRACE || token.type == :LBRACK
37
+ data_stack.push(true)
38
+ elsif token.type == :RBRACE || token.type == :RBRACK
39
+ data_stack.pop
40
+ elsif token.type == :COMMA && data_stack.empty? && paren_stack.empty?
41
+ state = :ST_BEGIN
42
+ end
43
+ # Datatypes cannot have variables so when a variable is found it must be
44
+ # end of the data type
45
+ when :ST_SKIP_TYPE
46
+ if token.type == :VARIABLE
47
+ state = :ST_SKIP
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'parameter_types' do
4
+ let(:msg) { 'missing datatype for parameter spec::%s' }
5
+
6
+ [ 'class', 'define' ].each do | rt |
7
+ context "#{rt} without parameters" do
8
+ let(:code) { 'class spec() {}' }
9
+ it 'should not be a problem' do
10
+ expect(problems).to have(0).problem
11
+ end
12
+ end
13
+ context "simple #{rt} without type" do
14
+ let(:code) { 'class spec($foo) { }' }
15
+ it 'should be a problem' do
16
+ expect(problems).to have(1).problem
17
+ expect(problems).to contain_warning(msg % :foo).on_line(1)
18
+ end
19
+ end
20
+
21
+ context "#{rt} with many params without type" do
22
+ let(:code) do
23
+ <<-EOL
24
+ class spec (
25
+ $attr1,
26
+ $attr2,
27
+ $attr3
28
+ ) { }
29
+ EOL
30
+ end
31
+ it 'should be a problem' do
32
+ expect(problems).to have(3).problem
33
+ end
34
+ end
35
+ context "#{rt} with many params without type on one line" do
36
+ let(:code) { 'class spec ($attr1, $attr2, $attr3 ) { }' }
37
+ it 'should be a problem' do
38
+ expect(problems).to have(3).problem
39
+ end
40
+ end
41
+ context "#{rt} with many params and defaults without type" do
42
+ let(:code) do
43
+ <<-EOL
44
+ class spec (
45
+ $attr0,
46
+ $attr1 = 1,
47
+ $attr2 = $attr1,
48
+ $attr3 = [ 'array', 'with', 'entries'],
49
+ $attr4 = { 'key' => 'value' }
50
+ $attr5 = {
51
+ 'key' => 'value',
52
+ 'key2' => [
53
+ 'val1',
54
+ 'val2',
55
+ ],
56
+ },
57
+ ) { }
58
+ EOL
59
+ end
60
+ it 'should be a problem' do
61
+ expect(problems).to have(5).problem
62
+ end
63
+ end
64
+
65
+ context "#{rt} with some attributes typed" do
66
+ let(:code) do
67
+ <<-EOL
68
+ class spec (
69
+ Integer $attr1,
70
+ String $attr2 = 'foo',
71
+ $attr3 = undef,
72
+ $attr4 = { 'key' => 'value' }
73
+ Array[Struct[{
74
+ name => String[1],
75
+ source_url => String[1],
76
+ delete => Optional[Boolean],
77
+ exclude => Optional[Variant[String,Array[String]]],
78
+ include => Optional[Variant[String,Array[String]]],
79
+ sync_hour => Optional[String],
80
+ }]] $repos = [],
81
+ Hash $attr5 = {
82
+ 'key' => 'value',
83
+ 'key2' => [
84
+ 'val1',
85
+ 'val2',
86
+ ],
87
+ },
88
+ ) { }
89
+ EOL
90
+ end
91
+ it 'should be a problem' do
92
+ expect(problems).to have(2).problem
93
+ end
94
+ end
95
+ context "#{rt} with some attributes typed on one line" do
96
+ let(:code) { 'class spec(Integer $attr1, $attr2 = 5, Variant[String,Integer] $attr 3, $attr4 = [1,2]) { }' }
97
+ it 'should be a problem' do
98
+ expect(problems).to have(2).problem
99
+ end
100
+ end
101
+
102
+ context "#{rt} with all attributes typed" do
103
+ let(:code) do
104
+ <<-EOL
105
+ class spec (
106
+ Integer $attr1,
107
+ String $attr2 = 'foo',
108
+ Optional[String] $attr3 = undef,
109
+ Optional[Variant[Integer,Array[String] $attr4 = undef,
110
+ Stdlib::MyType $attr5 = undef,
111
+ ) { }
112
+ EOL
113
+ end
114
+ it 'should not be a problem' do
115
+ expect(problems).to have(0).problem
116
+ end
117
+ end
118
+ context "#{rt} with all attributes typed complex" do
119
+ let(:code) do
120
+ <<-EOL
121
+ class spec (
122
+ Integer $attr1,
123
+ String $attr2 = 'foo',
124
+ Optional[String] $attr3 = undef,
125
+ Optional[Variant[Integer,Array[String] $attr4,
126
+ Array[Struct[{
127
+ name => String[1],
128
+ source_url => String[1],
129
+ delete => Optional[Boolean],
130
+ exclude => Optional[Variant[String,Array[String]]],
131
+ include => Optional[Variant[String,Array[String]]],
132
+ sync_hour => Optional[String],
133
+ }]] $repos = [],
134
+ ) { }
135
+ EOL
136
+ end
137
+ it 'should not be a problem' do
138
+ expect(problems).to have(0).problem
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-param-types
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hostnet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-05 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.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-its
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.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: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: |2
90
+ A new check for puppet-lint that validates that all parameters are typed.
91
+ email: opensource@hostnet.nl
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - LICENSE
97
+ - README.md
98
+ - lib/puppet-lint/plugins/check_parameter_types.rb
99
+ - spec/puppet-lint/plugins/check_parameter_types_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/hostnet/puppet-lint-param-types
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: puppet-lint check that validates that all parameters are typed
125
+ test_files:
126
+ - spec/puppet-lint/plugins/check_parameter_types_spec.rb
127
+ - spec/spec_helper.rb