Skip to content

Commit

Permalink
Add nix-shell tests and unit tests
Browse files Browse the repository at this point in the history
- The unit tests are for haskellLib functions

- The nix-shell tests are for checking that development environments work.
  • Loading branch information
rvl committed Jan 22, 2019
1 parent 4fae5af commit d23b080
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
3 changes: 3 additions & 0 deletions modules/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ in {
type = attrsOf (componentType false);
default = {};
};
# all = mkOption {
# type = componentType false;
# };
};

name = mkOption {
Expand Down
6 changes: 6 additions & 0 deletions test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ let
# The new Haskell infra applied to nix representation of Hackage
haskell = import ../. hackage;

haskellLib = let hl = import ../lib { inherit lib; haskellLib = hl; }; in hl;

in {
cabal-simple = callPackage ./cabal-simple { inherit haskell; };
cabal-22 = callPackage ./cabal-22 { inherit haskell; };
with-packages = callPackage ./with-packages { inherit haskell; };

# Run unit tests with: nix-instantiate --eval --strict -A unit
# An empty list means success.
unit = callPackage ./unit.nix { inherit haskellLib; };
}

## possible test cases
Expand Down
28 changes: 28 additions & 0 deletions test/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash jq nix

set -euo pipefail

cd $(dirname $0)

printf "*** Running the nix-build tests...\n" >& 2
nix-build --no-out-link --keep-going ./default.nix
echo >& 2

printf "*** Running the unit tests... " >& 2
res=$(nix-instantiate --eval --json --strict ./default.nix -A unit)
num_failed=$(jq length <<< "$res")
if [ $num_failed -eq 0 ]; then
printf "PASSED\n" >& 2
else
printf "$num_failed FAILED\n" >& 2
jq . <<< "$res"
exit 1
fi

printf "*** Checking that a nix-shell works for cabal...\n" >& 2
nix-shell --pure ./default.nix \
-A with-packages.test-shell \
--run "echo CABAL_CONFIG=$CABAL_CONFIG && cd with-packages && cabal new-build"

printf "\n*** Finished\n" >& 2
50 changes: 50 additions & 0 deletions test/unit.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ lib, haskellLib }:

let
emptyConfig = {
components = {
benchmarks = { };
exes = { };
foreignlibs = { };
library = "library";
sublibs = { };
tests = { };
};
package.identifier.name = "empty";
};

componentsConfig = {
components = {
benchmarks = { bbb = "bbb"; };
exes = { eee = "eee"; };
foreignlibs = { fff = "fff"; };
library = "library";
sublibs = { };
tests = { ttt = "ttt"; };
};
package.identifier.name = "nnn";
};

in
lib.runTests {
test-applyComponents-id = {
expr = haskellLib.applyComponents (componentId: component: component) emptyConfig;
expected = emptyConfig.components;
};

test-applyComponents-library = {
expr = haskellLib.applyComponents (componentId: component: componentId.cname) emptyConfig;
expected = emptyConfig.components // { library = "empty"; };
};

test-applyComponents-components = {
expr = haskellLib.applyComponents (componentId: component: component) componentsConfig;
expected = componentsConfig.components;
};

# testing that the tests work
testId = {
expr = lib.id 1;
expected = 1;
};
}
23 changes: 21 additions & 2 deletions test/with-packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,36 @@ in

buildCommand = let
inherit (packages.test-with-packages.components) library;
# inherit (packages.test-with-packages.components) all; # TODO
inherit (packages.test-with-packages) devEnv;
in ''
########################################################################
# test with-packages
printf "checking that package env has the dependencies... " >& 2
printf "checking that the package env has the dependencies... " >& 2
${devEnv}/bin/runghc ${./Point.hs}
echo
# fixme: probably don't want separate derivation for this -- just use all
printf "checking that components.library.shell has the dependencies... " >& 2
''${library.shell}/bin/runghc ${./Point.hs}
echo
# printf "checking that components.all.shell has the dependencies... " >& 2
# ''${all.shell}/bin/runghc ${./Point.hs}
# echo
touch $out
'';

meta.platforms = platforms.all;
} // { inherit packages pkgSet; }
} // {
# Used for debugging with nix repl
inherit packages pkgSet;

# Used for testing externally with nix-shell (../tests.sh).
# This just adds cabal-install to the existing shell.
test-shell = pkgSet.config.hsPkgs.test-with-packages.components.library.overrideAttrs (oldAttrs: {
buildInputs = (oldAttrs.buildInputs or []) ++ [ pkgs.cabal-install ];
});
}

0 comments on commit d23b080

Please sign in to comment.