|
| 1 | +{ pkgs, lib ? pkgs.lib, ... }: |
| 2 | +let |
| 3 | + inherit (lib) any escapeShellArg replaceStrings runTests toList; |
| 4 | + |
| 5 | + evalComposition = modules: import ../../src/nix/eval-composition.nix { |
| 6 | + inherit pkgs; |
| 7 | + modules = toList modules; |
| 8 | + }; |
| 9 | + |
| 10 | + testComposition = { expected, fn, config }: { |
| 11 | + inherit expected; |
| 12 | + expr = fn (evalComposition config); |
| 13 | + }; |
| 14 | + |
| 15 | + search = pattern: str: (builtins.match ".*${pattern}.*" str) != null; |
| 16 | + |
| 17 | + assertionsMatch = patterns: assertions: |
| 18 | + let |
| 19 | + failed = builtins.filter (assertion: !assertion.assertion) assertions; |
| 20 | + matchAnyPattern = assertion: any (pattern: search pattern assertion.message) (toList patterns); |
| 21 | + in |
| 22 | + any matchAnyPattern failed; |
| 23 | + |
| 24 | + checkAssertions = expected: patterns: config: testComposition { |
| 25 | + inherit expected config; |
| 26 | + fn = composition: assertionsMatch patterns composition.config.assertions; |
| 27 | + }; |
| 28 | + |
| 29 | + checkAssertionsMatch = checkAssertions true; |
| 30 | + checkAssertionsDoNotMatch = checkAssertions false; |
| 31 | + |
| 32 | + mkRepoTagAssertionPattern = component: attrName: name: replaceStrings [ "\n" ] [ " " ] '' |
| 33 | + Unable to infer the ${component} of the image associated with |
| 34 | + config\.services\.${name}\. Please set |
| 35 | + config\.services\.${name}\.image\.${attrName} to a non-empty string\. |
| 36 | + ''; |
| 37 | + |
| 38 | + imageNameAssertionPattern = mkRepoTagAssertionPattern "name" "imageName"; |
| 39 | + imageTagAssertionPattern = mkRepoTagAssertionPattern "tag" "imageTag"; |
| 40 | + |
| 41 | + tests = runTests { |
| 42 | + testNoImageName = checkAssertionsMatch (imageNameAssertionPattern "no-name") { |
| 43 | + services.no-name.image = { |
| 44 | + tarball = "/no/name.tar.gz"; |
| 45 | + tag = "test"; |
| 46 | + }; |
| 47 | + }; |
| 48 | + |
| 49 | + testNoImageTag = checkAssertionsMatch (imageTagAssertionPattern "no-tag") { |
| 50 | + services.no-tag.image = { |
| 51 | + tarball = "/no/tag.tar.gz"; |
| 52 | + name = "test"; |
| 53 | + }; |
| 54 | + }; |
| 55 | + |
| 56 | + testImageNameInference = testComposition { |
| 57 | + config = { |
| 58 | + services.nix-store-path.image.tarball = builtins.storeDir + "/foo-bar-baz.tar.gz"; |
| 59 | + }; |
| 60 | + expected = "bar-baz"; |
| 61 | + fn = composition: composition.config.services.nix-store-path.image.tarball.imageName; |
| 62 | + }; |
| 63 | + |
| 64 | + testImageTagInference = testComposition { |
| 65 | + config = { |
| 66 | + services.nix-store-path.image.tarball = builtins.storeDir + "/foo-bar-baz.tar.gz"; |
| 67 | + }; |
| 68 | + expected = "foo"; |
| 69 | + fn = composition: composition.config.services.nix-store-path.image.tarball.imageTag; |
| 70 | + }; |
| 71 | + }; |
| 72 | +in |
| 73 | +# Abort if `tests`(list containing failed tests) is not empty |
| 74 | +pkgs.runCommandNoCC "module-options-arion-test" { } '' |
| 75 | + ${pkgs.jq}/bin/jq 'if . == [] then . else halt_error(1) end' > "$out" <<<${escapeShellArg (builtins.toJSON tests)} |
| 76 | +'' |
0 commit comments