Skip to content

Commit 29ac893

Browse files
committed
Make coverageReport and projectCoverageReport functions
- Make the "coverageReport" and "projectCoverageReport" functions available to the user. - The existing "coverageReport" and "projectCoverageReport" attributes have been renamed to "coverageReport'" and "projectCoverageReport'". They remain for convenience. - Updated the documentation accordingly.
1 parent f3a6bdc commit 29ac893

File tree

5 files changed

+70
-48
lines changed

5 files changed

+70
-48
lines changed

docs/tutorials/coverage.md

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,60 @@
33
haskell.nix can generate coverage information for your package or
44
project using Cabal's inbuilt hpc support.
55

6-
## Pre-requisites
7-
8-
It is currently required that you enable coverage for each library you
9-
want coverage for prior to attempting to generate a coverage report. I
10-
hope to fix this before merging this PR:
11-
12-
```nix
13-
haskell-nix.cabalProject ({
14-
src = pkgs.haskell-nix.haskellLib.cleanGit {
15-
name = "haskell-nix-project";
16-
src = ./.;
17-
};
18-
modules = [
19-
{
20-
packages.package-1.components.library.doCoverage = true;
21-
packages.package-2.components.library.doCoverage = true;
22-
}
23-
];
24-
});
25-
```
26-
276
## Per-package
287

298
```bash
30-
nix-build default.nix -A $pkg.coverageReport
9+
nix-build default.nix -A "$pkg.coverageReport'"
3110
```
3211

33-
This will generate a coverage report for the package you requested.
12+
This will generate a coverage report for the package you requested. By
13+
default, all tests that are enabled (configured with
14+
`doCheck == true`) are included in the coverage report.
3415

3516
See the [developer coverage docs](../dev/coverage.md#package-reports) for more information.
3617

3718
## Project-wide
3819

3920
```bash
40-
nix-build default.nix -A projectCoverageReport
21+
nix-build default.nix -A "projectCoverageReport'"
4122
```
4223

4324
This will generate a coverage report for all the local packages in
4425
your project.
4526

4627
See the [developer coverage docs](../dev/coverage.md#project-wide-reports) for more information.
28+
29+
## Custom
30+
31+
haskell.nix also exposes two functions which allow you to generate
32+
custom coverage reports: `coverageReport` and `projectCoverageReport`.
33+
These are found in the haskell.nix library:
34+
35+
```nix
36+
let
37+
inherit (pkgs.haskell-nix) haskellLib;
38+
39+
project = pkgs.haskell-nix.project {
40+
# 'cleanGit' cleans a source directory based on the files known by git
41+
src = pkgs.haskell-nix.haskellLib.cleanGit {
42+
name = "haskell-nix-project";
43+
src = ./.;
44+
};
45+
# For `cabal.project` based projects specify the GHC version to use.
46+
compiler-nix-name = "ghc884"; # Not used for `stack.yaml` based projects.
47+
};
48+
49+
custom$pkgCoverageReport = haskellLib.coverageReport {
50+
inherit (project.$pkg.identifier) name version;
51+
inherit (project.$pkg.components) library tests;
52+
};
53+
54+
customProjectCoverageReport = haskellLib.projectCoverageReport {
55+
packages = haskellLib.selectProjectPackages project;
56+
coverageReportOverrides = { "${project.$pkg.identifier.name}" = custom$pkgCoverageReport; };
57+
};
58+
in project // {
59+
inherit custom$pkgCoverageReport customProjectCoverageReport;
60+
}
61+
62+
```

lib/cover-project.nix

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{ pkgs, stdenv, lib, haskellLib }:
22

3-
project:
3+
# List of project packages to generate a coverage report for
4+
{ packages
5+
, coverageReportOverrides ? {}
6+
}:
47

58
let
9+
getPackageCoverageReport = packageName: (coverageReportOverrides."${packageName}" or packages."${packageName}".coverageReport');
610

711
# Create table rows for an project coverage index page that look something like:
812
#
@@ -55,7 +59,7 @@ let
5559
<th>TestSuite</th>
5660
</tr>
5761
58-
${with lib; concatStringsSep "\n" (mapAttrsToList (_ : packageTableRows) project)}
62+
${with lib; concatStringsSep "\n" (mapAttrsToList (_ : packageTableRows) packages)}
5963
6064
</tbody>
6165
</table>
@@ -92,7 +96,7 @@ stdenv.mkDerivation {
9296
9397
${with lib; concatStringsSep "\n" (mapAttrsToList (n: package: ''
9498
identifier="${package.identifier.name}-${package.identifier.version}"
95-
report=${package.coverageReport}
99+
report=${getPackageCoverageReport n}
96100
tix="$report/share/hpc/tix/$identifier/$identifier.tix"
97101
if test -f "$tix"; then
98102
tixFiles+=("$tix")
@@ -102,7 +106,7 @@ stdenv.mkDerivation {
102106
cp -R $report/share/hpc/mix/* $out/share/hpc/mix
103107
cp -R $report/share/hpc/tix/* $out/share/hpc/tix
104108
cp -R $report/share/hpc/html/* $out/share/hpc/html
105-
'') project)}
109+
'') packages)}
106110
107111
if [ ''${#tixFiles[@]} -ne 0 ]; then
108112
hpcSumCmd+=("''${tixFiles[@]}")

lib/cover.nix

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
{ stdenv, lib, haskellLib, pkgs }:
22

3-
{ name, version, library, tests, plan }:
3+
{ name
4+
, version
5+
, library
6+
, tests
7+
}:
48

59
let
610
buildWithCoverage = builtins.map (d: d.covered);
711
runCheck = builtins.map (d: haskellLib.check d);
812

13+
testsAsList = lib.attrValues tests;
914
libraryCovered = library.covered;
10-
testsWithCoverage = buildWithCoverage tests;
15+
testsWithCoverage = buildWithCoverage testsAsList;
1116
checks = runCheck testsWithCoverage;
1217

1318
identifier = name + "-" + version;
1419

15-
getTestModulesFor = test: plan.components.tests."${test.exeName}".modules;
16-
1720
in stdenv.mkDerivation {
1821
name = (identifier + "-coverage-report");
1922

@@ -41,11 +44,11 @@ in stdenv.mkDerivation {
4144
hpcMarkupCmdBase+=("--hpcdir=$mixDir")
4245
done
4346
44-
${lib.optionalString ((builtins.length tests) > 0) ''
47+
${lib.optionalString ((builtins.length testsAsList) > 0) ''
4548
# Exclude test modules from tix file
4649
excludedModules=('Main')
4750
# Exclude test modules
48-
testModules="${with lib; concatStringsSep " " (foldl' (acc: test: acc ++ (getTestModulesFor test)) [] testsWithCoverage)}"
51+
testModules="${with lib; concatStringsSep " " (foldl' (acc: test: acc ++ test.config.modules) [] testsWithCoverage)}"
4952
for module in $testModules; do
5053
excludedModules+=("$module")
5154
done

overlays/haskell.nix

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ final: prev: {
480480
inherit (callProjectResults) index-state;
481481
};
482482
in project // {
483-
projectCoverageReport = haskellLib.projectCoverageReport (haskellLib.selectProjectPackages project.hsPkgs);
483+
projectCoverageReport' = haskellLib.projectCoverageReport { packages = haskellLib.selectProjectPackages project.hsPkgs; };
484484
};
485485

486486
# Take `hsPkgs` from the `rawProject` and update all the packages and
@@ -503,11 +503,10 @@ final: prev: {
503503
) package'.components;
504504
inherit project;
505505

506-
coverageReport = haskellLib.coverageReport {
506+
coverageReport' = haskellLib.coverageReport {
507507
inherit (package.identifier) name version;
508508
inherit (components) library;
509-
tests = with final.lib; attrValues (filterAttrs (_: d: d.config.doCheck) components.tests);
510-
plan = project.pkg-set.config.packages."${package.identifier.name}";
509+
tests = final.lib.filterAttrs (_: d: d.config.doCheck) components.tests;
511510
};
512511
}
513512
) rawProject.hsPkgs
@@ -519,7 +518,7 @@ final: prev: {
519518

520519
cabalProject = args: let p = cabalProject' args;
521520
in p.hsPkgs // {
522-
inherit (p) plan-nix projectCoverageReport;
521+
inherit (p) plan-nix projectCoverageReport';
523522
# Provide `nix-shell -A shells.ghc` for users migrating from the reflex-platform.
524523
# But we should encourage use of `nix-shell -A shellFor`
525524
shells.ghc = p.hsPkgs.shellFor {};
@@ -543,12 +542,12 @@ final: prev: {
543542
stack-nix = callProjectResults.projectNix;
544543
};
545544
in project // {
546-
projectCoverageReport = haskellLib.projectCoverageReport (haskellLib.selectProjectPackages project.hsPkgs);
545+
projectCoverageReport' = haskellLib.projectCoverageReport { packages = haskellLib.selectProjectPackages project.hsPkgs; };
547546
};
548547

549548
stackProject = args: let p = stackProject' args;
550549
in p.hsPkgs // {
551-
inherit (p) stack-nix;
550+
inherit (p) stack-nix projectCoverageReport';
552551
# Provide `nix-shell -A shells.ghc` for users migrating from the reflex-platform.
553552
# But we should encourage use of `nix-shell -A shellFor`
554553
shells.ghc = p.hsPkgs.shellFor {};

test/coverage/default.nix

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ let
1515
}];
1616
};
1717

18-
cabalProject = cabalProject' projectArgs;
19-
stackProject = stackProject' projectArgs;
18+
cabalProj = cabalProject' projectArgs;
19+
stackProj = stackProject' projectArgs;
2020

2121
in recurseIntoAttrs ({
2222
run = stdenv.mkDerivation {
@@ -75,12 +75,12 @@ in recurseIntoAttrs ({
7575
}
7676
7777
${concatStringsSep "\n" (map (project: ''
78-
pkga_basedir="${project.hsPkgs.pkga.coverageReport}/share/hpc/"
78+
pkga_basedir="${project.hsPkgs.pkga.coverageReport'}/share/hpc/"
7979
findFileExistsNonEmpty "$pkga_basedir/mix/pkga-0.1.0.0/" "PkgA.mix"
8080
dirExistsEmpty "$pkga_basedir/tix/pkga-0.1.0.0"
8181
dirExistsEmpty "$pkga_basedir/html/pkga-0.1.0.0"
8282
83-
pkgb_basedir="${project.hsPkgs.pkgb.coverageReport}/share/hpc/"
83+
pkgb_basedir="${project.hsPkgs.pkgb.coverageReport'}/share/hpc/"
8484
testTix="$pkgb_basedir/tix/tests/tests.tix"
8585
libTix="$pkgb_basedir/tix/pkgb-0.1.0.0/pkgb-0.1.0.0.tix"
8686
fileExistsNonEmpty "$testTix"
@@ -98,7 +98,7 @@ in recurseIntoAttrs ({
9898
exit 1
9999
fi
100100
101-
project_basedir="${project.projectCoverageReport}/share/hpc/"
101+
project_basedir="${project.projectCoverageReport'}/share/hpc/"
102102
fileExistsNonEmpty "$project_basedir/html/index.html"
103103
fileExistsNonEmpty "$project_basedir/tix/all/all.tix"
104104
dirExists "$project_basedir/html/pkga-0.1.0.0"
@@ -111,7 +111,7 @@ in recurseIntoAttrs ({
111111
dirExists "$project_basedir/tix/pkga-0.1.0.0"
112112
dirExists "$project_basedir/tix/pkgb-0.1.0.0"
113113
dirExists "$project_basedir/tix/tests"
114-
'') [ cabalProject stackProject ])}
114+
'') [ cabalProj stackProj ])}
115115
116116
touch $out
117117
'';
@@ -120,7 +120,7 @@ in recurseIntoAttrs ({
120120

121121
passthru = {
122122
# Used for debugging with nix repl
123-
inherit cabalProject stackProject;
123+
inherit cabalProj stackProj;
124124
};
125125
};
126126
})

0 commit comments

Comments
 (0)