1
1
<div class =" hidden-warning " ><a href =" https://docs.haskellstack.org/ " ><img src =" https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg " ></a ></div >
2
2
3
- # Code Coverage
3
+ # Code coverage
4
4
5
- Code coverage is enabled by passing the ` --coverage ` flag to ` stack build ` .
5
+ Code coverage is a measure of the degree to which the source code of a program
6
+ is executed when a test suite is run.
7
+ [ Haskell Program Coverage (HPC)] ( https://ku-fpg.github.io/software/hpc/ ) is a
8
+ code coverage tool for Haskell that is provided with GHC. Code coverage is
9
+ enabled by passing the ` --coverage ` flag to ` stack build ` .
10
+
11
+ The following refers to the local HPC root directory. Its location can be
12
+ obtained by command:
13
+
14
+ ~~~ text
15
+ stack path --local-hpc-root
16
+ ~~~
6
17
7
18
## Usage
8
19
9
20
` stack test --coverage ` is quite streamlined for the following use-case:
10
21
11
- 1 . You have test- suites which exercise your local packages.
22
+ 1 . You have test suites which exercise your local packages.
12
23
13
- 2 . These test-suites link against your library, rather than building the library
14
- directly. Coverage information is only given for libraries, ignoring the
15
- modules which get compiled directly into your executable. A common case where
16
- this doesn't happen is when your test-suite and library both have something
17
- like ` hs-source-dirs: src/ ` . In this case, when building your test-suite you
18
- may also be compiling your library, instead of just linking against it.
24
+ 2 . These test suites link against your library, rather than building the
25
+ library directly. Coverage information is only given for libraries, ignoring
26
+ the modules which get compiled directly into your executable. A common case
27
+ where this doesn't happen is when your test suite and library both have
28
+ something like ` hs-source-dirs: src/ ` . In this case, when building your test
29
+ suite you may also be compiling your library, instead of just linking
30
+ against it.
19
31
20
32
When your project has these properties, you will get the following:
21
33
22
- 1 ) Textual coverage reports in the build output.
34
+ 1 . Textual coverage reports in the build output.
23
35
24
- 2 ) A unified textual and HTML report, considering the coverage on all local
25
- libraries, based on all of the tests that were run.
36
+ 2 . A unified textual and HTML report, considering the coverage on all local
37
+ libraries, based on all of the tests that were run.
26
38
27
- 3 ) An index of all generated HTML reports, at
28
- ` $(stack path --local-hpc- root)/index.html ` .
39
+ 3 . An index of all generated HTML reports, in ` index.html ` in the the local
40
+ HPC root directory .
29
41
30
- ## " stack hpc report" command
42
+ ## The ` stack hpc report ` command
31
43
32
44
The ` stack hpc report ` command generates a report for a selection of targets and
33
- ` .tix ` files. For example, if we have 3 different packages with test- suites,
45
+ ` .tix ` files. For example, if we have three different packages with test suites,
34
46
packages ` A ` , ` B ` , and ` C ` , the default unified report will have coverage from
35
- all 3. If we want a unified report with just two, we can instead command:
47
+ all three. If we want a unified report with just two, we can instead command:
36
48
37
49
~~~ text
38
50
stack hpc report A B
39
51
~~~
40
52
41
53
This will output a textual report for the combined coverage from ` A ` and ` B ` 's
42
- test- suites, along with a path to the HTML for the report. To further
54
+ test suites, along with a path to the HTML for the report. To further
43
55
streamline this process, you can pass the ` --open ` option, to open the report in
44
56
your browser.
45
57
@@ -56,19 +68,19 @@ This report will consider all test results as well as the newly generated
56
68
` an-exe.tix ` file. Since this is a common use-case, there is a convenient flag
57
69
to use all stored results - ` stack hpc report --all an-exe.tix ` .
58
70
59
- ## " extra-tix-files" directory
71
+ ## The ` extra-tix-files ` directory
60
72
61
- During the execution of the build, you can place additional tix files in
62
- ` $(stack path --local-hpc-root)/ extra-tix-files/ ` in order for them to be
63
- included in the unified report. A couple caveats:
73
+ During the execution of the build, you can place additional tix files in the
74
+ ` extra-tix-files ` subdirectory in the local HPC root directory, in order for
75
+ them to be included in the unified report. A couple caveats:
64
76
65
- * These tix files must be generated by executables that are built against the
66
- exact same library versions. Also note that, on subsequent builds with coverage,
67
- the ` $(stack path -- local-hpc- root) ` directory will be recursively deleted. It
68
- just stores the most recent coverage data.
77
+ 1 . These tix files must be generated by executables that are built against the
78
+ exact same library versions. Also note that, on subsequent builds with
79
+ coverage, the local HPC root directory will be recursively deleted. It
80
+ just stores the most recent coverage data.
69
81
70
- * These tix files will not be considered by ` stack hpc report ` unless listed
71
- explicitly by file name.
82
+ 2 . These tix files will not be considered by ` stack hpc report ` unless listed
83
+ explicitly by file name.
72
84
73
85
## Implementation details
74
86
@@ -81,41 +93,43 @@ However, advanced users may want to understand exactly how `--coverage` works:
81
93
the executable.
82
94
83
95
When switching on this flag, it will usually cause all local packages to be
84
- rebuilt (see [ #1940 ] ( https://github.com/commercialhaskell/stack/issues/1940 ) ).
96
+ rebuilt (see issue
97
+ [ #1940 ] ( https://github.com/commercialhaskell/stack/issues/1940 ) ).
85
98
86
- 2 . Before the build runs with ` --coverage ` , the contents of ` stack path -- local-hpc- root`
87
- gets deleted. This prevents old reports from getting mixed
99
+ 2 . Before the build runs with ` --coverage ` , the contents of the local HPC root
100
+ directory gets deleted. This prevents old reports from getting mixed
88
101
with new reports. If you want to preserve report information from multiple
89
- runs, copy the contents of this path to a new folder .
102
+ runs, copy the contents of this path to a new directory .
90
103
91
104
3 . Before a test run, if a ` test-name.tix ` file exists in the package directory,
92
105
it will be deleted.
93
106
94
107
4 . After a test run, it will expect a ` test-name.tix ` file to exist. This file
95
108
will then get loaded, modified, and outputted to
96
- ` $(stack path --local-hpc-root)/ pkg-name/test-name/test-name.tix) ` .
109
+ ` pkg-name/test-name/test-name.tix ` in the local HPC root directory .
97
110
98
111
The ` .tix ` file gets modified to remove coverage file that isn't associated
99
112
with a library. So, this means that you won't get coverage information for
100
- the modules compiled in the ` executable ` or ` test-suite ` stanza of your cabal
113
+ the modules compiled in the ` executable ` or ` test-suite ` stanza of your Cabal
101
114
file. This makes it possible to directly union multiple ` *.tix ` files from
102
115
different executables (assuming they are using the exact same versions of the
103
116
local packages).
104
117
105
118
If there is enough popular demand, it may be possible in the future to give
106
119
coverage information for modules that are compiled directly into the
107
- executable. See
120
+ executable. See issue
108
121
[ #1359 ] ( https://github.com/commercialhaskell/stack/issues/1359 ) .
109
122
110
123
5 . Once we have a ` .tix ` file for a test, we also generate a textual and HTML
111
124
report for it. The textual report is sent to the terminal. The index of the
112
- test-specific HTML report is available at
113
- ` $(stack path --local-hpc- root)/pkg-name/test-name/index.html `
125
+ test-specific HTML report is available ` pkg-name/test-name/index.html ` in the
126
+ local HPC root directory.
114
127
115
128
6 . After the build completes, if there are multiple output ` *.tix ` files, they
116
129
get combined into a unified report. The index of this report will be
117
- available at ` $(stack path --local-hpc-root)/ combined/all/index.html`
130
+ available at ` combined/all/index.html ` in the local HPC root directory.
118
131
119
132
7 . Finally, an index of the resulting coverage reports is generated. It links to
120
133
the individual coverage reports (one for each test-suite), as well as the
121
- unified report. This index is available at ` $(stack path --local-hpc-root)/index.html `
134
+ unified report. This index is available at ` index.html ` in the local HPC root
135
+ directory.
0 commit comments