Add coverage support

Change-Id: I6dc9d8bb542203a6eb25460f3c8fced449c54c14
This commit is contained in:
Dmitry Ilyin 2016-03-30 15:24:26 +03:00
parent 6d343f2038
commit d54efb1c7b
6 changed files with 71 additions and 4 deletions

View File

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

View File

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

View File

@ -5,6 +5,7 @@ module Noop
puppet_default_settings
puppet_debug_override if ENV['SPEC_PUPPET_DEBUG']
puppet_resource_scope_override
rspec_coverage_add_override
return unless file_name_spec_set?
hiera_config_override
setup_manifest
@ -92,5 +93,34 @@ module Noop
)
end
def rspec_coverage_add_override
RSpec::Puppet::Coverage.class_eval do
def add_from_catalog(catalog, test_module)
catalog.to_a.each do |resource|
next if @filters.include?(resource.to_s)
if resource.file == Puppet[:manifest]
add(resource)
else
@excluded = [] unless @excluded
@excluded << resource.to_s
end
end
end
def report!
report = {}
report[:total] = @collection.size
report[:touched] = @collection.count { |_, resource| resource.touched? }
report[:untouched] = report[:total] - report[:touched]
report[:coverage] = "%5.2f" % ((report[:touched].to_f / report[:total].to_f) * 100)
report[:resources] = Hash[*@collection.map do |name, wrapper|
[name, wrapper.to_hash]
end.flatten]
report[:excluded] = @excluded
report
end
end
end
end
end

View File

@ -70,6 +70,21 @@ Facts hierarchy:
Noop::Config.dir_path_reports + file_name_report_json
end
# @return [Pathname]
def dir_name_coverage
Pathname.new 'coverage'
end
# @return [Pathname]
def dir_path_coverage
Noop::Config.dir_path_reports + dir_name_coverage
end
# @return [Pathname]
def file_path_coverage_report
dir_path_coverage + Noop::Utils.convert_to_path("#{file_name_base_task_report}.yaml")
end
# @return [Hash]
def file_data_report_json
return unless file_present_report_json?

3
reports/coverage/.gitignore vendored Normal file
View File

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

View File

@ -116,6 +116,26 @@ def run_test(manifest_file, *args)
true
end
at_exit do
Noop.dir_path_coverage.mktree unless Noop.dir_path_coverage.directory?
report = RSpec::Puppet::Coverage.report!
Noop::Utils.output "Coverage:#{report[:coverage]}% (#{report[:touched]}/#{report[:total]})"
if report[:untouched] > 0
resources_report = "Untouched resources:\n"
resources = report[:resources]
if resources.is_a? Hash
resources.each do |resource, status|
resources_report += "* #{resource}\n" unless status['touched']
end
end
Noop::Utils.output resources_report
end
Noop::Utils.debug "Saving coverage report to: '#{Noop.file_path_coverage_report}'"
File.open(Noop.file_path_coverage_report, 'w') do |file|
file.puts YAML.dump report
end
end if ENV['SPEC_COVERAGE']
yield self if block_given?
end