Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sevanspowell committed Aug 3, 2020
1 parent 0b4bf2f commit f5f564b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 138 deletions.
6 changes: 2 additions & 4 deletions builder/hspkg-builder.nix
Expand Up @@ -103,11 +103,9 @@ in rec {
(lib.filterAttrs (_: d: d.config.doCheck) components.tests));

coverageReport = haskellLib.coverageReport {
inherit ghc src;
inherit (package.identifier) name;
testDerivations = lib.attrValues (lib.filterAttrs (_: d: d.config.doCheck) components.tests);
toCoverDerivations = [ components.library ];
inherit pkgs;
inherit (components) library;
tests = lib.attrValues (lib.filterAttrs (_: d: d.config.doCheck) components.tests);
};

inherit (package) identifier detailLevel isLocal;
Expand Down
71 changes: 7 additions & 64 deletions lib/cover-files-only.nix
@@ -1,68 +1,9 @@
{ pkgs, stdenv, lib, haskellLib }:

projects:
project:

let
buildWithCoverage = builtins.map (d: d.covered);
runCheck = builtins.map (d: haskellLib.check d);

# drvs' = buildWithCoverage drvs;
# drvSources = builtins.map (d: d.src.outPath) drvs;
# testsWithCoverage = buildWithCoverage tests;
# checks' = runCheck testsWithCoverage;

projects' = builtins.map (p:
rec {
name = p.name;
drv = p.drv.covered;
testsWithCoverage = buildWithCoverage p.tests;
checks = runCheck testsWithCoverage;
}
) projects;

doThis = p: ''
mkdir -p $out/share/hpc/mix/${p.name}
mkdir -p $out/share/hpc/tix/${p.name}
for drv in ${lib.concatStringsSep " " ([ p.drv ] ++ p.testsWithCoverage)}; do
# Copy over mix files
local mixDir=$(findMixDir $drv)
cp -R $mixDir/* $out/share/hpc/mix/${p.name}
done
# Exclude test modules from tix file
excludedModules=('"Main"')
local drv=${p.drv.src.outPath}
# Exclude test modules
local cabalFile=$(findCabalFile $drv)
for module in $(${pkgs.cq}/bin/cq $cabalFile testModules | ${pkgs.jq}/bin/jq ".[]"); do
excludedModules+=("$module")
done
echo "''${excludedModules[@]}"
hpcSumCmdBase=("hpc" "sum" "--union")
for exclude in ''${excludedModules[@]}; do
hpcSumCmdBase+=("--exclude=$exclude")
done
for check in ${lib.concatStringsSep " " p.checks}; do
pushd $check/share/hpc/tix
for tixFileRel in $(find . -iwholename "*.tix" -type f); do
set -x
mkdir -p $out/share/hpc/tix/${p.name}/$(dirname $tixFileRel)
cp $tixFileRel $out/share/hpc/tix/${p.name}/$tixFileRel.pre-exclude
local hpcSumCmd=("''${hpcSumCmdBase[@]}")
hpcSumCmd+=("--output=$out/share/hpc/tix/${p.name}/$tixFileRel" "$tixFileRel")
echo "''${hpcSumCmd[@]}"
eval "''${hpcSumCmd[@]}"
done
popd
done
'';

coverageReports = lib.mapAttrsToList (n: package: package.coverageReport) project;
in
stdenv.mkDerivation {
name = "coverage-report";
Expand All @@ -84,9 +25,11 @@ stdenv.mkDerivation {
find $1 -iname "*.cabal" -print -quit
}
# Copy over the mix and tix files for each package
${lib.concatStringsSep "\n" (builtins.map doThis projects')}
echo "Project coverage reports are: "
for report in ${lib.concatStringsSep " " coverageReports}; do
echo $report
done
'';

# # Generate combined tix file for all packages
Expand Down
114 changes: 61 additions & 53 deletions lib/cover.nix
@@ -1,83 +1,91 @@
{ stdenv, lib, haskellLib }:
{ ghc, pkgs, name, src, testDerivations, toCoverDerivations }:
{ stdenv, lib, haskellLib, pkgs }:

{ name, library, tests }:

let
buildWithCoverage = builtins.map (d: d.covered);
runCheck = builtins.map (d: haskellLib.check d);

tests = buildWithCoverage testDerivations;
covering = buildWithCoverage toCoverDerivations;

coverageChecks = builtins.map (d: haskellLib.check d) (builtins.filter (d: d.config.doCheck) tests);
libraryCovered = library.covered;
testsWithCoverage = buildWithCoverage tests;
checks = runCheck testsWithCoverage;

in stdenv.mkDerivation {
name = (name + "-coverage");

inherit src;
name = (name + "-coverage-report");

phases = ["buildPhase"];

buildInputs = (with pkgs; [
git
]) ++ (with pkgs.haskellPackages; [
hpc-coveralls
]);
buildInputs = (with pkgs; [ ghc cq jq ]);

# If doCheck or doCrossCheck are false we may still build this
# component and we want it to quietly succeed.
buildPhase = ''
runHook preCheck
mkdir $out
mkdir -p $out/share/hpc
mkdir -p $out/share/hpc/mix
mkdir -p $out/share/hpc/tix
findMixDir() {
find $1 -iwholename "*/hpc/vanilla/mix" -exec find {} -maxdepth 1 -type d -iwholename "*/mix/*" \; -quit
}
tixFile=all.tix
findCabalFile() {
find $1 -iname "*.cabal" -print -quit
}
mkdir -p $out/share/hpc/mix/${name}
mkdir -p $out/share/hpc/tix/${name}
cabalFile=$(find $src -iname "*.cabal" -print -quit)
excludedModules=("Main")
for module in $(${pkgs.cq}/bin/cq $cabalFile testModules | ${pkgs.jq}/bin/jq ".[]"); do
for drv in ${lib.concatStringsSep " " ([ libraryCovered ] ++ testsWithCoverage)}; do
# Copy over mix files
local mixDir=$(findMixDir $drv)
cp -R $mixDir/* $out/share/hpc/mix/${name}
done
# Exclude test modules from tix file
excludedModules=('"Main"')
local drv=${libraryCovered.src.outPath}
# Exclude test modules
local cabalFile=$(findCabalFile $drv)
for module in $(cq $cabalFile testModules | jq ".[]"); do
excludedModules+=("$module")
done
echo "done"
echo "''${excludedModules[@]}"
hpcSumCmd=("${ghc}/bin/hpc" "sum" "--union" "--output=$tixFile")
for check in ${lib.concatStringsSep " " coverageChecks}; do
for tix in $(find $check -name '*.tix' -print); do
hpcSumCmd+=("$tix")
done
cp -R $check/share/hpc/tix/* $out/share/hpc/tix/
done
hpcSumCmdBase=("hpc" "sum" "--union")
for exclude in ''${excludedModules[@]}; do
hpcSumCmd+=("--exclude=$exclude")
hpcSumCmdBase+=("--exclude=$exclude")
done
echo "''${hpcSumCmd[@]}"
eval "''${hpcSumCmd[@]}"
hpcMarkupCmd=("${ghc}/bin/hpc" "markup" "$tixFile" "--destdir=$out" "--srcdir=$src")
for component in ${lib.concatStringsSep " " (tests ++ covering)}; do
echo "COMPONENT IS $component"
local mixDir=$(findMixDir $component)
for check in ${lib.concatStringsSep " " checks}; do
pushd $check/share/hpc/tix
# Find each tix file (relative to check directory above)
for tixFileRel in $(find . -iwholename "*.tix" -type f); do
# Output tix file as-is with suffix
mkdir -p $out/share/hpc/tix/${name}/$(dirname $tixFileRel)
cp $tixFileRel $out/share/hpc/tix/${name}/$tixFileRel.pre-exclude
# Output tix file with test modules excluded
local hpcSumCmd=("''${hpcSumCmdBase[@]}")
hpcSumCmd+=("--output=$out/share/hpc/tix/${name}/$tixFileRel" "$tixFileRel")
echo "''${hpcSumCmd[@]}"
eval "''${hpcSumCmd[@]}"
done
hpcMarkupCmd+=("--hpcdir=$mixDir")
cp -R $mixDir $out/share/hpc/mix
popd
done
for exclude in ''${excludedModules[@]}; do
hpcMarkupCmd+=("--exclude=$exclude")
done
echo "''${hpcMarkupCmd[@]}"
eval "''${hpcMarkupCmd[@]}"
mkdir -p $out/share/hpc/tix/all
cp $tixFile $out/share/hpc/tix/all/all.tix
# hpcMarkupCmd=("hpc" "markup" "$tixFile" "--destdir=$out" "--srcdir=$src")
# for component in ; do
# echo "COMPONENT IS $component"
# local mixDir=$(findMixDir $component)
# hpcMarkupCmd+=("--hpcdir=$mixDir")
# cp -R $mixDir $out/share/hpc/mix
# done
# for exclude in ''${excludedModules[@]}; do
# hpcMarkupCmd+=("--exclude=$exclude")
# done
# echo "''${hpcMarkupCmd[@]}"
# eval "''${hpcMarkupCmd[@]}"
cp -r $src/* $out
# mkdir -p $out/share/hpc/tix/all
# cp $tixFile $out/share/hpc/tix/all/all.tix
runHook postCheck
# cp -r $src/* $out
'';
}
4 changes: 2 additions & 2 deletions lib/default.nix
Expand Up @@ -241,10 +241,10 @@ in {

# Do coverage of a project
coverageReport = import ./cover.nix {
inherit stdenv lib haskellLib;
inherit stdenv lib haskellLib pkgs;
};

coverageReport' = import ./cover-files-only.nix {
projectCoverageReport = import ./cover-files-only.nix {
inherit stdenv lib haskellLib pkgs;
};

Expand Down
17 changes: 2 additions & 15 deletions overlays/haskell.nix
Expand Up @@ -479,7 +479,7 @@ final: prev: {
plan-nix = callProjectResults.projectNix;
inherit (callProjectResults) index-state;

coverageReport = projectCoverageReport (haskellLib.selectProjectPackages hsPkgs);
projectCoverageReport = haskellLib.projectCoverageReport (haskellLib.selectProjectPackages hsPkgs);
};

# Take `hsPkgs` from the `rawProject` and update all the packages and
Expand Down Expand Up @@ -511,7 +511,7 @@ final: prev: {

cabalProject = args: let p = cabalProject' args;
in p.hsPkgs // {
inherit (p) plan-nix coverageReport;
inherit (p) plan-nix projectCoverageReport;
# Provide `nix-shell -A shells.ghc` for users migrating from the reflex-platform.
# But we should encourage use of `nix-shell -A shellFor`
shells.ghc = p.hsPkgs.shellFor {};
Expand Down Expand Up @@ -584,19 +584,6 @@ final: prev: {
} // final.lib.optionalAttrs (p ? stack-nix) { inherit (p) stack-nix; }
// final.lib.optionalAttrs (p ? plan-nix ) { inherit (p) plan-nix; };

projectCoverageReport = packages:
let
# TODO what if package doesn't have library and/or tests?
projectInfo = with final.lib;
mapAttrsToList (n: p: {
name = n;
drv = p.components.library;
tests = filter isDerivation (attrValues p.components.tests);
}
) packages;
in
haskellLib.coverageReport' projectInfo;

# Like `cabalProject'`, but for building the GHCJS compiler.
# This is exposed to allow GHCJS developers to work on the GHCJS
# code in a nix-shell with `shellFor`.
Expand Down

0 comments on commit f5f564b

Please sign in to comment.