Skip to content

Commit 38e1ea1

Browse files
committed
Simplify coverage Nix logic
1 parent 47a769b commit 38e1ea1

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

lib/cover.nix

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,34 @@
77
}:
88

99
let
10+
identifier = name + "-" + version;
11+
toBashArray = arr: "(" + (lib.concatStringsSep " " arr) + ")";
12+
1013
testsAsList = lib.attrValues tests;
1114
checks = builtins.map (d: haskellLib.check d) testsAsList;
1215

13-
identifier = name + "-" + version;
14-
15-
toBashArray = arr: "(" + (lib.concatStringsSep " " arr) + ")";
16+
# Exclude test modules from tix file. The Main module is
17+
# hard-coded here because the Main module is not listed in
18+
# "$test.config.modules" (the plan.nix) but must be excluded.
19+
# Note that the name of the Main module file does not matter. So
20+
# a line in your cabal file such as:
21+
# main-is: Spec.hs
22+
# still generates a "Main.mix" file with the contents:
23+
# Mix "Spec.hs" ...
24+
# Hence we can hardcode the name "Main" here.
25+
testModules = lib.foldl' (acc: test: acc ++ test.config.modules) ["Main"] testsAsList;
26+
27+
# Mix information HPC will need.
28+
# For libraries, where we copy the mix information from differs from
29+
# where HPC should look for the mix files. For tests, they are the
30+
# same location.
31+
mixInfo = [ { rootDir = "${library}/share/hpc/vanilla/mix";
32+
searchSubDir = "${identifier}";
33+
}
34+
] ++ map (drv: { rootDir = "${drv}/share/hpc/vanilla/mix"; searchSubDir = null; }) testsAsList;
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;
1638

1739
in pkgs.runCommand (identifier + "-coverage-report")
1840
{ buildInputs = (with pkgs; [ ghc ]); }
@@ -44,8 +66,6 @@ in pkgs.runCommand (identifier + "-coverage-report")
4466
local -n tixFs=$2
4567
local outFile="$3"
4668
47-
echo "EXCLUDED ''${excludedModules[@]}"
48-
echo "EXCLUDED ''${tixFs[@]}"
4969
local hpcSumCmd=("hpc" "sum" "--union" "--output=$outFile")
5070
5171
for module in "''${excludedModules[@]}"; do
@@ -60,43 +80,31 @@ in pkgs.runCommand (identifier + "-coverage-report")
6080
eval "''${hpcSumCmd[@]}"
6181
}
6282
83+
local mixDirs=${toBashArray mixDirs}
84+
6385
mkdir -p $out/share/hpc/vanilla/mix/${identifier}
6486
mkdir -p $out/share/hpc/vanilla/tix/${identifier}
6587
mkdir -p $out/share/hpc/vanilla/html/${identifier}
6688
67-
local src=${library.src.outPath}
68-
local mixDirs=${toBashArray (map (drv: "${drv}/share/hpc/vanilla/mix") ([library] ++ testsAsList))}
69-
# Exclude test modules from tix file. The Main module is
70-
# hard-coded here because the Main module is not listed in
71-
# "$test.config.modules" (the plan.nix) but must be excluded.
72-
# Note that the name of the Main module file does not matter. So
73-
# a line in your cabal file such as:
74-
# main-is: Spec.hs
75-
# still generates a "Main.mix" file with the contents:
76-
# Mix "Spec.hs" ...
77-
# Hence we can hardcode the name "Main" here.
78-
local testModules=${toBashArray (["Main"] ++ (lib.foldl' (acc: test: acc ++ test.config.modules) [] testsAsList))}
79-
local totalTixFile="$out/share/hpc/vanilla/tix/${identifier}/${identifier}.tix"
80-
local markupOutDir="$out/share/hpc/vanilla/html/${identifier}"
81-
82-
echo "''${testModules[@]}"
83-
8489
# Copy over mix files verbatim
85-
for dir in $mixDirs; do
86-
if [ ! -z "$mixDir" ]; then
87-
cp -R "$mixDir" $out/share/hpc/vanilla/mix/
90+
for dir in "''${mixDirs[@]}"; do
91+
if [ -d "$dir" ]; then
92+
cp -R "$dir"/* $out/share/hpc/vanilla/mix/
8893
fi
8994
done
9095
96+
# Copy over tix files verbatim
9197
${lib.optionalString ((builtins.length testsAsList) > 0) ''
9298
local tixFiles=()
9399
${lib.concatStringsSep "\n" (builtins.map (check: ''
94100
if [ -d "${check}/share/hpc/vanilla/tix" ]; then
95101
pushd ${check}/share/hpc/vanilla/tix
96102
97103
tixFile="$(find . -iwholename "*.tix" -type f -print -quit)"
104+
local newTixFile=$out/share/hpc/vanilla/tix/"$tixFile"
98105
99-
cp "$tixFile" $out/share/hpc/vanilla/tix/
106+
mkdir -p "$(dirname $newTixFile)"
107+
cp "$tixFile" "$newTixFile"
100108
101109
tixFiles+=("${check}/share/hpc/vanilla/tix/$tixFile")
102110
@@ -105,10 +113,18 @@ in pkgs.runCommand (identifier + "-coverage-report")
105113
'') checks)
106114
}
107115
116+
# Sum tix files to create a tix file with all relevant tix
117+
# information and markup a HTML report from this info.
108118
if (( "''${#tixFiles[@]}" > 0 )); then
109-
sumTix testModules tixFiles "$totalTixFile"
110-
111-
markup "$src" mixDirs testModules "$markupOutDir" "$totalTixFile"
119+
local src=${library.src.outPath}
120+
local mixSearchDirs=${toBashArray mixSearchDirs}
121+
local testModules=${toBashArray testModules}
122+
local sumTixFile="$out/share/hpc/vanilla/tix/${identifier}/${identifier}.tix"
123+
local markupOutDir="$out/share/hpc/vanilla/html/${identifier}"
124+
125+
sumTix testModules tixFiles "$sumTixFile"
126+
127+
markup "$src" mixSearchDirs testModules "$markupOutDir" "$sumTixFile"
112128
fi
113129
''}
114130
''

0 commit comments

Comments
 (0)