about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-24 18:01:02 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-24 18:24:58 +0100
commit445ee1fa4a9e82250d21c66ba957a1e19077efd9 (patch)
tree6efbe6a5fe6fcad0da3b57443b246f53eaa81588
parentfix(modules/nvim/opts): Ensure a consistent fileformat (diff)
downloadnixos-config-445ee1fa4a9e82250d21c66ba957a1e19077efd9.tar.gz
nixos-config-445ee1fa4a9e82250d21c66ba957a1e19077efd9.zip
tests(infrastructure/driver): Support setting the golden file
-rw-r--r--tests/README.md5
-rw-r--r--tests/infrastructure/default.nix15
-rw-r--r--tests/infrastructure/driver.sh9
3 files changed, 26 insertions, 3 deletions
diff --git a/tests/README.md b/tests/README.md
index 0c55bdf1..698c5d83 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -85,3 +85,8 @@ must match.
 
 Does the same thing as [Expect](#expect), but enforces, that the
 regex does not match.
+
+#### `SetGolden`
+
+Set the golden file (the file used for the hash generation) to `ARGS`.
+`ARGS` must be a valid file path.
diff --git a/tests/infrastructure/default.nix b/tests/infrastructure/default.nix
index 4f1ec7a2..64a6ad01 100644
--- a/tests/infrastructure/default.nix
+++ b/tests/infrastructure/default.nix
@@ -82,7 +82,7 @@ nixos-lib.runTest {
       ''}")
 
       machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkGoldenRecord" ''
-        set -e
+        set -ex
 
         # HACK: Prevent zsh from complaining about missing configuration.
         # TODO: These tests should probably just run under bash. <2024-11-22>
@@ -95,6 +95,8 @@ nixos-lib.runTest {
 
         __TEST_EVAL_AWK_CLEAN_FILE="${./clean.awk}"
         __TEST_EVAL_LOG_FILE="${logFile}"
+        __TEST_EVAL_GOLDEN_FILE="$(mktemp)"
+        printf "%s" "${goldenFile}" >"$__TEST_EVAL_GOLDEN_FILE"
 
         . ${./driver.sh}
 
@@ -105,9 +107,16 @@ nixos-lib.runTest {
 
         # Clear the pipe again
         "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE"
+
+        # Check if the golden file was changed.
+        if [ "$(cat "$__TEST_EVAL_GOLDEN_FILE")" != "${goldenFile}" ]; then
+          mv "$(cat "$__TEST_EVAL_GOLDEN_FILE")" "${goldenFile}"
+        fi
+
+        rm "$__TEST_EVAL_GOLDEN_FILE"
       ''}")
 
-      machine.succeed("sudo -u soispha ${pkgs.writeShellScript "testHashOfGolden" ''
+      machine.succeed("sudo -u soispha ${pkgs.writeShellScript "testHashOfGolden" (''
           set -e
 
           cd "${testDir}"
@@ -127,7 +136,7 @@ nixos-lib.runTest {
           else
             echo "Hash was successfully checked."
           fi
-        ''}")
+        '')}")
 
 
       machine.copy_from_vm("${goldenFile}", "golden")
diff --git a/tests/infrastructure/driver.sh b/tests/infrastructure/driver.sh
index 52f7d3ad..f8688161 100644
--- a/tests/infrastructure/driver.sh
+++ b/tests/infrastructure/driver.sh
@@ -63,6 +63,15 @@ __test_eval() {
                 ;;
             esac
             ;;
+        "SetGolden")
+            msg "Trying to set '$args' as golden file."
+            [ -f "$args" ] || {
+                msg "Argument is not a file!"
+                exit 1
+            }
+            printf "%s" "$args" >"$__TEST_EVAL_GOLDEN_FILE"
+            msg "Set golden file to: '$args'"
+            ;;
         *)
             msg "Unrecognized command: '$cmd'"
             exit 1