{ pkgs, myPkgs, lib, nixos-lib, extraModules, sysLib, ... }: { name, configuration, description, hash, testData, testShell ? pkgs.dash, alternateScreen ? false, }: nixos-lib.runTest { hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs inherit name; node = { specialArgs = { inherit myPkgs sysLib; }; # Use the nixpkgs as constructed by the `nixpkgs.*` options pkgs = null; }; nodes = { machine = {config, ...}: { imports = [ extraModules.home-manager ../../modules/by-name/us/users/module.nix ] ++ configuration.imports; config = lib.modules.mkMerge [ { soispha = { users = { enable = true; # The password is 'test'. hashedPassword = "$y$j9T$yYYUpE9OaxmEO8MaPI2jr0$WGpYYWaLySakI.Mwqz4sljGSOetAp4s5CIUa1VUU1l2"; }; }; home-manager.users.soispha.home.stateVersion = "24.11"; } configuration.config ]; }; }; testScript = {nodes, ...}: let testDir = "${nodes.machine.home-manager.users.soispha.home.homeDirectory}/test"; goldenFile = "${testDir}/__test_golden"; logFile = "${testDir}/__test_log"; testTmux = lib.getExe pkgs.tmux; in /* python */ '' start_all() __TEST_TMUX="${testTmux}" machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkTestEnvironment" '' set -e mkdir --parents "${testDir}" cd "${testDir}" ${ lib.strings.concatStringsSep "\n\n" ( builtins.attrValues (builtins.mapAttrs (name: value: '' mkdir --parents "$(dirname '${name}')" cp --recursive '${value}' '${name}' '') testData) ) } ''}") machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkGoldenRecord" '' set -e # HACK: Prevent zsh from complaining about missing configuration. # TODO: These tests should probably just run under bash. <2024-11-22> touch ~soispha/.zshrc cd "${testDir}" __TEST_TMUX="${testTmux}" __TEST_SHELL="${lib.getExe testShell}" __TEST_TMUX_PANE="__TEST_TMUX_PANE" __TEST_EVAL_USE_ALTERNATE_SCREEN="${ if alternateScreen then "true" else "false" }" __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} "$__TEST_TMUX" new-session -d -s "$__TEST_TMUX_PANE" "$__TEST_SHELL" # Warm up the shell in the tmux sesssion "$__TEST_TMUX" send-keys -t "$__TEST_TMUX_PANE" "echo hi" "Enter" "clear" "Enter" sleep 2 "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" -o 'cat >>${goldenFile}' __test_eval "${description}" # Remove the pipe "$__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" ('' set -e cd "${testDir}" '' + lib.optionalString (hash != null) /* bash */ '' golden_hash="$(sha256sum ${goldenFile} | awk '{print $1}')" if [ "$golden_hash" != "${hash}" ]; then echo "Hash mismatch." echo "Expected '${hash}'," echo "but got '$golden_hash'" exit 1 else echo "Hash was successfully checked." fi '')}") machine.copy_from_vm("${goldenFile}", "golden") machine.copy_from_vm("${logFile}", "log") ''; }