From ef5202d8a86b6772d4fa8dcb6c9ae48dca6f9261 Mon Sep 17 00:00:00 2001 From: Kiara Grouwstra Date: Mon, 10 Nov 2025 14:07:49 +0100 Subject: [PATCH] test utils (#581) Reviewed-on: https://git.fediversity.eu/fediversity/fediversity/pulls/581 --- .forgejo/workflows/ci.yaml | 6 +++ deployment/utils-test.nix | 89 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 deployment/utils-test.nix diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index 9046ae7d..6f06d5ab 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -14,6 +14,12 @@ concurrency: group: ${{ forgejo.workflow }}-${{ forgejo.event.pull_request.number || forgejo.ref }} jobs: + check-utils: + runs-on: native + steps: + - uses: actions/checkout@v4 + - run: nix-shell --run 'nix-unit ./deployment/utils-test.nix' + check-function: runs-on: native steps: diff --git a/deployment/utils-test.nix b/deployment/utils-test.nix new file mode 100644 index 00000000..4d66d200 --- /dev/null +++ b/deployment/utils-test.nix @@ -0,0 +1,89 @@ +let + inherit (import ../default.nix { }) pkgs; + inherit (pkgs.callPackage ./utils.nix { }) + mapKeys + evalOption + toBash + withPackages + filterNull + withEnv + ; + inherit (pkgs) lib; + inherit (lib) mkOption types; + inherit (types) submodule; +in +{ + _class = "nix-unit"; + + test-mapKeys = { + expr = mapKeys (k: "${k}${k}") { a = 1; }; + expected = { + aa = 1; + }; + }; + + test-evalOption = { + expr = evalOption (mkOption { + type = submodule { + options = { + a = mkOption { default = 3; }; + }; + }; + }) { }; + expected = { + a = 3; + }; + }; + + test-toBash-int = { + expr = toBash 1; + expected = "1"; + }; + + test-toBash-null = { + expr = toBash null; + expected = ""; + }; + + test-toBash-attrs = { + expr = toBash { a = 1; }; + expected = ''{\"a\":1}''; + }; + + test-toBash-str = { + expr = toBash "'b\""; + expected = "'b\\\""; + }; + + test-withPackages = { + expr = withPackages [ pkgs.hello ]; + expected = { + makeWrapperArgs = [ + "--prefix" + "PATH" + ":" + "${lib.makeBinPath [ pkgs.hello ]}" + ]; + }; + }; + + test-filterNull = { + expr = filterNull { + a = 4; + b = null; + }; + expected = { + a = 4; + }; + }; + + test-withEnv = { + expr = withEnv { + a = 1; + b = null; + c = "'d\""; + e.f = [ "g" ]; + }; + expected = ''a="1" b="" c="'d\"" e="{\"f\":[\"g\"]}"''; + }; +}