Class: RSpec::Puppet::Coverage

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

Defined Under Namespace

Classes: ResourceWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverage

Returns a new instance of Coverage.



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

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

Instance Attribute Details

#filtersObject

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



19
20
21
22
23
# File 'lib/rspec-puppet/coverage.rb', line 19

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

#add_filter(type, title) ⇒ Object



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

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



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

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



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

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

#filtered?(resource) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#report!Object



47
48
49
50
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
78
79
80
81
# File 'lib/rspec-puppet/coverage.rb', line 47

def report!
  report = {}

  report[:total] = @collection.size
  report[:touched] = @collection.count { |_, resource| resource.touched? }
  report[:untouched] = report[:total] - report[:touched]
  report[:coverage] = sprintf("%5.2f", ((report[:touched].to_f/report[:total].to_f)*100))

  report[:detailed] = Hash[*@collection.map do |name, wrapper|
    [name, wrapper.to_hash]
  end.flatten]

  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[:detailed].reject do |_,rsrc|
          rsrc["touched"]
        end
        untouched_resources.inject([]) do |memo, (name,_)|
          memo << "  #{name}"
        end.sort.join("\n")
      }
    EOH
  end

end