Skip to content

Commit e01f641

Browse files
committed
Simplify interface of coverageReport
- Remove the concept of "package boundaries" from the "coverageReport" function. - The "coverageReport" is now a function of: - arbitrary checks generating tix files - arbitrary mix modules - This more closely reflects the usage of hpc, which doesn't care about package boundaries. - Use this new "coverageReport" function to simplify the "projectCoverageReport" implementation. A project coverage report now simply: - copies out constituent coverage reports. - writes out an "all coverage report" using all checks in the project and all mix modules.. - writes out a summary index page.
1 parent 3820af5 commit e01f641

File tree

3 files changed

+103
-79
lines changed

3 files changed

+103
-79
lines changed

lib/cover-project.nix

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let
3838
</div>
3939
<div>
4040
<h2>Individual Reports</h2>
41-
<p>The following reports show how each module is covered by tests in its own package:</p>
41+
<p>The following reports show how the tests of each package cover modules in the project:</p>
4242
<ul>
4343
${with lib; concatStringsSep "\n" (map coverageListElement coverageReports)}
4444
</ul>
@@ -49,7 +49,7 @@ let
4949

5050
ghc = project.pkg-set.config.ghc.package;
5151

52-
libs = lib.remove null (map (r: r.library) coverageReports);
52+
libs = lib.unique (lib.concatMap (r: r.mixLibraries) coverageReports);
5353

5454
mixDirs =
5555
map
@@ -58,6 +58,17 @@ let
5858

5959
srcDirs = map (l: l.srcSubDirPath) libs;
6060

61+
allCoverageReport = haskellLib.coverageReport {
62+
name = "all";
63+
checks = lib.flatten (lib.concatMap
64+
(pkg: lib.optional (pkg ? checks) (lib.filter lib.isDerivation (lib.attrValues pkg.checks)))
65+
(lib.attrValues (haskellLib.selectProjectPackages project.hsPkgs)));
66+
mixLibraries = lib.concatMap
67+
(pkg: lib.optional (pkg.components ? library) pkg.components.library)
68+
(lib.attrValues (haskellLib.selectProjectPackages project.hsPkgs));
69+
ghc = project.pkg-set.config.ghc.package;
70+
};
71+
6172
in pkgs.runCommand "project-coverage-report"
6273
({ nativeBuildInputs = [ (ghc.buildGHC or ghc) pkgs.buildPackages.zip ];
6374
LANG = "en_US.UTF-8";
@@ -126,30 +137,14 @@ in pkgs.runCommand "project-coverage-report"
126137
cp -R $report/share/hpc/vanilla/html/* $out/share/hpc/vanilla/html/
127138
'') coverageReports)}
128139
129-
if [ ''${#tixFiles[@]} -ne 0 ]; then
130-
# Create tix file with test run information for all packages
131-
tixFile="$out/share/hpc/vanilla/tix/all/all.tix"
132-
hpcSumCmd=("hpc" "sum" "--union" "--output=$tixFile")
133-
hpcSumCmd+=("''${tixFiles[@]}")
134-
echo "''${hpcSumCmd[@]}"
135-
eval "''${hpcSumCmd[@]}"
136-
137-
# Markup a HTML coverage report for the entire project
138-
cp ${projectIndexHtml} $out/share/hpc/vanilla/html/index.html
139-
echo "report coverage-per-package $out/share/hpc/vanilla/html/index.html" >> $out/nix-support/hydra-build-products
140-
141-
local markupOutDir="$out/share/hpc/vanilla/html/all"
142-
local srcDirs=${toBashArray srcDirs}
143-
local mixDirs=${toBashArray mixDirs}
144-
local allMixModules=()
145-
146-
mkdir $markupOutDir
147-
findModules allMixModules "$out/share/hpc/vanilla/mix/" "*.mix"
140+
# Copy out "all" coverage report
141+
cp -R ${allCoverageReport}/share/hpc/vanilla/tix/all $out/share/hpc/vanilla/tix
142+
cp -R ${allCoverageReport}/share/hpc/vanilla/html/all $out/share/hpc/vanilla/html
148143
149-
markup srcDirs mixDirs allMixModules "$markupOutDir" "$tixFile"
144+
# Markup a HTML coverage summary report for the entire project
145+
cp ${projectIndexHtml} $out/share/hpc/vanilla/html/index.html
150146
151-
echo "report coverage $markupOutDir/hpc_index.html" >> $out/nix-support/hydra-build-products
152-
( cd $out/share/hpc/vanilla/html ; zip -r $out/share/hpc/vanilla/html.zip . )
153-
echo "file zip $out/share/hpc/vanilla/html.zip" >> $out/nix-support/hydra-build-products
154-
fi
147+
echo "report coverage $out/share/hpc/vanilla/html/index.html" >> $out/nix-support/hydra-build-products
148+
( cd $out/share/hpc/vanilla/html ; zip -r $out/share/hpc/vanilla/html.zip . )
149+
echo "file zip $out/share/hpc/vanilla/html.zip" >> $out/nix-support/hydra-build-products
155150
''

lib/cover.nix

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
# The following collects coverage information from a set of given "checks" and
2+
# provides a coverage report showing how those "checks" cover a set of given
3+
# "mixLibraries".
14
{ stdenv, lib, haskellLib, pkgs }:
25

3-
# Name of the coverage report, which should be unique
6+
# Name of the coverage report, which should be unique.
47
{ name
5-
# Library to check coverage of
6-
, library
7-
# List of check derivations that generate coverage
8-
, checks
9-
# List of other libraries to include in the coverage report. The
10-
# default value if just the derivation provided as the `library`
11-
# argument. Use a larger list of libraries if you would like the tests
12-
# of one local package to generate coverage for another.
13-
, mixLibraries ? [library]
14-
# hack for project-less projects
15-
, ghc ? library.project.pkg-set.config.ghc.package
8+
# List of check derivations that generate coverage.
9+
, checks ? []
10+
# List of libraries to include in the coverage report. If one of the above
11+
# checks generates coverage for a particular library, coverage will only
12+
# be included if that library is in this list.
13+
, mixLibraries ? []
14+
# Hack for project-less projects.
15+
, ghc ? if mixLibraries == [] then null else (lib.head mixLibraries).project.pkg-set.config.ghc.package
1616
}:
1717

1818
let
@@ -26,7 +26,7 @@ let
2626
in pkgs.runCommand (name + "-coverage-report")
2727
({nativeBuildInputs = [ (ghc.buildGHC or ghc) pkgs.buildPackages.zip ];
2828
passthru = {
29-
inherit name library checks;
29+
inherit name checks mixLibraries;
3030
};
3131
# HPC will fail if the Haskell file contains non-ASCII characters,
3232
# unless our locale is set correctly. This has been fixed, but we
@@ -92,56 +92,83 @@ in pkgs.runCommand (name + "-coverage-report")
9292
}
9393
9494
function findModules() {
95-
local searchDir=$2
95+
local -n result=$1
96+
local -n searchDirs=$2
9697
local pattern=$3
9798
98-
pushd $searchDir
99-
mapfile -d $'\0' $1 < <(find ./ -type f \
100-
-wholename "$pattern" -not -name "Paths*" \
101-
-exec basename {} \; \
102-
| sed "s/\.mix$//" \
103-
| tr "\n" "\0")
104-
popd
99+
for dir in "''${searchDirs[@]}"; do
100+
pushd $dir
101+
local temp=()
102+
mapfile -d $'\0' temp < <(find ./ -type f \
103+
-wholename "$pattern" -not -name "Paths*" \
104+
-exec basename {} \; \
105+
| sed "s/\.mix$//" \
106+
| tr "\n" "\0")
107+
result+=("''${temp[@]}")
108+
popd
109+
done
105110
}
106111
107-
local mixDirs=${toBashArray mixDirs}
108-
109112
mkdir -p $out/nix-support
110-
mkdir -p $out/share/hpc/vanilla/mix/${name}
113+
mkdir -p $out/share/hpc/vanilla/mix/
111114
mkdir -p $out/share/hpc/vanilla/tix/${name}
112115
mkdir -p $out/share/hpc/vanilla/html/${name}
113116
114-
# Copy over mix files verbatim
117+
local srcDirs=${toBashArray srcDirs}
118+
local mixDirs=${toBashArray mixDirs}
119+
120+
# Copy out mix files used for this report
115121
for dir in "''${mixDirs[@]}"; do
116122
if [ -d "$dir" ]; then
117-
cp -R "$dir"/* $out/share/hpc/vanilla/mix/${name}
123+
cp -R "$dir" $out/share/hpc/vanilla/mix/
118124
fi
119125
done
120126
121-
local srcDirs=${toBashArray srcDirs}
122-
local allMixModules=()
123-
local pkgMixModules=()
124-
125-
# The behaviour of stack coverage reports is to provide tix files
126-
# that include coverage information for every local package, but
127-
# to provide HTML reports that only include coverage info for the
128-
# current package. We emulate the same behaviour here. If the user
129-
# includes all local packages in the mix libraries argument, they
130-
# will get a coverage report very similar to stack.
131-
132-
# All mix modules
133-
findModules allMixModules "$out/share/hpc/vanilla/mix/${name}" "*.mix"
134-
# Only mix modules corresponding to this package
135-
findModules pkgMixModules "$out/share/hpc/vanilla/mix/${name}" "*${name}*/*.mix"
136-
137-
# For each test
127+
local mixModules=()
128+
# Mix modules for all packages in "mixLibraries"
129+
findModules mixModules mixDirs "*.mix"
130+
131+
# We need to make a distinction between library "exposed-modules" and
132+
# "other-modules" used in test suites:
133+
# - "exposed-modules" are addressed as "$library-$version-$hash/module"
134+
# - "other-modules" are addressed as "module"
135+
#
136+
# This complicates the code required to find the mix modules. For a given mix directory:
137+
#
138+
# mix
139+
# └── ntp-client-0.0.1
140+
# └── ntp-client-0.0.1-gYjRsBHUCaHX7ENcjHnw5
141+
# ├── Network.NTP.Client.mix
142+
# ├── Network.NTP.Client.Packet.mix
143+
# └── Network.NTP.Client.Query.mix
144+
#
145+
# Iff ntp-client uses "other-modules" in a test suite, both:
146+
# - "mix/ntp-client-0.0.1", and
147+
# - "mix/ntp-client-0.0.1/ntp-client-0.0.1-gYjRsBHUCaHX7ENcjHnw5"
148+
# need to be provided to hpc as search directories.
149+
#
150+
# I'd prefer to just exclude "other-modules", but I can't think of an easy
151+
# way to do that in bash.
152+
#
153+
# Here we expand the search dirs and modify the mix dirs accordingly:
154+
for dir in "''${mixDirs[@]}"; do
155+
local otherModulesSearchDirs=()
156+
# Simply consider any directory with a mix file as a search directory.
157+
mapfile -d $'\0' otherModulesSearchDirs < <(find $dir -type f \
158+
-wholename "*.mix" \
159+
-exec dirname {} \; \
160+
| uniq \
161+
| tr "\n" "\0")
162+
mixDirs+=("''${otherModulesSearchDirs[@]}")
163+
done
164+
138165
local tixFiles=()
139166
${lib.concatStringsSep "\n" (builtins.map (check: ''
140167
if [ -d "${check}/share/hpc/vanilla/tix" ]; then
141168
pushd ${check}/share/hpc/vanilla/tix
142169
143170
tixFile="$(find . -iwholename "*.tix" -type f -print -quit)"
144-
local newTixFile=$out/share/hpc/vanilla/tix/${name}/"$tixFile"
171+
local newTixFile=$out/share/hpc/vanilla/tix/${check.name}/"$(basename $tixFile)"
145172
146173
mkdir -p "$(dirname $newTixFile)"
147174
# Copy over the tix file verbatim
@@ -150,24 +177,25 @@ in pkgs.runCommand (name + "-coverage-report")
150177
# Add the tix file to our list
151178
tixFiles+=("$newTixFile")
152179
153-
# Create a coverage report for *just that test*
154-
markup srcDirs mixDirs pkgMixModules "$out/share/hpc/vanilla/html/${name}/${check.exeName}/" "$newTixFile"
180+
# Create a coverage report for *just that check* affecting any of the
181+
# "mixLibraries"
182+
markup srcDirs mixDirs mixModules "$out/share/hpc/vanilla/html/${check.name}/" "$newTixFile"
155183
156184
popd
157185
fi
158186
'') checks)
159187
}
160188
161-
# Sum tix files to create a tix file with all relevant tix
162-
# information and markup a HTML report from this info.
189+
# Sum tix files to create a tix file with tix information from all tests in
190+
# the package and markup a HTML report from this info.
163191
local sumTixFile="$out/share/hpc/vanilla/tix/${name}/${name}.tix"
164192
local markupOutDir="$out/share/hpc/vanilla/html/${name}"
165193
166-
# Sum all of our tix file, including modules from any local package
167-
sumTix allMixModules tixFiles "$sumTixFile"
194+
# Sum all of our tix files
195+
sumTix mixModules tixFiles "$sumTixFile"
168196
169-
# Markup a HTML report, included modules from only this package
170-
markup srcDirs mixDirs pkgMixModules "$markupOutDir" "$sumTixFile"
197+
# Markup a HTML report
198+
markup srcDirs mixDirs mixModules "$markupOutDir" "$sumTixFile"
171199
172200
# Provide a HTML zipfile and Hydra links
173201
( cd "$markupOutDir" ; zip -r $out/share/hpc/vanilla/${name}-html.zip . )

overlays/haskell.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,9 @@ final: prev: {
591591

592592
coverageReport = haskellLib.coverageReport (rec {
593593
name = package.identifier.name + "-" + package.identifier.version;
594-
library = if components ? library then components.library else null;
594+
# Include the checks for a single package.
595595
checks = final.lib.filter (final.lib.isDerivation) (final.lib.attrValues package'.checks);
596+
# Checks from that package may provide coverage information for any library in the project.
596597
mixLibraries = final.lib.concatMap
597598
(pkg: final.lib.optional (pkg.components ? library) pkg.components.library)
598599
(final.lib.attrValues (haskellLib.selectProjectPackages project.hsPkgs));

0 commit comments

Comments
 (0)