Skip to content

Commit b589689

Browse files
committed
Add nix-shell tests and unit tests
- The unit tests are for haskellLib functions - The nix-shell tests are for checking that development environments work.
1 parent 4fae5af commit b589689

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

modules/package.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ in {
173173
type = attrsOf (componentType false);
174174
default = {};
175175
};
176+
# all = mkOption {
177+
# type = componentType false;
178+
# };
176179
};
177180

178181
name = mkOption {

test/default.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ let
1313
# The new Haskell infra applied to nix representation of Hackage
1414
haskell = import ../. hackage;
1515

16+
haskellLib = let hl = import ../lib { inherit lib; haskellLib = hl; }; in hl;
17+
1618
in {
1719
cabal-simple = callPackage ./cabal-simple { inherit haskell; };
1820
cabal-22 = callPackage ./cabal-22 { inherit haskell; };
1921
with-packages = callPackage ./with-packages { inherit haskell; };
22+
23+
# Run unit tests with: nix-instantiate --eval --strict -A unit
24+
# An empty list means success.
25+
unit = callPackage ./unit.nix { inherit haskellLib; };
2026
}
2127

2228
## possible test cases

test/tests.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#! /usr/bin/env nix-shell
2+
#! nix-shell -i bash -p bash jq nix
3+
4+
set -euo pipefail
5+
6+
cd $(dirname $0)
7+
8+
printf "*** Running the nix-build tests...\n" >& 2
9+
nix-build --no-out-link --keep-going ./default.nix
10+
echo >& 2
11+
12+
printf "*** Running the unit tests... " >& 2
13+
res=$(nix-instantiate --eval --json --strict ./default.nix -A unit)
14+
num_failed=$(jq length <<< "$res")
15+
if [ $num_failed -eq 0 ]; then
16+
printf "PASSED\n" >& 2
17+
else
18+
printf "$num_failed FAILED\n" >& 2
19+
jq . <<< "$res"
20+
exit 1
21+
fi
22+
23+
printf "*** Checking that a nix-shell works for cabal...\n" >& 2
24+
nix-shell --pure ./default.nix \
25+
-A with-packages.test-shell \
26+
--run 'echo CABAL_CONFIG=$CABAL_CONFIG && cd with-packages && cabal new-build'
27+
28+
printf "\n*** Finished successfully\n" >& 2

test/unit.nix

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{ lib, haskellLib }:
2+
3+
let
4+
emptyConfig = {
5+
components = {
6+
benchmarks = { };
7+
exes = { };
8+
foreignlibs = { };
9+
library = "library";
10+
sublibs = { };
11+
tests = { };
12+
};
13+
package.identifier.name = "empty";
14+
};
15+
16+
componentsConfig = {
17+
components = {
18+
benchmarks = { bbb = "bbb"; };
19+
exes = { eee = "eee"; };
20+
foreignlibs = { fff = "fff"; };
21+
library = "library";
22+
sublibs = { };
23+
tests = { ttt = "ttt"; };
24+
};
25+
package.identifier.name = "nnn";
26+
};
27+
28+
in
29+
lib.runTests {
30+
test-applyComponents-id = {
31+
expr = haskellLib.applyComponents (componentId: component: component) emptyConfig;
32+
expected = emptyConfig.components;
33+
};
34+
35+
test-applyComponents-library = {
36+
expr = haskellLib.applyComponents (componentId: component: componentId.cname) emptyConfig;
37+
expected = emptyConfig.components // { library = "empty"; };
38+
};
39+
40+
test-applyComponents-components = {
41+
expr = haskellLib.applyComponents (componentId: component: component) componentsConfig;
42+
expected = componentsConfig.components;
43+
};
44+
45+
# testing that the tests work
46+
testId = {
47+
expr = lib.id 1;
48+
expected = 1;
49+
};
50+
}

test/with-packages/default.nix

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,36 @@ in
3232

3333
buildCommand = let
3434
inherit (packages.test-with-packages.components) library;
35+
# inherit (packages.test-with-packages.components) all; # TODO
3536
inherit (packages.test-with-packages) devEnv;
3637
in ''
3738
########################################################################
3839
# test with-packages
3940
40-
printf "checking that package env has the dependencies... " >& 2
41+
printf "checking that the package env has the dependencies... " >& 2
4142
${devEnv}/bin/runghc ${./Point.hs}
4243
echo
4344
45+
# # fixme: probably don't want separate derivation for this -- just use all
46+
# printf "checking that components.library.shell has the dependencies... " >& 2
47+
# ''${library.shell}/bin/runghc ${./Point.hs}
48+
# echo
49+
50+
# printf "checking that components.all.shell has the dependencies... " >& 2
51+
# ''${all.shell}/bin/runghc ${./Point.hs}
52+
# echo
53+
4454
touch $out
4555
'';
4656

4757
meta.platforms = platforms.all;
48-
} // { inherit packages pkgSet; }
58+
} // {
59+
# Used for debugging with nix repl
60+
inherit packages pkgSet;
61+
62+
# Used for testing externally with nix-shell (../tests.sh).
63+
# This just adds cabal-install to the existing shell.
64+
test-shell = pkgSet.config.hsPkgs.test-with-packages.components.library.overrideAttrs (oldAttrs: {
65+
buildInputs = (oldAttrs.buildInputs or []) ++ [ pkgs.cabal-install ];
66+
});
67+
}

0 commit comments

Comments
 (0)