Browse Source

Add coverage support

Change-Id: I6dc9d8bb542203a6eb25460f3c8fced449c54c14
Dmitry Ilyin 3 years ago
parent
commit
d54efb1c7b

+ 0
- 1
Gemfile View File

@@ -7,7 +7,6 @@ group :development, :test do
7 7
   gem 'rspec-puppet-utils', '~> 2.0.0'
8 8
   gem 'deep_merge'
9 9
   gem 'pry'
10
-  gem 'simplecov'
11 10
   gem 'puppet-spec'
12 11
   gem 'colorize'
13 12
   gem 'parallel'

+ 3
- 3
lib/noop/manager/options.rb View File

@@ -137,9 +137,9 @@ module Noop
137 137
         opts.on('-a', '--spec_status', 'Show spec status blocks') do
138 138
           ENV['SPEC_SHOW_STATUS'] = 'YES'
139 139
         end
140
-        # opts.on('--spec_coverage', 'Show spec coverage statistics') do
141
-        #   ENV['SPEC_COVERAGE'] = 'YES'
142
-        # end
140
+        opts.on('--spec_coverage', 'Show spec coverage statistics') do
141
+          ENV['SPEC_COVERAGE'] = 'YES'
142
+        end
143 143
         opts.on('--puppet_binary_files', 'Check if Puppet installs binary files') do
144 144
           ENV['SPEC_PUPPET_BINARY_FILES'] = 'YES'
145 145
         end

+ 30
- 0
lib/noop/task/overrides.rb View File

@@ -5,6 +5,7 @@ module Noop
5 5
       puppet_default_settings
6 6
       puppet_debug_override if ENV['SPEC_PUPPET_DEBUG']
7 7
       puppet_resource_scope_override
8
+      rspec_coverage_add_override
8 9
       return unless file_name_spec_set?
9 10
       hiera_config_override
10 11
       setup_manifest
@@ -92,5 +93,34 @@ module Noop
92 93
       )
93 94
     end
94 95
 
96
+    def rspec_coverage_add_override
97
+      RSpec::Puppet::Coverage.class_eval do
98
+        def add_from_catalog(catalog, test_module)
99
+          catalog.to_a.each do |resource|
100
+            next if @filters.include?(resource.to_s)
101
+            if resource.file == Puppet[:manifest]
102
+              add(resource)
103
+            else
104
+              @excluded = [] unless @excluded
105
+              @excluded << resource.to_s
106
+            end
107
+          end
108
+        end
109
+        
110
+        def report!
111
+          report = {}
112
+          report[:total] = @collection.size
113
+          report[:touched] = @collection.count { |_, resource| resource.touched? }
114
+          report[:untouched] = report[:total] - report[:touched]
115
+          report[:coverage] = "%5.2f" % ((report[:touched].to_f / report[:total].to_f) * 100)
116
+          report[:resources] = Hash[*@collection.map do |name, wrapper|
117
+            [name, wrapper.to_hash]
118
+          end.flatten]
119
+          report[:excluded] = @excluded
120
+          report
121
+        end
122
+      end
123
+    end
124
+
95 125
   end
96 126
 end

+ 15
- 0
lib/noop/task/report.rb View File

@@ -70,6 +70,21 @@ Facts hierarchy:
70 70
       Noop::Config.dir_path_reports + file_name_report_json
71 71
     end
72 72
 
73
+    # @return [Pathname]
74
+    def dir_name_coverage
75
+      Pathname.new 'coverage'
76
+    end
77
+
78
+    # @return [Pathname]
79
+    def dir_path_coverage
80
+      Noop::Config.dir_path_reports + dir_name_coverage
81
+    end
82
+
83
+    # @return [Pathname]
84
+    def file_path_coverage_report
85
+      dir_path_coverage + Noop::Utils.convert_to_path("#{file_name_base_task_report}.yaml")
86
+    end
87
+
73 88
     # @return [Hash]
74 89
     def file_data_report_json
75 90
       return unless file_present_report_json?

+ 3
- 0
reports/coverage/.gitignore View File

@@ -0,0 +1,3 @@
1
+*.yaml
2
+*.lock
3
+*.json

+ 20
- 0
spec/shared-examples.rb View File

@@ -116,6 +116,26 @@ def run_test(manifest_file, *args)
116 116
     true
117 117
   end
118 118
 
119
+  at_exit do
120
+    Noop.dir_path_coverage.mktree unless Noop.dir_path_coverage.directory?
121
+    report = RSpec::Puppet::Coverage.report!
122
+    Noop::Utils.output "Coverage:#{report[:coverage]}% (#{report[:touched]}/#{report[:total]})"
123
+    if report[:untouched] > 0
124
+      resources_report = "Untouched resources:\n"
125
+      resources = report[:resources]
126
+      if resources.is_a? Hash
127
+        resources.each do |resource, status|
128
+          resources_report += "* #{resource}\n" unless status['touched']
129
+        end
130
+      end
131
+      Noop::Utils.output resources_report
132
+    end
133
+    Noop::Utils.debug "Saving coverage report to: '#{Noop.file_path_coverage_report}'"
134
+    File.open(Noop.file_path_coverage_report, 'w') do |file|
135
+      file.puts YAML.dump report
136
+    end
137
+  end if ENV['SPEC_COVERAGE']
138
+
119 139
   yield self if block_given?
120 140
 
121 141
 end

Loading…
Cancel
Save