From 38b5138956a027bf05f3ddc33b7d54d0b74d04c7 Mon Sep 17 00:00:00 2001 From: Alex David Date: Thu, 17 Jul 2025 20:25:17 -0700 Subject: [PATCH] Add initial simple test cases --- tests/.gitignore | 1 + tests/flake.lock | 27 +++++++++++++++++ tests/flake.nix | 47 +++++++++++++++++++++++++++++ tests/test-cases.nix | 72 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 tests/.gitignore create mode 100644 tests/flake.lock create mode 100644 tests/flake.nix create mode 100644 tests/test-cases.nix diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..c4a847d --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +/result diff --git a/tests/flake.lock b/tests/flake.lock new file mode 100644 index 0000000..e23651e --- /dev/null +++ b/tests/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1749727998, + "narHash": "sha256-mHv/yeUbmL91/TvV95p+mBVahm9mdQMJoqaTVTALaFw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "fd487183437963a59ba763c0cc4f27e3447dd6dd", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-25.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/tests/flake.nix b/tests/flake.nix new file mode 100644 index 0000000..ded15cc --- /dev/null +++ b/tests/flake.nix @@ -0,0 +1,47 @@ +{ + # This is a separate flake file from the one at the root because we don't + # want to depend on nixpkgs + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; + outputs = { nixpkgs, ... }: let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + lib = pkgs.lib; + jail = import ../lib.nix { inherit pkgs; }; + + ansi = clrCode: "[${toString clrCode}m"; + red = ansi 31; + reset = ansi 0; + + test = { + assertStdout = actual: expected: '' + ACTUAL=$(${lib.getExe actual}) + if test "$ACTUAL" != ${lib.escapeShellArg expected}; then + printf '${red}Expected "%s" to equal "%s".${reset}' "$ACTUAL" ${lib.escapeShellArg expected} + exit 1 + fi + ''; + + assertFileDoesNotExist = fileName: '' + if test -e ${lib.escapeShellArg fileName}; then + printf '${red}Expected "%s" to not exist.${reset}' ${lib.escapeShellArg fileName} + exit 1 + fi + ''; + }; + + normalizeTestBody = testBody: + let type = builtins.typeOf testBody; in + if type == "string" then testBody else + if type == "list" then lib.concatStringsSep "\n" (map normalizeTestBody testBody) else + if lib.isDerivation testBody then lib.getExe testBody else + throw "Unknown test body type ${type}"; + in { + checks.x86_64-linux = lib.mapAttrs (name: testBody: + pkgs.runCommand (lib.strings.sanitizeDerivationName name) {} '' + export HOME=$(pwd) + export LANG= + ${normalizeTestBody testBody} + touch $out + '' + ) (import ./test-cases.nix { inherit pkgs lib test jail; }); + }; +} diff --git a/tests/test-cases.nix b/tests/test-cases.nix new file mode 100644 index 0000000..6861246 --- /dev/null +++ b/tests/test-cases.nix @@ -0,0 +1,72 @@ +{ pkgs, lib, test, jail }: with test; let + sh = script: (pkgs.writeShellApplication { name = "test-script"; text = script; }); + defaultPath = "${pkgs.coreutils}/bin"; +in { + "simple jail works" = assertStdout + (jail "hello" pkgs.hello []) + "Hello, world!"; + + "it sets a sane default path" = assertStdout + (jail "test" (sh ''echo "$PATH"'') []) + defaultPath; + + "it does not forward environment variables by default" = [ + "export MY_ENV_VAR=secret" + (assertStdout + (jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') []) + "unset" + ) + ]; + + "combinators/add-cleanup" = [ + (assertStdout + (jail "test" (sh "cat /foo-in-jail") (c: [ + (c.ro-bind "foo" "/foo-in-jail") + (c.add-runtime "echo hello > foo") + (c.add-cleanup "rm foo") + ])) + "hello" + ) + (assertFileDoesNotExist "foo") + ]; + + "combinators/add-path" = assertStdout + (jail "test" (sh ''echo "$PATH"'') (c: [ + (c.add-path "/some/new/path") + ])) + "${defaultPath}:/some/new/path"; + + "combinators/add-pkg-deps" = assertStdout + (jail "test" (sh ''echo "$PATH"'') (c: [ + (c.add-pkg-deps [ pkgs.hello ]) + ])) + "${defaultPath}:${pkgs.hello}/bin"; + + "combinators/bind-pkg" = assertStdout + (jail "test" (sh "cat /foo") (c: [ + (c.bind-pkg "/foo" (pkgs.writeText "foo" "bar")) + ])) + "bar"; + + "combinators/fwd-env" = [ + "export MY_ENV_VAR=secret" + (assertStdout + (jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.fwd-env "MY_ENV_VAR") + ])) + "secret" + ) + ]; + + "combinators/set-argv" = assertStdout + (jail "test" (sh "printf '1=%s,2=%s,3=%s' \"$@\"") (c: [ + (c.set-argv [ "foo" "bar baz" "foo>'bar'" ]) + ])) + "1=foo,2=bar baz,3=foo>'bar'"; + + "combinators/set-env" = assertStdout + (jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [ + (c.set-env "MY_ENV_VAR" "some-value") + ])) + "some-value"; +}