From 863c17621c33f8d86aaf949c608155e7fc00e0f6 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 17 Apr 2021 19:30:55 -0500 Subject: [PATCH] libtests: outfactor in preparation of lib/flake.nix --- lib/pkgs-lib/tests/default.nix | 35 +------ lib/tests/default.nix | 27 ++++++ lib/tests/lib.nix | 93 +++++++++++++++++++ .../tests/profiles/foo/default.nix | 0 .../tests/profiles/t/default.nix | 0 lib/{pkgs-lib => }/tests/testPathsIn/bar | 0 lib/{pkgs-lib => }/tests/testPathsIn/baz | 0 lib/{pkgs-lib => }/tests/testPathsIn/foo | 0 .../tests/testPathsToImportedAttrs/bar.nix | 0 .../testPathsToImportedAttrs/dir/default.nix | 0 .../tests/testPathsToImportedAttrs/f.nix | 0 .../tests/testPathsToImportedAttrs/foo.nix | 0 .../tests/testPathsToImportedAttrs/t.nix | 0 13 files changed, 123 insertions(+), 32 deletions(-) create mode 100644 lib/tests/default.nix create mode 100644 lib/tests/lib.nix rename lib/{pkgs-lib => }/tests/profiles/foo/default.nix (100%) rename lib/{pkgs-lib => }/tests/profiles/t/default.nix (100%) rename lib/{pkgs-lib => }/tests/testPathsIn/bar (100%) rename lib/{pkgs-lib => }/tests/testPathsIn/baz (100%) rename lib/{pkgs-lib => }/tests/testPathsIn/foo (100%) rename lib/{pkgs-lib => }/tests/testPathsToImportedAttrs/bar.nix (100%) rename lib/{pkgs-lib => }/tests/testPathsToImportedAttrs/dir/default.nix (100%) rename lib/{pkgs-lib => }/tests/testPathsToImportedAttrs/f.nix (100%) rename lib/{pkgs-lib => }/tests/testPathsToImportedAttrs/foo.nix (100%) rename lib/{pkgs-lib => }/tests/testPathsToImportedAttrs/t.nix (100%) diff --git a/lib/pkgs-lib/tests/default.nix b/lib/pkgs-lib/tests/default.nix index 3c284683..891baee9 100644 --- a/lib/pkgs-lib/tests/default.nix +++ b/lib/pkgs-lib/tests/default.nix @@ -1,4 +1,4 @@ -{ pkgs-lib, pkgs, system, inputs, nixos, lib, ... }: +{ pkgs, system, inputs, nixos, lib, ... }: let mkChecks = { hosts, nodes, homes ? { } }: let @@ -7,8 +7,7 @@ let nodes; deployChecks = inputs.deploy.lib.${system}.deployChecks { nodes = deployHosts; }; tests = - { libTests = libTests; } - // lib.optionalAttrs (deployHosts != { }) { + lib.optionalAttrs (deployHosts != { }) { profilesTest = profilesTest (hosts.${(builtins.head (builtins.attrNames deployHosts))}); } // lib.mapAttrs (n: v: v.activationPackage) homes; @@ -51,33 +50,5 @@ let machine.systemctl("is-system-running --wait") ''; }; - - libTests = pkgs.runCommandNoCC "devos-lib-tests" - { - buildInputs = [ - pkgs.nix - ( - let tests = pkgs-lib.callLibs ./lib.nix; - 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 - ''; in -{ inherit mkTest libTests profilesTest mkChecks; } +{ inherit mkTest profilesTest mkChecks; } diff --git a/lib/tests/default.nix b/lib/tests/default.nix new file mode 100644 index 00000000..c13dd8a1 --- /dev/null +++ b/lib/tests/default.nix @@ -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 + '' diff --git a/lib/tests/lib.nix b/lib/tests/lib.nix new file mode 100644 index 00000000..68baa9f5 --- /dev/null +++ b/lib/tests/lib.nix @@ -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 = [ ]; + }; + }; + }; +} diff --git a/lib/pkgs-lib/tests/profiles/foo/default.nix b/lib/tests/profiles/foo/default.nix similarity index 100% rename from lib/pkgs-lib/tests/profiles/foo/default.nix rename to lib/tests/profiles/foo/default.nix diff --git a/lib/pkgs-lib/tests/profiles/t/default.nix b/lib/tests/profiles/t/default.nix similarity index 100% rename from lib/pkgs-lib/tests/profiles/t/default.nix rename to lib/tests/profiles/t/default.nix diff --git a/lib/pkgs-lib/tests/testPathsIn/bar b/lib/tests/testPathsIn/bar similarity index 100% rename from lib/pkgs-lib/tests/testPathsIn/bar rename to lib/tests/testPathsIn/bar diff --git a/lib/pkgs-lib/tests/testPathsIn/baz b/lib/tests/testPathsIn/baz similarity index 100% rename from lib/pkgs-lib/tests/testPathsIn/baz rename to lib/tests/testPathsIn/baz diff --git a/lib/pkgs-lib/tests/testPathsIn/foo b/lib/tests/testPathsIn/foo similarity index 100% rename from lib/pkgs-lib/tests/testPathsIn/foo rename to lib/tests/testPathsIn/foo diff --git a/lib/pkgs-lib/tests/testPathsToImportedAttrs/bar.nix b/lib/tests/testPathsToImportedAttrs/bar.nix similarity index 100% rename from lib/pkgs-lib/tests/testPathsToImportedAttrs/bar.nix rename to lib/tests/testPathsToImportedAttrs/bar.nix diff --git a/lib/pkgs-lib/tests/testPathsToImportedAttrs/dir/default.nix b/lib/tests/testPathsToImportedAttrs/dir/default.nix similarity index 100% rename from lib/pkgs-lib/tests/testPathsToImportedAttrs/dir/default.nix rename to lib/tests/testPathsToImportedAttrs/dir/default.nix diff --git a/lib/pkgs-lib/tests/testPathsToImportedAttrs/f.nix b/lib/tests/testPathsToImportedAttrs/f.nix similarity index 100% rename from lib/pkgs-lib/tests/testPathsToImportedAttrs/f.nix rename to lib/tests/testPathsToImportedAttrs/f.nix diff --git a/lib/pkgs-lib/tests/testPathsToImportedAttrs/foo.nix b/lib/tests/testPathsToImportedAttrs/foo.nix similarity index 100% rename from lib/pkgs-lib/tests/testPathsToImportedAttrs/foo.nix rename to lib/tests/testPathsToImportedAttrs/foo.nix diff --git a/lib/pkgs-lib/tests/testPathsToImportedAttrs/t.nix b/lib/tests/testPathsToImportedAttrs/t.nix similarity index 100% rename from lib/pkgs-lib/tests/testPathsToImportedAttrs/t.nix rename to lib/tests/testPathsToImportedAttrs/t.nix