Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task to check if puppet resources have a spec described #439

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/puppetlabs_spec_helper/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'puppetlabs_spec_helper/version'
require 'puppetlabs_spec_helper/tasks/fixtures'
require 'puppetlabs_spec_helper/tasks/check_symlinks'
require 'puppetlabs_spec_helper/tasks/spec_described'
require 'English'

# optional gems
Expand Down Expand Up @@ -252,6 +253,41 @@
print new_version
end

describe_problems = %w[missing unknown]

desc "Check to ensure defined puppet code has been described in spec\n(defaults: coverage=100)"
task :spec_described, [:coverage] do |_task, args|
args.with_defaults(coverage: '100')

Check warning on line 260 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L260

Added line #L260 was not covered by tests

colorize = RSpec::Core::Formatters::ConsoleCodes
described = PuppetlabsSpecHelper::Tasks::SpecDescribed.new
result = described.check

Check warning on line 264 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L262-L264

Added lines #L262 - L264 were not covered by tests

puts "Spec described coverage: #{colorize.wrap(format('%3.1f%%', result[:percent]), described.coverage_color(result[:percent], args[:coverage], warn: 1))}"

Check warning on line 266 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L266

Added line #L266 was not covered by tests

if result[:have] < result[:want] || !result[:unknown].values.flatten.empty?
(result[:code].keys | result[:missing].keys).each do |res_type|
want_type = (result[:code][res_type]&.size || 0)
miss_type = (result[:missing][res_type]&.size || 0)
percent_type = (want_type - miss_type) / want_type.to_f * 100.0
color = described.coverage_color(percent_type, args[:coverage])
puts " * #{RSpec::Core::Formatters::Helpers.pluralize(want_type, res_type)}: #{colorize.wrap(format('%3.1f%%', percent_type), color)}"

Check warning on line 274 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L268-L274

Added lines #L268 - L274 were not covered by tests

describe_problems.each do |problem|
what = result[:"#{problem}"]
next if what[res_type].nil? || what[res_type].empty?

Check warning on line 278 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L276-L278

Added lines #L276 - L278 were not covered by tests

puts " #{problem}:"
what[res_type].each do |r|
info = " in #{result[:test_files][r]}" if result[:test_files][r]
puts " - #{r}#{info}"

Check warning on line 283 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L280-L283

Added lines #L280 - L283 were not covered by tests
end
end
end
end
abort if result[:percent] < args[:coverage].to_f

Check warning on line 288 in lib/puppetlabs_spec_helper/rake_tasks.rb

View check run for this annotation

Codecov / codecov/patch

lib/puppetlabs_spec_helper/rake_tasks.rb#L288

Added line #L288 was not covered by tests
end

desc 'Runs all necessary checks on a module in preparation for a release'
task :release_checks do
Rake::Task[:lint].invoke
Expand Down
80 changes: 80 additions & 0 deletions lib/puppetlabs_spec_helper/tasks/spec_described.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

module PuppetlabsSpecHelper
module Tasks
# Helper
class SpecDescribed
def hash_diff(first, second)
first.merge(first) { |ka, va| va.reject { |v| second[ka]&.include?(v) } }
end

def coverage_color(percent, required = 100, warn: 0.5)
if percent >= required.to_f
:green
elsif percent < required.to_f * warn.to_f
:red
else
:yellow
end
end

def check
code = {}
code_files = {}
Dir.glob('{functions,manifests,types}/**/*.pp') do |fn|
res_type = res_title = nil
File.foreach(fn) do |line|
if line =~ /^\s*(class|function|define|type|function)\s*([^={\s]+)/
res_type = Regexp.last_match(1)
res_title = Regexp.last_match(2)
res_type = 'type_alias' if res_type == 'type'
code[res_type] ||= []
break
end
end
if res_type
code[res_type] << res_title if res_type
code_files[res_title] = fn
end
end
Dir.glob('lib/puppet/functions/**/*.rb') do |fn|
File.foreach(fn) do |line|
if line =~ /^\s*Puppet::Functions\.create_function\(:?['"]?([^']+)['"]?\)/
code['function'] ||= []
code['function'] << Regexp.last_match(1)
code_files[Regexp.last_match(1)] = fn
end
end
end

test = {}
test_files = {}
Dir.glob('spec/{classes,defines,functions,type_aliases}/**/*rb') do |fn|
resource_type = fn.split(File::SEPARATOR)[1].match(/(class|function|define|type_alias)/).captures[0]
test[resource_type] ||= []
File.foreach(fn) do |line|
if (m = line.match(/^describe ["']([^'"\s]+)/))
test[resource_type] << m.captures[0]
test_files[m.captures[0]] = fn
end
end
end

results = {
code: code,
code_files: code_files,
test: test,
test_files: test_files,
missing: hash_diff(code, test),
unknown: hash_diff(test, code),
want: code.values.flatten.size
}

results[:have] = results[:want] - results[:missing].values.flatten.size
results[:percent] = results[:have] / results[:want].to_f * 100

results
end
end
end
end
Loading