puppet-lint-topscope-variable-check 1.0.1

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
+ SHA256:
3
+ metadata.gz: e0ad23632093e2963ab543f0e674b202023a5c6f959138af1c1f5f9391457f5a
4
+ data.tar.gz: f1aa73f43b44e3cf0cfbe3ab5751e4ce4b036c95bfb4c12830c3e3fae4572320
5
+ SHA512:
6
+ metadata.gz: 729764ccad0c21ff5ac08c93d892bf60b9e158c7639f307eaa3eaff6e1d30f21de26a4fdd47b0c2940c451ccbd998f9c6d63c69804c8fdce368bc8bf707079b6
7
+ data.tar.gz: ebd1ab0074b7c9056c6a66b38160a7b6d9cda7323be9e188343aaf0ac719a48e5fed838a511e8c743b94c4e438b8b633abfe64d1ed42c78d75b9de3fd07bbd75
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Sixt GmbH & Co. Autovermietung KG
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,105 @@
1
+ puppet-lint-topscope-variable-check
2
+ ========================
3
+ [![Build Status](https://api.travis-ci.org/sixt/puppet-lint-topscope-variable-check.svg)](https://travis-ci.org/sixt/puppet-lint-topscope-variable-check)
4
+
5
+
6
+ A puppet-lint plugin to check that topscope variables in a class do not begin with $::
7
+
8
+ ## Installation
9
+
10
+ ### From the command line
11
+ ```shell
12
+ $ gem install puppet-lint-topscope-variable-check
13
+ ```
14
+
15
+ ### In a Gemfile
16
+ ```ruby
17
+ gem 'puppet-lint-topscope-variable-check', require: false
18
+ ```
19
+
20
+ ## Checks
21
+
22
+ #### What you have done
23
+
24
+ ```puppet
25
+ class foo::blub {
26
+ notify { 'foo':
27
+ message => $::foo::bar,
28
+ }
29
+ }
30
+ ```
31
+
32
+ or
33
+ ```puppet
34
+ class foo::blub(
35
+ String $foo = $::foo::params::foo
36
+ ) {
37
+ # ...
38
+ }
39
+ ```
40
+
41
+ #### What you should have done
42
+
43
+ ```puppet
44
+ class foo::blub {
45
+ notify { 'foo'
46
+ message => $foo::bar,
47
+ }
48
+ }
49
+ ```
50
+
51
+ and
52
+
53
+ ```puppet
54
+ class foo::blub(
55
+ String $foo = $foo::params::foo
56
+ ) {
57
+ # ...
58
+ }
59
+ ```
60
+
61
+ #### Disabling the check
62
+
63
+ To disable this check, you can add `--no-topscope_variable-check` to your puppet-lint command line:
64
+ ```shell
65
+ $ puppet-lint --no-topscope_variable-checks
66
+ ```
67
+
68
+ Alternatively, if you are calling puppet-lint via the Rake task, you should insert the following line into your `Rakefile`:
69
+
70
+ ```ruby
71
+ PuppetLint.configuration.send('disable_topscope_variable')
72
+ ```
73
+ ## Limitations
74
+
75
+ The fix function of this plugin does not work when the variable is used in a string.
76
+ For example:
77
+
78
+ ``` puppet
79
+ $foo = "/etc/$::foobar::path/test"
80
+ ```
81
+
82
+ ## License
83
+ ```
84
+ MIT License
85
+
86
+ Copyright (c) 2018 Sixt GmbH & Co. Autovermietung KG
87
+
88
+ Permission is hereby granted, free of charge, to any person obtaining a copy
89
+ of this software and associated documentation files (the "Software"), to deal
90
+ in the Software without restriction, including without limitation the rights
91
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
92
+ copies of the Software, and to permit persons to whom the Software is
93
+ furnished to do so, subject to the following conditions:
94
+
95
+ The above copyright notice and this permission notice shall be included in all
96
+ copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
99
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
100
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
101
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
102
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
103
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
104
+ SOFTWARE.
105
+ ```
@@ -0,0 +1,23 @@
1
+ PuppetLint.new_check(:topscope_variable) do
2
+ def check
3
+ class_list = (class_indexes + defined_type_indexes)
4
+ # do not check if the code is not part of a class
5
+ return if class_list.first.nil?
6
+ class_name = class_list.first[:name_token].value.split('::').first
7
+ tokens.select { |x| x.type == :VARIABLE }.each do |token|
8
+ next if token.value !~ /^::#{class_name}::/
9
+ fixed = token.value.sub(/^::/, '')
10
+ notify(
11
+ :warning,
12
+ message: "use $#{fixed} instead of $#{token.value}",
13
+ line: token.line,
14
+ column: token.column,
15
+ token: token
16
+ )
17
+ end
18
+ end
19
+
20
+ def fix(problem)
21
+ problem[:token].value.sub!(/^::/, '')
22
+ end
23
+ end
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'topscope_variable' do
4
+ let(:msg) { 'use $foo::bar instead of $::foo::bar' }
5
+ context 'with fix disabled' do
6
+ context 'with correct topscope' do
7
+ let(:code) do
8
+ <<-PUP.strip_heredoc
9
+ class foo::blub {
10
+ notify { 'foo':
11
+ message => $foo::bar
12
+ }
13
+ }
14
+ PUP
15
+ end
16
+
17
+ it 'should not detect any problems' do
18
+ expect(problems).to have(0).problem
19
+ end
20
+ end
21
+
22
+ context 'with incorrect topscope' do
23
+ let(:code) do
24
+ <<-PUP.strip_heredoc
25
+ class foo::blub {
26
+ notify { 'foo':
27
+ message => $::foo::bar
28
+ }
29
+ }
30
+ PUP
31
+ end
32
+
33
+ it 'should detect one problem' do
34
+ expect(problems).to have(1).problem
35
+ end
36
+ end
37
+
38
+ context 'with correct topscope in params' do
39
+ let(:code) do
40
+ <<-PUP.strip_heredoc
41
+ class foo::blub(
42
+ String $foo = $params::foo
43
+ ) {
44
+ # ...
45
+ }
46
+ PUP
47
+ end
48
+
49
+ it 'should not detect any problems' do
50
+ expect(problems).to have(0).problem
51
+ end
52
+ end
53
+
54
+ context 'with incorrect topscope in params' do
55
+ let(:code) do
56
+ <<-PUP.strip_heredoc
57
+ class foo::blub(
58
+ String $foo = $::foo::params::foo
59
+ ) {
60
+ # ...
61
+ }
62
+ PUP
63
+ end
64
+
65
+ it 'should detect one problem' do
66
+ expect(problems).to have(1).problem
67
+ end
68
+ end
69
+
70
+ context 'with a fact that has been used improperly' do
71
+ let(:code) do
72
+ <<-PUP.strip_heredoc
73
+ class foo::blub {
74
+ notify { 'foo':
75
+ message => $blub::bar
76
+ }
77
+ }
78
+ PUP
79
+ end
80
+
81
+ it 'should not detect any problems' do
82
+ expect(problems).to have(0).problem
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'with fix enabled' do
88
+ before do
89
+ PuppetLint.configuration.fix = true
90
+ end
91
+
92
+ after do
93
+ PuppetLint.configuration.fix = false
94
+ end
95
+
96
+ context 'with correct topscope' do
97
+ let(:code) do
98
+ <<-PUP.strip_heredoc
99
+ class foo::blub {
100
+ notify { 'foo':
101
+ message => $foo::bar
102
+ }
103
+ }
104
+ PUP
105
+ end
106
+
107
+ it 'should not detect any problems' do
108
+ expect(problems).to have(0).problem
109
+ end
110
+ end
111
+
112
+ context 'with incorrect topscope' do
113
+ let(:code) do
114
+ <<-PUP.strip_heredoc
115
+ class foo::blub {
116
+ notify { 'foo':
117
+ message => $::foo::bar
118
+ }
119
+ }
120
+ PUP
121
+ end
122
+
123
+ it 'should detect one problem' do
124
+ expect(problems).to have(1).problem
125
+ end
126
+
127
+ it 'should fix the problem' do
128
+ expect(problems).to contain_fixed(msg).on_line(3).in_column(16)
129
+ end
130
+
131
+ it 'should remove :: after the $' do
132
+ expect(manifest).to eq <<-PUP.strip_heredoc
133
+ class foo::blub {
134
+ notify { 'foo':
135
+ message => $foo::bar
136
+ }
137
+ }
138
+ PUP
139
+ end
140
+ end
141
+
142
+ context 'with incorrect topscope in quoted variable' do
143
+ let(:code) do
144
+ <<-PUP.strip_heredoc
145
+ class foo::blub {
146
+ notify { 'foo':
147
+ message => ">${::foo::bar}<"
148
+ }
149
+ }
150
+ PUP
151
+ end
152
+
153
+ it 'should detect one problem' do
154
+ expect(problems).to have(1).problem
155
+ end
156
+
157
+ it 'should fix the problem' do
158
+ expect(problems).to contain_fixed(msg).on_line(3).in_column(19)
159
+ end
160
+
161
+ # it 'should remove :: after the $' do
162
+ # expect(manifest).to eq <<-PUP.strip_heredoc
163
+ # class foo::blub {
164
+ # notify { 'foo':
165
+ # message => ">${foo::bar}<"
166
+ # }
167
+ # }
168
+ # PUP
169
+ # end
170
+ end
171
+ end
172
+
173
+ context 'without a class scope' do
174
+ let(:code) do
175
+ <<-PUP.strip_heredoc
176
+ include ::foo
177
+ PUP
178
+ end
179
+ it 'should not detect any problems' do
180
+ expect(problems).to have(0).problem
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,13 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'puppet-lint'
5
+ PuppetLint::Plugins.load_spec_helper
6
+
7
+ # strip left spaces from heredoc
8
+ # this emulates the behaviour of the ~ heredoc from ruby 2.3
9
+ class String
10
+ def strip_heredoc
11
+ gsub(/^#{scan(/^\s*/).min_by(&:length)}/, '')
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-topscope-variable-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Merfort
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-12 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-collection_matchers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.58'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.58'
111
+ description: " A puppet-lint plugin to check that topscope variable names don't
112
+ start with ::.\n"
113
+ email: platformengineering@sixt.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE
119
+ - README.md
120
+ - lib/puppet-lint/plugins/topscope_variable.rb
121
+ - spec/puppet-lint/plugins/topscope_variable_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/Sixt/puppet-lint-topscope-variable-check
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.7.8
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: A puppet-lint plugin to check topscope variables.
147
+ test_files:
148
+ - spec/spec_helper.rb
149
+ - spec/puppet-lint/plugins/topscope_variable_spec.rb