Class: RSpec::Puppet::Coverage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rspec-puppet/coverage.rb

Defined Under Namespace

Classes: ResourceWrapper

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverage

Returns a new instance of Coverage.



30
31
32
33
# File 'lib/rspec-puppet/coverage.rb', line 30

def initialize
  @collection = {}
  @filters = ['Stage[main]', 'Class[Settings]', 'Class[main]', 'Node[default]']
end

Class Attribute Details

.instanceObject



25
26
27
# File 'lib/rspec-puppet/coverage.rb', line 25

def instance
  @instance ||= new
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



15
16
17
# File 'lib/rspec-puppet/coverage.rb', line 15

def filters
  @filters
end

Instance Method Details

#add(resource) ⇒ Object



35
36
37
38
39
# File 'lib/rspec-puppet/coverage.rb', line 35

def add(resource)
  if !exists?(resource) && !filtered?(resource)
    @collection[resource.to_s] = ResourceWrapper.new(resource)
  end
end

#add_filter(type, title) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec-puppet/coverage.rb', line 41

def add_filter(type, title)
  def capitalize_name(name)
    name.split('::').map { |subtitle| subtitle.capitalize }.join('::')
  end

  type = capitalize_name(type)
  if type == 'Class'
    title = capitalize_name(title)
  end

  @filters << "#{type}[#{title}]"
end

#add_from_catalog(catalog, test_module) ⇒ Object

add all resources from catalog declared in module test_module



55
56
57
58
59
60
# File 'lib/rspec-puppet/coverage.rb', line 55

def add_from_catalog(catalog, test_module)
  coverable_resources = catalog.to_a.reject { |resource| !test_module.nil? && filter_resource?(resource, test_module) }
  coverable_resources.each do |resource|
    add(resource)
  end
end

#capitalize_name(name) ⇒ Object



42
43
44
# File 'lib/rspec-puppet/coverage.rb', line 42

def capitalize_name(name)
  name.split('::').map { |subtitle| subtitle.capitalize }.join('::')
end

#cover!(resource) ⇒ Object



66
67
68
69
70
# File 'lib/rspec-puppet/coverage.rb', line 66

def cover!(resource)
  if !filtered?(resource) && (wrapper = find(resource))
    wrapper.touch!
  end
end

#coverage_test(coverage_desired, coverage_actual) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rspec-puppet/coverage.rb', line 100

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(RSpec::Core::NullReporter)
    passed = if coverage_results.execution_result.respond_to? :status then
               coverage_results.execution_result.status == :passed
             else
               coverage_results.execution_result[:status] == 'passed'
             end

    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

Returns:

  • (Boolean)


62
63
64
# File 'lib/rspec-puppet/coverage.rb', line 62

def filtered?(resource)
  filters.include?(resource.to_s)
end

#report!(coverage_desired = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rspec-puppet/coverage.rb', line 72

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

#resultsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rspec-puppet/coverage.rb', line 119

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