File: index.md

package info (click to toggle)
ruby-rspec-puppet 2.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,416 kB
  • sloc: ruby: 6,661; makefile: 6
file content (90 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
layout: base
title: Coverage Reports
icon: fa fa-map-o
breadcrumbs:
    -
        name: Documentation
        path: /documentation/
---

## Basic Report

rspec-puppet can generate a basic resource coverage report at the end of the
test run by the following to your `spec/spec_helper.rb` file.

{% highlight ruby %}
RSpec.configure do |c|
  c.after(:suite) do
    RSpec::Puppet::Coverage.report!
  end
end
{% endhighlight %}

This checks which Puppet resources have been explicitly checked as part of the
test run and outputs both a coverage percentage and a list of untouched
resources.

<div class="callout-block callout-info">
<div class="icon-holder"><i class="fa fa-info-circle"></i></div>
<div class="content">
If you are using <code>parallel_tests</code> to speed up your rspec-puppet and
want to generate coverage reports, you <b>must</b> configure it in an
<code>after(:suite)</code> hook in <code>spec/spec_helper.rb</code> as
documented above and not with any other method (like an <code>at_exit</code>
hook in a spec file).
</div>
</div>

## Setting A Minimum Coverage Level

A desired code coverage percentage can be provided as an argument to
`RSpec::Puppet::Coverage.report!`.

{% highlight ruby %}
RSpec.configure do |c|
  c.after(:suite) do
    RSpec::Puppet::Coverage.report!(95)
  end
end
{% endhighlight %}

If this percentage is not achieved, a test failure will be raised.

## Excluded Resources

Resources declared outside of the module being tested (i.e. resources added by
module dependencies) are automatically excluded from the coverage report.

<div class="callout-block callout-info">
<div class="icon-holder"><i class="fa fa-info-circle"></i></div>
<div class="content">
Prior to Puppet 4.6.0, resources created by functions
(<code>create_resources</code>, <code>ensure_packages</code> etc) did not have
the required information in them to determine which manifest they came from and
so can not be excluded from the coverage report.
</div>
</div>

To exclude other resources from the coverage reports, for example to avoid `anchor`s,
use the `add_filter(type, title)` and `add_filter_regex(type, regex)` methods:

{% highlight ruby %}
RSpec.configure do |c|
  c.before(:suite) do
    # Exclude File[/tmp] from all coverage reports
    RSpec::Puppet::Coverage.add_filter('File', '/tmp')
    # Exclude all anchor resources from all coverage reports
    RSpec::Puppet::Coverage.add_filter_regex('Anchor', '.*')
  end
end
{% endhighlight %}

<div class="callout-block callout-info">
<div class="icon-holder"><i class="fa fa-exclamation-triangle"></i></div>
<div class="content">
Note that currently filters are global and do not get reset in between examples.
To avoid accidents you should only configure global excludes and only in the
`before(:suite)` hook.
</div>
</div>