libtests: outfactor in preparation of lib/flake.nix

This commit is contained in:
David Arnold 2021-04-17 19:30:55 -05:00
parent 0db2bb041e
commit 863c17621c
13 changed files with 123 additions and 32 deletions

View file

@ -1,4 +1,4 @@
{ pkgs-lib, pkgs, system, inputs, nixos, lib, ... }: { pkgs, system, inputs, nixos, lib, ... }:
let let
mkChecks = { hosts, nodes, homes ? { } }: mkChecks = { hosts, nodes, homes ? { } }:
let let
@ -7,8 +7,7 @@ let
nodes; nodes;
deployChecks = inputs.deploy.lib.${system}.deployChecks { nodes = deployHosts; }; deployChecks = inputs.deploy.lib.${system}.deployChecks { nodes = deployHosts; };
tests = tests =
{ libTests = libTests; } lib.optionalAttrs (deployHosts != { }) {
// lib.optionalAttrs (deployHosts != { }) {
profilesTest = profilesTest (hosts.${(builtins.head (builtins.attrNames deployHosts))}); profilesTest = profilesTest (hosts.${(builtins.head (builtins.attrNames deployHosts))});
} // lib.mapAttrs (n: v: v.activationPackage) homes; } // lib.mapAttrs (n: v: v.activationPackage) homes;
@ -51,33 +50,5 @@ let
machine.systemctl("is-system-running --wait") machine.systemctl("is-system-running --wait")
''; '';
}; };
libTests = pkgs.runCommandNoCC "devos-lib-tests"
{
buildInputs = [
pkgs.nix
(
let tests = pkgs-lib.callLibs ./lib.nix;
in in
if tests == [ ] { inherit mkTest profilesTest mkChecks; }
then null
else throw (builtins.toJSON tests)
)
];
} ''
datadir="${pkgs.nix}/share"
export TEST_ROOT=$(pwd)/test-tmp
export NIX_BUILD_HOOK=
export NIX_CONF_DIR=$TEST_ROOT/etc
export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
export NIX_STATE_DIR=$TEST_ROOT/var/nix
export NIX_STORE_DIR=$TEST_ROOT/store
export PAGER=cat
cacheDir=$TEST_ROOT/binary-cache
nix-store --init
touch $out
'';
in
{ inherit mkTest libTests profilesTest mkChecks; }

27
lib/tests/default.nix Normal file
View file

@ -0,0 +1,27 @@
{ pkgs, lib, dev, ... }:
pkgs.runCommandNoCC "devos-lib-tests"
{
buildInputs = [
pkgs.nix
(
let tests = import ./lib.nix { inherit pkgs lib dev; }; in
if tests == [ ] then null
else throw (builtins.toJSON tests)
)
];
} ''
datadir="${pkgs.nix}/share"
export TEST_ROOT=$(pwd)/test-tmp
export NIX_BUILD_HOOK=
export NIX_CONF_DIR=$TEST_ROOT/etc
export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
export NIX_STATE_DIR=$TEST_ROOT/var/nix
export NIX_STORE_DIR=$TEST_ROOT/store
export PAGER=cat
cacheDir=$TEST_ROOT/binary-cache
nix-store --init
touch $out
''

93
lib/tests/lib.nix Normal file
View file

@ -0,0 +1,93 @@
{ pkgs, lib, dev, ... }:
with dev;
lib.runTests {
testConcatAttrs = {
expr = concatAttrs [{ foo = 1; } { bar = 2; } { baz = 3; }];
expected = { foo = 1; bar = 2; baz = 3; };
};
testGenAttrs' = {
expr = genAttrs'
[ "/foo/bar" "/baz/buzz" ]
(path: {
name = baseNameOf path;
value = "${path}/fizz";
});
expected = { bar = "/foo/bar/fizz"; buzz = "/baz/buzz/fizz"; };
};
testMapFilterAttrs = {
expr = mapFilterAttrs
(n: v: n == "foobar" && v == 1)
(n: v: lib.nameValuePair ("${n}bar") (v + 1))
{ foo = 0; bar = 2; };
expected = { foobar = 1; };
};
testPathsIn = {
expr = pathsIn (toString ./testPathsIn);
expected = map toString [
./testPathsIn/bar
./testPathsIn/baz
./testPathsIn/foo
];
};
testPathsToImportedAttrs = {
expr =
pathsToImportedAttrs [
(toString ./testPathsToImportedAttrs/dir)
./testPathsToImportedAttrs/foo.nix
./testPathsToImportedAttrs/bar.nix
./testPathsToImportedAttrs/t.nix
./testPathsToImportedAttrs/f.nix
];
expected = {
dir = { a = 5; };
foo = { bar = 1; };
bar = { foo = 2; };
t = true;
f = false;
};
};
testRgxToString = lib.testAllTrue [
(rgxToString ".+x" "vxk" == "vx")
(rgxToString "^fo" "foo" == "fo")
(rgxToString "a?" "a" == "a")
(rgxToString "hat" "foohatbar" == "hat")
];
testSafeReadDir = {
expr = safeReadDir ./profiles // safeReadDir ./nonexistentdir;
expected = {
foo = "directory";
t = "directory";
};
};
testSuites =
let
profiles = os.mkProfileAttrs (toString ./profiles);
users = "";
userProfiles = "";
suites = { profiles, ... }: {
system.bar = [ profiles.foo ];
};
in
{
expr = os.mkSuites { inherit profiles users userProfiles suites; };
expected = {
system = {
bar = [ profiles.foo.default ];
allProfiles = [ profiles.foo.default profiles.t.default ];
allUsers = [ ];
};
};
};
}