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.



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

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



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

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

#cover!(resource) ⇒ Object



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

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

#filtered?(resource) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#report!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rspec-puppet/coverage.rb', line 34

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