Skip to content

Commit 2ce5851

Browse files
committed
Tidy up code coverage
1 parent d164ab4 commit 2ce5851

File tree

3 files changed

+70
-56
lines changed

3 files changed

+70
-56
lines changed

doc/coverage.md

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
<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>
22

3-
# Code Coverage
3+
# Code coverage
44

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+
~~~
617

718
## Usage
819

920
`stack test --coverage` is quite streamlined for the following use-case:
1021

11-
1. You have test-suites which exercise your local packages.
22+
1. You have test suites which exercise your local packages.
1223

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.
1931

2032
When your project has these properties, you will get the following:
2133

22-
1) Textual coverage reports in the build output.
34+
1. Textual coverage reports in the build output.
2335

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.
2638

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.
2941

30-
## "stack hpc report" command
42+
## The `stack hpc report` command
3143

3244
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,
3446
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:
3648

3749
~~~text
3850
stack hpc report A B
3951
~~~
4052

4153
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
4355
streamline this process, you can pass the `--open` option, to open the report in
4456
your browser.
4557

@@ -56,19 +68,19 @@ This report will consider all test results as well as the newly generated
5668
`an-exe.tix` file. Since this is a common use-case, there is a convenient flag
5769
to use all stored results - `stack hpc report --all an-exe.tix`.
5870

59-
## "extra-tix-files" directory
71+
## The `extra-tix-files` directory
6072

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:
6476

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.
6981

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.
7284

7385
## Implementation details
7486

@@ -81,41 +93,43 @@ However, advanced users may want to understand exactly how `--coverage` works:
8193
the executable.
8294

8395
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)).
8598

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
88101
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.
90103

91104
3. Before a test run, if a `test-name.tix` file exists in the package directory,
92105
it will be deleted.
93106

94107
4. After a test run, it will expect a `test-name.tix` file to exist. This file
95108
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.
97110

98111
The `.tix` file gets modified to remove coverage file that isn't associated
99112
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
101114
file. This makes it possible to directly union multiple `*.tix` files from
102115
different executables (assuming they are using the exact same versions of the
103116
local packages).
104117

105118
If there is enough popular demand, it may be possible in the future to give
106119
coverage information for modules that are compiled directly into the
107-
executable. See
120+
executable. See issue
108121
[#1359](https://github.com/commercialhaskell/stack/issues/1359).
109122

110123
5. Once we have a `.tix` file for a test, we also generate a textual and HTML
111124
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.
114127

115128
6. After the build completes, if there are multiple output `*.tix` files, they
116129
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.
118131

119132
7. Finally, an index of the resulting coverage reports is generated. It links to
120133
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.

doc/shell_autocompletion.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ within Stack is still lacking. For further information, see issue
1515
eval "$(stack --bash-completion-script stack)"
1616
~~~
1717

18-
!!! info
18+
!!! info
1919

20-
Stack's hidden option `--bash-completion-script <stack_executable_name>`
21-
outputs a command that can be evaluated by Bash. For example:
20+
Stack's hidden option `--bash-completion-script <stack_executable_name>`
21+
outputs a command that can be evaluated by Bash. For example:
2222

23-
~~~text
24-
stack --bash-completion-script stack
25-
_stack.exe()
26-
{
27-
local CMDLINE
28-
local IFS=$'\n'
29-
CMDLINE=(--bash-completion-index $COMP_CWORD)
23+
~~~text
24+
stack --bash-completion-script stack
25+
_stack.exe()
26+
{
27+
local CMDLINE
28+
local IFS=$'\n'
29+
CMDLINE=(--bash-completion-index $COMP_CWORD)
3030

31-
for arg in ${COMP_WORDS[@]}; do
32-
CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)
33-
done
31+
for arg in ${COMP_WORDS[@]}; do
32+
CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)
33+
done
3434

35-
COMPREPLY=( $(stack "${CMDLINE[@]}") )
36-
}
37-
~~~
35+
COMPREPLY=( $(stack "${CMDLINE[@]}") )
36+
}
37+
~~~
3838

3939
=== "Zsh"
4040

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ nav:
3434
- Snapshot specification: custom_snapshot.md
3535
- stack.yaml vs a Cabal file: stack_yaml_vs_cabal_package_file.md
3636
- Build command: build_command.md
37+
- Code coverage: coverage.md
3738
- Stack and Visual Studio Code: Stack_and_VS_Code.md
3839
- Developing on Windows: developing_on_windows.md
3940
- Dependency visualization: dependency_visualization.md
@@ -43,7 +44,6 @@ nav:
4344
- Shell auto-completion: shell_autocompletion.md
4445
- Travis CI: travis_ci.md
4546
- Azure CI: azure_ci.md
46-
- Code coverage: coverage.md
4747
- GHCi: ghci.md
4848
- Lock files: lock_files.md
4949
- Advanced documentation:

0 commit comments

Comments
 (0)