puppet-lint-metrics-check 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d51581998a6c07979046e2f521f0adff298e2feb
4
+ data.tar.gz: 8ccd7883109b2c468e3265a5a84d85b450603370
5
+ SHA512:
6
+ metadata.gz: 8604ae5a4cd11e6e6f6d0ce1a0ce43dc55a23cc0e1f55debffda75d0f768219ef49a9c1bce3b740738b07af1de504739ad28f53f6d637275e03aa88d6b0d5981
7
+ data.tar.gz: c7ccbb60524c49e3b79d2df186cd885d4c40ef5c07a89db950cda4bdcdcd26238fa3a9237f35ea3717e7800cc0bb8c3666f08b7886252689810092608c267b1d
@@ -0,0 +1,50 @@
1
+ # puppet-lint-metrics-check
2
+
3
+ [![Build Status](https://img.shields.io/travis/danzilio/puppet-lint-metrics-check.svg)](https://travis-ci.org/danzilio/puppet-lint-metrics-check)
4
+ [![Gem Version](https://img.shields.io/gem/v/puppet-lint-metrics-check.svg)](https://rubygems.org/gems/puppet-lint-metrics-check)
5
+ [![Gem Downloads](https://img.shields.io/gem/dt/puppet-lint-metrics-check.svg)](https://rubygems.org/gems/puppet-lint-metrics-check)
6
+
7
+ A puppet-lint plugin to analyze your code and report code quality metrics. Right now this code only supports the Assignment Branch Condition metric. For more information on how we compute this metric, please see [docs/abc_metric.md](docs/abc_metric.md).
8
+
9
+ ## Installing
10
+ ### From the command line
11
+ ```shell
12
+ $ gem install puppet-lint-metrics-check
13
+ ```
14
+
15
+ ### In a Gemfile
16
+ ```ruby
17
+ gem 'puppet-lint-metrics-check'
18
+ ```
19
+
20
+ ## Checks
21
+ ### Assignment Branch Condition Size
22
+
23
+ This check calculates the Assignment Branch Condition (ABC) size for your manifest. By default, if the ABC size calculated for your manifest is above 30 a warning will be reported, if it's above 100 puppet-lint will report an error.
24
+
25
+ You can customize the warning and error thresholds using the `PuppetLint.configuration.metrics_abc_error` and `PuppetLint.configuration.metrics_abc_warning` methods. You can configure these values in your `Rakefile`.
26
+
27
+ #### To set the error threshold to 50
28
+
29
+ ```ruby
30
+ PuppetLint.configuration.metrics_abc_error = 50
31
+ ```
32
+
33
+ #### To set the warning threshold to 10
34
+
35
+ ```ruby
36
+ PuppetLint.configuration.metrics_abc_warning = 10
37
+ ```
38
+
39
+ #### Disabling the check
40
+ To disable this check, you can add `--no-abc_metric-check` to your puppet-lint command line.
41
+
42
+ ```shell
43
+ $ puppet-lint --no-abc_metric-check path/to/file.pp
44
+ ```
45
+
46
+ Alternatively, if you’re calling puppet-lint via the Rake task, you should insert the following line to your `Rakefile`.
47
+
48
+ ```ruby
49
+ PuppetLint.configuration.send('disable_abc_metric')
50
+ ```
@@ -0,0 +1,107 @@
1
+ require 'puppet'
2
+
3
+ class PuppetLint
4
+ module Metrics
5
+ class Abc
6
+ attr_reader :metrics
7
+
8
+ def initialize()
9
+ @@abc_visitor ||= Puppet::Pops::Visitor.new(nil, "abc", 0, 0)
10
+ @metrics = {
11
+ assignment: 0,
12
+ branch: 0,
13
+ conditional: 0,
14
+ }
15
+ end
16
+
17
+ def compute(target)
18
+ target.eAllContents.each { |m| abc(m) }
19
+ [@metrics, Math.sqrt(@metrics[:assignment]**2 + @metrics[:branch]**2 + @metrics[:conditional]**2).round(2)]
20
+ end
21
+
22
+ def abc(o)
23
+ @@abc_visitor.visit_this_0(self, o)
24
+ end
25
+
26
+ def abc_Object(o)
27
+ nil
28
+ end
29
+
30
+ def conditional
31
+ @metrics[:conditional] += 1
32
+ end
33
+
34
+ def branch
35
+ @metrics[:branch] += 1
36
+ end
37
+
38
+ def assignment
39
+ @metrics[:assignment] += 1
40
+ end
41
+
42
+ def abc_AssignmentExpression(o)
43
+ assignment
44
+ end
45
+
46
+ def abc_AttributeOperation(o)
47
+ assignment
48
+ end
49
+
50
+ def abc_ResourceExpression(o)
51
+ branch
52
+ end
53
+
54
+ def abc_CollectorExpression(o)
55
+ branch
56
+ end
57
+
58
+ def abc_CallExpression(o)
59
+ branch
60
+ end
61
+
62
+ def abc_BlockExpression(o)
63
+ branch
64
+ end
65
+
66
+ def abc_EqualityExpression(o)
67
+ conditional
68
+ end
69
+
70
+ def abc_MatchExpression(o)
71
+ conditional
72
+ end
73
+
74
+ def abc_NotMatchExpression(o)
75
+ conditional
76
+ end
77
+
78
+ def abc_ComparisonExpression(o)
79
+ conditional
80
+ end
81
+
82
+ def abc_InExpression(o)
83
+ conditional
84
+ end
85
+
86
+ def abc_NotExpression(o)
87
+ conditional
88
+ end
89
+
90
+ def abc_IfExpression(o)
91
+ conditional
92
+ end
93
+
94
+ def abc_UnlessExpression(o)
95
+ conditional
96
+ end
97
+
98
+ def abc_CaseExpression(o)
99
+ conditional
100
+ end
101
+
102
+ def abc_SelectorExpression(o)
103
+ conditional
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,28 @@
1
+ require 'puppet-lint/metrics/abc'
2
+
3
+ PuppetLint.new_check(:abc_size) do
4
+ def check
5
+ @warn = PuppetLint.configuration.metrics_abc_warning || 30
6
+ @fail = PuppetLint.configuration.metrics_abc_error || 100
7
+
8
+ parser = Puppet::Pops::Parser::EvaluatingParser.new
9
+ program = parser.parse_string(manifest_lines.join("\n"))
10
+ abc_metric = PuppetLint::Metrics::Abc.new
11
+
12
+ abc_size = abc_metric.compute(program.model).last
13
+
14
+ notification = {
15
+ :message => "assignment branch condition size is #{abc_size}",
16
+ :line => 0,
17
+ :column => 0,
18
+ }
19
+
20
+ if @fail > abc_size
21
+ if abc_size >= @warn
22
+ notify :warning, notification
23
+ end
24
+ else
25
+ notify :error, notification
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'abc_size' do
4
+ context 'warn when abc is above the configured warning size' do
5
+ before do
6
+ PuppetLint.configuration.metrics_abc_warning = 0
7
+ end
8
+
9
+ let(:code) do
10
+ <<-EOS
11
+ include foo
12
+ EOS
13
+ end
14
+
15
+ it 'should have one problem' do
16
+ expect(problems).to have(1).problems
17
+ end
18
+
19
+ it 'should create a warning' do
20
+ expect(problems).to contain_warning('assignment branch condition size is 1.0')
21
+ end
22
+ end
23
+
24
+ context 'fail when abc is above the configured failure size' do
25
+ before do
26
+ PuppetLint.configuration.metrics_abc_error = 0
27
+ end
28
+
29
+ let(:code) do
30
+ <<-EOS
31
+ include foo
32
+ EOS
33
+ end
34
+
35
+ it 'should have one problem' do
36
+ expect(problems).to have(1).problems
37
+ end
38
+
39
+ it 'should create a warning' do
40
+ expect(problems).to contain_error('assignment branch condition size is 1.0')
41
+ end
42
+ end
43
+
44
+ context 'do nothing when abc is within parameters' do
45
+ before do
46
+ PuppetLint.configuration.metrics_abc_warning = 30
47
+ PuppetLint.configuration.metrics_abc_error = 100
48
+ end
49
+
50
+ let(:code) do
51
+ <<-EOS
52
+ include foo
53
+ EOS
54
+ end
55
+
56
+ it 'should have one problem' do
57
+ expect(problems).to have(0).problems
58
+ end
59
+
60
+ it 'should create a warning' do
61
+ expect(problems).not_to contain_error('assignment branch condition size is 1.0')
62
+ expect(problems).not_to contain_warning('assignment branch condition size is 1.0')
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-metrics-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Danzilio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-26 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: puppet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
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: 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: rake
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
+ description: A puppet-lint plugin to check code quality metrics.
98
+ email: david@danzilio.net
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - README.md
104
+ - lib/puppet-lint/metrics/abc.rb
105
+ - lib/puppet-lint/plugins/check_abc_size.rb
106
+ - spec/spec/puppet-lint/plugins/check_abc_size/check_abc_size_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: https://github.com/danzilio/puppet-lint-metrics-check
109
+ licenses:
110
+ - Apache-2.0
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A puppet-lint plugin to check code quality metrics.
132
+ test_files:
133
+ - spec/spec/puppet-lint/plugins/check_abc_size/check_abc_size_spec.rb
134
+ - spec/spec_helper.rb