Skip to content

Commit 69902c7

Browse files
committed
Update doco, minor fixes, remove override modules
- Update doco - Fix tests - Remove override modules
1 parent 972f320 commit 69902c7

File tree

6 files changed

+122
-134
lines changed

6 files changed

+122
-134
lines changed

changelog.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ that will impact users.
88
packages to enable coverage for.
99
* Added a `doCoverage` flag to the component builder that outputs HPC
1010
information when coverage is enabled.
11-
* Added the `overrideModules` library function to make it more
12-
ergonomic fo users to enable coverage on existing projects.
1311
* Added test for coverage.
1412

1513
## July 21, 2020

docs/dev/coverage.md

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,44 @@ The coverage information generated will look something like this:
3232
└── share
3333
└── hpc
3434
└── vanilla
35+
├── html
36+
│   └── my-library-0.1.0.0
37+
│   ├── my-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
38+
│ │ ├── My.Lib.Config.hs.html
39+
│ │ ├── My.Lib.Types.hs.html
40+
│ │ └── My.Lib.Util.hs.html
41+
│   ├── hpc_index_alt.html
42+
│   ├── hpc_index_exp.html
43+
│   ├── hpc_index_fun.html
44+
│   └── hpc_index.html
3545
├── mix
36-
│   ├── my-library-0.1.0.0
37-
│   │   └── my-library-0.1.0.0-ERSaOroBZhe9awsoBkhmcV
38-
│   │   ├── My.Lib.Config.mix
39-
│   │   ├── My.Lib.Types.mix
40-
│   │   └── My.Lib.Util.mix
41-
│   └── my-test-1
42-
│   ├── Spec.mix
43-
│   └── Main.mix
44-
├── tix
45-
│ ├── my-library-0.1.0.0
46-
│ │   └── my-library-0.1.0.0.tix
47-
│ └── my-test-1
48-
│ └── my-test-1.tix
49-
└── html
50-
└── my-library-0.1.0.0
51-
├── my-library-0.1.0.0-ERSaOroBZhe9awsoBkhmcV
52-
│ ├── My.Lib.Config.hs.html
53-
│ ├── My.Lib.Types.hs.html
54-
│ └── My.Lib.Util.hs.html
55-
├── hpc_index_alt.html
56-
├── hpc_index_exp.html
57-
├── hpc_index_fun.html
58-
└── hpc_index.html
46+
│   └── my-library-0.1.0.0
47+
│   └── my-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
48+
│      ├── My.Lib.Config.mix
49+
│      ├── My.Lib.Types.mix
50+
│      └── My.Lib.Util.mix
51+
└── tix
52+
├── my-library-0.1.0.0
53+
│   └── my-library-0.1.0.0.tix
54+
└── my-library-test
55+
└── my-library-test.tix
5956
```
6057

61-
- The mix files are copied verbatim from the builds with coverage.
58+
- The mix files are copied verbatim from the library built with
59+
coverage.
6260
- The tix files for each test are copied from the check run verbatim.
6361
- The tix files for each library are generated by summing the tix
6462
files for each test, but excluding any test modules.
6563
- Test modules are determined by inspecting the plan for the project
6664
(i.e. for the project "my-project" and test-suite "my-test-1", the
6765
test modules are read from:
68-
`my-project.project.pkg-set.config.packages.my-project.components.tests.my-test-1.modules`)
69-
- The hpc reports for each component are generated from their
70-
respective tix files.
71-
- `html/my-library-0.1.0.0/hpc_index.html` contains coverage
72-
information for the library, excluding any test modules.
66+
`my-project.checks.my-test-1.config.modules`)
67+
- The hpc HTML reports for each library are generated from their
68+
respective tix files (i.e. the
69+
`share/hpc/vanilla/html/my-library-0.1.0.0` report is generated from
70+
the
71+
`share/hpc/vanilla/tix/my-library-0.1.0.0/my-library-0.1.0.0.tix`
72+
file)
7373

7474
### Project-wide reports
7575

@@ -81,51 +81,46 @@ like this:
8181
└── share
8282
└── hpc
8383
└── vanilla
84+
├── html
85+
│   ├── index.html
86+
│   ├── my-library-0.1.0.0
87+
│   │   ├── my-library-0.1.0.0-ERSaOroBZhe9awsoBkhmcV
88+
│ │   │ ├── My.Lib.Config.hs.html
89+
│ │   │ ├── My.Lib.Types.hs.html
90+
│ │   │ └── My.Lib.Util.hs.html
91+
│   │   ├── hpc_index_alt.html
92+
│   │   ├── hpc_index_exp.html
93+
│   │   ├── hpc_index_fun.html
94+
│   │   └── hpc_index.html
95+
│ └── other-libray-0.1.0.0
96+
│ ├── other-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
97+
│ │   ├── Other.Lib.A.hs.html
98+
│ │   └── Other.Lib.B.hs.html
99+
│ ├── hpc_index_alt.html
100+
│ ├── hpc_index_exp.html
101+
│ ├── hpc_index_fun.html
102+
│ └── hpc_index.html
84103
├── mix
85104
│   ├── my-library-0.1.0.0
86105
│   │   └── my-library-0.1.0.0-ERSaOroBZhe9awsoBkhmcV
87106
│   │   ├── My.Lib.Config.mix
88107
│   │   ├── My.Lib.Types.mix
89108
│   │   └── My.Lib.Util.mix
90-
│   ├── my-test-1
91-
│   │   ├── Spec.mix
92-
│   │   └── Main.mix
93-
│   ├── other-library-0.1.0.0
94-
│   │   └── other-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
95-
│   │   ├── Other.Lib.A.mix
96-
│   │   └── Other.Lib.B.mix
97-
│   └── other-test-1
98-
│   ├── Spec.mix
99-
│   └── Main.mix
100-
├── tix
101-
│ ├── all
102-
│ │   └── all.tix
103-
│ ├── my-library-0.1.0.0
104-
│ │   └── my-library-0.1.0.0.tix
105-
│ ├── my-test-1
106-
│ │   └── my-test-1.tix
107-
│ ├── other-library-0.1.0.0
108-
│ │   └── other-library-0.1.0.0.tix
109-
│ └── other-test-1
110-
│ └── other-test-1.tix
111-
└── html
109+
│   └── other-library-0.1.0.0
110+
│   └── other-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
111+
│   ├── Other.Lib.A.mix
112+
│   └── Other.Lib.B.mix
113+
└── tix
114+
├── all
115+
│   └── all.tix
112116
├── my-library-0.1.0.0
113-
│   ├── my-library-0.1.0.0-ERSaOroBZhe9awsoBkhmcV
114-
│   │ ├── My.Lib.Config.hs.html
115-
│   │ ├── My.Lib.Types.hs.html
116-
│   │ └── My.Lib.Util.hs.html
117-
│   ├── hpc_index_alt.html
118-
│   ├── hpc_index_exp.html
119-
│   ├── hpc_index_fun.html
120-
│   └── hpc_index.html
121-
└── other-libray-0.1.0.0
122-
├── other-library-0.1.0.0-48EVZBwW9Kj29VTaRMhBDf
123-
│   ├── Other.Lib.A.hs.html
124-
│   └── Other.Lib.B.hs.html
125-
├── hpc_index_alt.html
126-
├── hpc_index_exp.html
127-
├── hpc_index_fun.html
128-
└── hpc_index.html
117+
│   └── my-library-0.1.0.0.tix
118+
├── my-test-1
119+
│   └── my-test-1.tix
120+
├── other-library-0.1.0.0
121+
│   └── other-library-0.1.0.0.tix
122+
└── other-test-1
123+
└── other-test-1.tix
129124
```
130125

131126
All of the coverage information is copied verbatim from the coverage

docs/tutorials/coverage.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,40 @@ project using Cabal's inbuilt hpc support.
66
## Prerequisites
77

88
To get a sensible coverage report, you need to enable coverage on each
9-
of the components of your project. We recommend you use the
10-
`overrideModules` function to do this:
9+
of the components of your project:
1110

1211
```nix
13-
let
14-
inherit (pkgs.haskell-nix) haskellLib;
15-
16-
project = pkgs.haskell-nix.project {
17-
src = pkgs.haskell-nix.haskellLib.cleanGit {
18-
name = "haskell-nix-project";
19-
src = ./.;
20-
};
21-
# For `cabal.project` based projects specify the GHC version to use.
22-
compiler-nix-name = "ghc884"; # Not used for `stack.yaml` based projects.
12+
pkgs.haskell-nix.project {
13+
src = pkgs.haskell-nix.haskellLib.cleanGit {
14+
name = "haskell-nix-project";
15+
src = ./.;
2316
};
17+
compiler-nix-name = "ghc884";
2418
25-
projectWithCoverage = project.overrideModules(oldModules: oldModules ++ [{
19+
modules = [{
2620
packages.$pkg.components.library.doCoverage = true;
2721
packages.$pkg.components.tests.a-test.doCoverage = true;
28-
}]);
29-
30-
in {
31-
inherit project projectWithCoverage;
22+
}];
3223
}
24+
```
25+
26+
If you would like to make coverage optional, add an argument to your nix expression:
27+
28+
```nix
29+
{ withCoverage ? false }:
3330
31+
pkgs.haskell-nix.project {
32+
src = pkgs.haskell-nix.haskellLib.cleanGit {
33+
name = "haskell-nix-project";
34+
src = ./.;
35+
};
36+
compiler-nix-name = "ghc884";
37+
38+
modules = pkgs.lib.optional withCoverage [{
39+
packages.$pkg.components.library.doCoverage = true;
40+
packages.$pkg.components.tests.a-test.doCoverage = true;
41+
}];
42+
}
3443
```
3544

3645
## Per-package
@@ -69,35 +78,38 @@ These are found in the haskell.nix library:
6978
let
7079
inherit (pkgs.haskell-nix) haskellLib;
7180
72-
project = pkgs.haskell-nix.project {
81+
project = haskellLib.project {
7382
src = pkgs.haskell-nix.haskellLib.cleanGit {
7483
name = "haskell-nix-project";
7584
src = ./.;
7685
};
77-
# For `cabal.project` based projects specify the GHC version to use.
78-
compiler-nix-name = "ghc884"; # Not used for `stack.yaml` based projects.
79-
};
86+
compiler-nix-name = "ghc884";
8087
81-
projectWithCoverage = project.overrideModules(oldModules: oldModules ++ [{
82-
packages.$pkg.components.library.doCoverage = true;
83-
packages.$pkg.components.tests.a-test.doCoverage = true;
84-
}]);
88+
modules = [{
89+
packages.$pkgA.components.library.doCoverage = true;
90+
packages.$pkgB.components.library.doCoverage = true;
91+
}];
92+
};
8593
8694
# Choose the library and tests you want included in the coverage
8795
# report for a package.
88-
custom$pkgCoverageReport = haskellLib.coverageReport {
89-
inherit (projectWithCoverage.$pkg.identifier) name version;
90-
inherit (projectWithCoverage.$pkg.components) library tests;
96+
custom$pkgACoverageReport = haskellLib.coverageReport {
97+
name = "$pkgA-unit-tests-only"
98+
inherit (project.$pkgA.components) library;
99+
checks = [project.$pkgA.components.checks.unit-test];
100+
};
101+
102+
custom$pkgBCoverageReport = haskellLib.coverageReport {
103+
name = "$pkgB-unit-tests-only"
104+
inherit (project.$pkgB.components) library;
105+
checks = [project.$pkgB.components.checks.unit-test];
91106
};
92107
93108
# Override the coverage report for a package, and also choose which
94109
# packages you want included in the coverage report.
95-
customProjectCoverageReport = haskellLib.projectCoverageReport {
96-
packages = haskellLib.selectProjectPackages projectWithCoverage;
97-
coverageReportOverrides = { "${projectWithCoverage.$pkg.identifier.name}" = custom$pkgCoverageReport; };
98-
};
110+
allUnitTestsProjectReport = haskellLib.projectCoverageReport [custom$pkgACoverageReport custom$pkgBCoverageReport];
99111
in {
100-
inherit project projectWithCoverage custom$pkgCoverageReport customProjectCoverageReport;
112+
inherit project custom$pkgACoverageReport custom$pkgBCoverageReport allUnitTestsProjectCoverageReport;
101113
}
102114
103115
```

lib/cover.nix

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
}:
1010

1111
let
12-
checks' = with lib; mapAttrs (_: check: assert (isDerivation check); check) checks;
13-
checksList = lib.attrValues checks';
14-
1512
toBashArray = arr: "(" + (lib.concatStringsSep " " arr) + ")";
1613

1714
# Exclude test modules from tix file. Getting coverage information
@@ -25,24 +22,17 @@ let
2522
# matter. So a line in your cabal file such as: "main-is: Spec.hs"
2623
# still generates a "Main.mix" file with the contents: Mix
2724
# "Spec.hs". Hence we can hardcode the name "Main" here.
28-
testModules = lib.foldl' (acc: test: acc ++ test.config.modules) ["Main"] checksList;
25+
testModules = lib.foldl' (acc: test: acc ++ test.config.modules) ["Main"] checks;
2926

3027
# Mix information HPC will need.
31-
# For libraries, where we copy the mix information from differs from
32-
# where HPC should look for the mix files. For tests, they are the
33-
# same location.
34-
mixInfo = [ { rootDir = "${library}/share/hpc/vanilla/mix"; searchSubDir = "${library.identifier.name}-${library.identifier.version}";}];
35-
36-
mixDirs = map (info: info.rootDir) mixInfo;
37-
mixSearchDirs = map (info: if info.searchSubDir == null then info.rootDir else info.rootDir + "/" + info.searchSubDir) mixInfo;
28+
mixDirs = [ "${library}/share/hpc/vanilla/mix/${library.identifier.name}-${library.identifier.version}" ];
3829

3930
ghc = library.project.pkg-set.config.ghc.package;
4031

4132
in pkgs.runCommand (name + "-coverage-report")
4233
{ buildInputs = [ ghc ];
4334
passthru = {
44-
inherit name library;
45-
checks = checks';
35+
inherit name library checks;
4636
};
4737
}
4838
''
@@ -96,7 +86,7 @@ in pkgs.runCommand (name + "-coverage-report")
9686
# Copy over mix files verbatim
9787
for dir in "''${mixDirs[@]}"; do
9888
if [ -d "$dir" ]; then
99-
cp -R "$dir"/* $out/share/hpc/vanilla/mix/
89+
cp -R "$dir"/* $out/share/hpc/vanilla/mix/${name}
10090
fi
10191
done
10292
@@ -116,20 +106,19 @@ in pkgs.runCommand (name + "-coverage-report")
116106
117107
popd
118108
fi
119-
'') checksList)
109+
'') checks)
120110
}
121111
122112
# Sum tix files to create a tix file with all relevant tix
123113
# information and markup a HTML report from this info.
124114
if (( "''${#tixFiles[@]}" > 0 )); then
125115
local src=${library.src.outPath}
126-
local mixSearchDirs=${toBashArray mixSearchDirs}
127116
local testModules=${toBashArray testModules}
128117
local sumTixFile="$out/share/hpc/vanilla/tix/${name}/${name}.tix"
129118
local markupOutDir="$out/share/hpc/vanilla/html/${name}"
130119
131120
sumTix testModules tixFiles "$sumTixFile"
132121
133-
markup "$src" mixSearchDirs testModules "$markupOutDir" "$sumTixFile"
122+
markup "$src" mixDirs testModules "$markupOutDir" "$sumTixFile"
134123
fi
135124
''

overlays/haskell.nix

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ final: prev: {
496496
packageCoverageReports = map (pkg: pkg.coverageReport) (final.lib.attrValues (haskellLib.selectProjectPackages project.hsPkgs));
497497
in project // {
498498
projectCoverageReport = haskellLib.projectCoverageReport packageCoverageReports;
499-
overrideModules = f: cabalProject' (args // { modules = f args.modules; });
500499
};
501500

502501
# Take `hsPkgs` from the `rawProject` and update all the packages and
@@ -522,7 +521,7 @@ final: prev: {
522521
coverageReport = haskellLib.coverageReport {
523522
name = package.identifier.name + "-" + package.identifier.version;
524523
inherit (components) library;
525-
checks = final.lib.filterAttrs (_: final.lib.isDerivation) (package'.checks);
524+
checks = final.lib.filter (final.lib.isDerivation) (final.lib.attrValues package'.checks);
526525
};
527526
}
528527
) rawProject.hsPkgs
@@ -538,7 +537,7 @@ final: prev: {
538537
args = { caller = "hackage-package"; } // args';
539538
p = cabalProject' args;
540539
in p.hsPkgs // {
541-
inherit (p) plan-nix index-state tool tools roots projectCoverageReport overrideModules;
540+
inherit (p) plan-nix index-state tool tools roots projectCoverageReport;
542541
# Provide `nix-shell -A shells.ghc` for users migrating from the reflex-platform.
543542
# But we should encourage use of `nix-shell -A shellFor`
544543
shells.ghc = p.hsPkgs.shellFor {};
@@ -569,12 +568,11 @@ final: prev: {
569568
packageCoverageReports = map (pkg: pkg.coverageReport) (final.lib.attrValues (haskellLib.selectProjectPackages project.hsPkgs));
570569
in project // {
571570
projectCoverageReport = haskellLib.projectCoverageReport packageCoverageReports;
572-
overrideModules = f: stackProject' (args // { modules = f args.modules; });
573571
};
574572

575573
stackProject = args: let p = stackProject' args;
576574
in p.hsPkgs // {
577-
inherit (p) stack-nix tool tools roots projectCoverageReport overrideModules;
575+
inherit (p) stack-nix tool tools roots projectCoverageReport;
578576
# Provide `nix-shell -A shells.ghc` for users migrating from the reflex-platform.
579577
# But we should encourage use of `nix-shell -A shellFor`
580578
shells.ghc = p.hsPkgs.shellFor {};

0 commit comments

Comments
 (0)