From a891633e2ae27caad675c2bf06d716203123e3ac Mon Sep 17 00:00:00 2001 From: Matt Schreiber Date: Sat, 14 Oct 2023 11:12:32 -0400 Subject: [PATCH] feat: add module options tests --- tests/flake-module.nix | 4 ++ tests/module-options-arion-test/default.nix | 76 +++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/module-options-arion-test/default.nix diff --git a/tests/flake-module.nix b/tests/flake-module.nix index 09f1c01..66949f1 100644 --- a/tests/flake-module.nix +++ b/tests/flake-module.nix @@ -31,6 +31,10 @@ modules = [ ../examples/minimal/arion-compose.nix ]; }; + testModuleOptions = import ./module-options-arion-test { + inherit lib; + pkgs = final; + }; }; }; } diff --git a/tests/module-options-arion-test/default.nix b/tests/module-options-arion-test/default.nix new file mode 100644 index 0000000..cb99e07 --- /dev/null +++ b/tests/module-options-arion-test/default.nix @@ -0,0 +1,76 @@ +{ pkgs, lib ? pkgs.lib, ... }: +let + inherit (lib) any escapeShellArg replaceStrings runTests toList; + + evalComposition = modules: import ../../src/nix/eval-composition.nix { + inherit pkgs; + modules = toList modules; + }; + + testComposition = { expected, fn, config }: { + inherit expected; + expr = fn (evalComposition config); + }; + + search = pattern: str: (builtins.match ".*${pattern}.*" str) != null; + + assertionsMatch = patterns: assertions: + let + failed = builtins.filter (assertion: !assertion.assertion) assertions; + matchAnyPattern = assertion: any (pattern: search pattern assertion.message) (toList patterns); + in + any matchAnyPattern failed; + + checkAssertions = expected: patterns: config: testComposition { + inherit expected config; + fn = composition: assertionsMatch patterns composition.config.assertions; + }; + + checkAssertionsMatch = checkAssertions true; + checkAssertionsDoNotMatch = checkAssertions false; + + mkRepoTagAssertionPattern = component: attrName: name: replaceStrings [ "\n" ] [ " " ] '' + Unable to infer the ${component} of the image associated with + config\.services\.${name}\. Please set + config\.services\.${name}\.image\.${attrName} to a non-empty string\. + ''; + + imageNameAssertionPattern = mkRepoTagAssertionPattern "name" "imageName"; + imageTagAssertionPattern = mkRepoTagAssertionPattern "tag" "imageTag"; + + tests = runTests { + testNoImageName = checkAssertionsMatch (imageNameAssertionPattern "no-name") { + services.no-name.image = { + tarball = "/no/name.tar.gz"; + tag = "test"; + }; + }; + + testNoImageTag = checkAssertionsMatch (imageTagAssertionPattern "no-tag") { + services.no-tag.image = { + tarball = "/no/tag.tar.gz"; + name = "test"; + }; + }; + + testImageNameInference = testComposition { + config = { + services.nix-store-path.image.tarball = builtins.storeDir + "/foo-bar-baz.tar.gz"; + }; + expected = "bar-baz"; + fn = composition: composition.config.services.nix-store-path.image.tarball.imageName; + }; + + testImageTagInference = testComposition { + config = { + services.nix-store-path.image.tarball = builtins.storeDir + "/foo-bar-baz.tar.gz"; + }; + expected = "foo"; + fn = composition: composition.config.services.nix-store-path.image.tarball.imageTag; + }; + }; +in +# Abort if `tests`(list containing failed tests) is not empty +pkgs.runCommandNoCC "module-options-arion-test" { } '' + ${pkgs.jq}/bin/jq 'if . == [] then . else halt_error(1) end' > "$out" <<<${escapeShellArg (builtins.toJSON tests)} +''