Class: RSpec::Puppet::Coverage
- Inherits:
-
Object
- Object
- RSpec::Puppet::Coverage
- Extended by:
- Forwardable
- Defined in:
- lib/rspec-puppet/coverage.rb
Defined Under Namespace
Classes: ResourceWrapper
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the value of attribute filters.
Instance Method Summary collapse
- #add(resource) ⇒ Object
- #add_filter(type, title) ⇒ Object
-
#add_from_catalog(catalog, test_module) ⇒ Object
add all resources from catalog declared in module test_module.
- #cover!(resource) ⇒ Object
- #coverage_test(coverage_desired, coverage_actual) ⇒ Object
- #filtered?(resource) ⇒ Boolean
-
#initialize ⇒ Coverage
constructor
A new instance of Coverage.
- #report!(coverage_desired = nil) ⇒ Object
- #results ⇒ Object
Constructor Details
#initialize ⇒ Coverage
Returns a new instance of Coverage.
18 19 20 21 |
# File 'lib/rspec-puppet/coverage.rb', line 18 def initialize @collection = {} @filters = ['Stage[main]', 'Class[Settings]', 'Class[main]'] end |
Class Attribute Details
.instance ⇒ Object
13 14 15 |
# File 'lib/rspec-puppet/coverage.rb', line 13 def instance @instance ||= new end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
4 5 6 |
# File 'lib/rspec-puppet/coverage.rb', line 4 def filters @filters end |
Instance Method Details
#add(resource) ⇒ Object
23 24 25 26 27 |
# File 'lib/rspec-puppet/coverage.rb', line 23 def add(resource) if !exists?(resource) && !filtered?(resource) @collection[resource.to_s] = ResourceWrapper.new(resource) end end |
#add_filter(type, title) ⇒ Object
29 30 31 |
# File 'lib/rspec-puppet/coverage.rb', line 29 def add_filter(type, title) @filters << "#{type.capitalize}[#{title.capitalize}]" end |
#add_from_catalog(catalog, test_module) ⇒ Object
add all resources from catalog declared in module test_module
34 35 36 37 38 39 |
# File 'lib/rspec-puppet/coverage.rb', line 34 def add_from_catalog(catalog, test_module) coverable_resources = catalog.to_a.select { |resource| !filter_resource?(resource, test_module) } coverable_resources.each do |resource| add(resource) end end |
#cover!(resource) ⇒ Object
45 46 47 48 49 |
# File 'lib/rspec-puppet/coverage.rb', line 45 def cover!(resource) if !filtered?(resource) && (wrapper = find(resource)) wrapper.touch! end end |
#coverage_test(coverage_desired, coverage_actual) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rspec-puppet/coverage.rb', line 79 def coverage_test(coverage_desired, coverage_actual) if coverage_desired.is_a?(Numeric) && coverage_desired.to_f <= 100.00 && coverage_desired.to_f >= 0.0 coverage_test = RSpec.describe("Code coverage.") coverage_results = coverage_test.example("Must be at least #{coverage_desired}% of code coverage") { expect( coverage_actual.to_f ).to be >= coverage_desired.to_f } coverage_test.run passed = coverage_results.execution_result.status == :passed RSpec.configuration.reporter.example_failed coverage_results unless passed else puts "The desired coverage must be 0 <= x <= 100, not '#{coverage_desired.inspect}'" end end |
#filtered?(resource) ⇒ Boolean
41 42 43 |
# File 'lib/rspec-puppet/coverage.rb', line 41 def filtered?(resource) filters.include?(resource.to_s) end |
#report!(coverage_desired = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rspec-puppet/coverage.rb', line 51 def report!(coverage_desired = nil) report = results puts <<-EOH.gsub(/^ {8}/, '') Total resources: #{report[:total]} Touched resources: #{report[:touched]} Resource coverage: #{report[:coverage]}% EOH if report[:coverage] != "100.00" puts <<-EOH.gsub(/^ {10}/, '') Untouched resources: #{ untouched_resources = report[:resources].reject do |_,rsrc| rsrc[:touched] end untouched_resources.inject([]) do |memo, (name,_)| memo << " #{name}" end.sort.join("\n") } EOH if coverage_desired coverage_test(coverage_desired, report[:coverage]) end end end |
#results ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rspec-puppet/coverage.rb', line 94 def results 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 end |