Merge "Add coverage support"
This commit is contained in:
commit
f718226fed
1
Gemfile
1
Gemfile
|
@ -7,7 +7,6 @@ group :development, :test do
|
||||||
gem 'rspec-puppet-utils', '~> 2.0.0'
|
gem 'rspec-puppet-utils', '~> 2.0.0'
|
||||||
gem 'deep_merge'
|
gem 'deep_merge'
|
||||||
gem 'pry'
|
gem 'pry'
|
||||||
gem 'simplecov'
|
|
||||||
gem 'puppet-spec'
|
gem 'puppet-spec'
|
||||||
gem 'colorize'
|
gem 'colorize'
|
||||||
gem 'parallel'
|
gem 'parallel'
|
||||||
|
|
|
@ -137,9 +137,9 @@ module Noop
|
||||||
opts.on('-a', '--spec_status', 'Show spec status blocks') do
|
opts.on('-a', '--spec_status', 'Show spec status blocks') do
|
||||||
ENV['SPEC_SHOW_STATUS'] = 'YES'
|
ENV['SPEC_SHOW_STATUS'] = 'YES'
|
||||||
end
|
end
|
||||||
# opts.on('--spec_coverage', 'Show spec coverage statistics') do
|
opts.on('--spec_coverage', 'Show spec coverage statistics') do
|
||||||
# ENV['SPEC_COVERAGE'] = 'YES'
|
ENV['SPEC_COVERAGE'] = 'YES'
|
||||||
# end
|
end
|
||||||
opts.on('--puppet_binary_files', 'Check if Puppet installs binary files') do
|
opts.on('--puppet_binary_files', 'Check if Puppet installs binary files') do
|
||||||
ENV['SPEC_PUPPET_BINARY_FILES'] = 'YES'
|
ENV['SPEC_PUPPET_BINARY_FILES'] = 'YES'
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ module Noop
|
||||||
puppet_default_settings
|
puppet_default_settings
|
||||||
puppet_debug_override if ENV['SPEC_PUPPET_DEBUG']
|
puppet_debug_override if ENV['SPEC_PUPPET_DEBUG']
|
||||||
puppet_resource_scope_override
|
puppet_resource_scope_override
|
||||||
|
rspec_coverage_add_override
|
||||||
return unless file_name_spec_set?
|
return unless file_name_spec_set?
|
||||||
hiera_config_override
|
hiera_config_override
|
||||||
setup_manifest
|
setup_manifest
|
||||||
|
@ -92,5 +93,34 @@ module Noop
|
||||||
)
|
)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -70,6 +70,21 @@ Facts hierarchy:
|
||||||
Noop::Config.dir_path_reports + file_name_report_json
|
Noop::Config.dir_path_reports + file_name_report_json
|
||||||
end
|
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]
|
# @return [Hash]
|
||||||
def file_data_report_json
|
def file_data_report_json
|
||||||
return unless file_present_report_json?
|
return unless file_present_report_json?
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
*.yaml
|
||||||
|
*.lock
|
||||||
|
*.json
|
|
@ -116,6 +116,26 @@ def run_test(manifest_file, *args)
|
||||||
true
|
true
|
||||||
end
|
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?
|
yield self if block_given?
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue