From ea6dfabe3cd81d0545d3db2b11df598413b4928f Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Sun, 21 Dec 2025 22:43:22 -0600 Subject: [PATCH] tests/zsh: add more test cases for dotdir Adds tests for various dotDir configurations including trailing slashes and spaces. Signed-off-by: Austin Horstman --- tests/modules/programs/zsh/default.nix | 7 ++ tests/modules/programs/zsh/dotdir.nix | 114 +++++++++++++++---------- 2 files changed, 76 insertions(+), 45 deletions(-) diff --git a/tests/modules/programs/zsh/default.nix b/tests/modules/programs/zsh/default.nix index 998be5f0..95eb564b 100644 --- a/tests/modules/programs/zsh/default.nix +++ b/tests/modules/programs/zsh/default.nix @@ -3,6 +3,13 @@ zsh-aliases = ./aliases.nix; zsh-dotdir-absolute = import ./dotdir.nix "absolute"; zsh-dotdir-default = import ./dotdir.nix "default"; + zsh-dotdir-path-normalization-abs-no-slash = import ./dotdir.nix "abs-no-slash"; + zsh-dotdir-path-normalization-abs-slash = import ./dotdir.nix "abs-slash"; + zsh-dotdir-path-normalization-rel-no-slash = import ./dotdir.nix "rel-no-slash"; + zsh-dotdir-path-normalization-rel-slash = import ./dotdir.nix "rel-slash"; + zsh-dotdir-path-normalization-root-no-slash = import ./dotdir.nix "root-no-slash"; + zsh-dotdir-path-normalization-root-slash = import ./dotdir.nix "root-slash"; + zsh-dotdir-path-normalization-abs-space = import ./dotdir.nix "abs-space"; zsh-dotdir-relative = import ./dotdir.nix "relative"; zsh-dotdir-shell-variable = import ./dotdir.nix "shell-variable"; zsh-history-ignore-pattern = ./history-ignore-pattern.nix; diff --git a/tests/modules/programs/zsh/dotdir.nix b/tests/modules/programs/zsh/dotdir.nix index 244b1985..703dbee6 100644 --- a/tests/modules/programs/zsh/dotdir.nix +++ b/tests/modules/programs/zsh/dotdir.nix @@ -7,24 +7,46 @@ case: }: let home = config.home.homeDirectory; + subDir = "subdir/subdir2"; - dotDir = + dotDirCases = { + absolute = "${home}/${subDir}"; + relative = subDir; + inherit (options.programs.zsh.dotDir) default; + shell-variable = "\${XDG_CONFIG_HOME:-$HOME/.config}/zsh"; + + # Path normalization cases + abs-no-slash = "${home}/subdir"; + abs-slash = "${home}/subdir/"; + rel-no-slash = "subdir"; + rel-slash = "subdir/"; + root-no-slash = "${home}"; + root-slash = "${home}/"; + abs-space = "${home}/subdir with space"; + }; + + dotDir = dotDirCases.${case} or (abort "Unknown case: ${case}"); + + # Normalize absolute path to match module behavior (no trailing slash) + absDotDir = let - subDir = "subdir/subdir2"; + fullPath = if lib.hasPrefix "/" dotDir then dotDir else "${home}/${dotDir}"; in - if case == "absolute" then - "${home}/${subDir}" - else if case == "relative" then - subDir - else if case == "default" then - options.programs.zsh.dotDir.default - else if case == "shell-variable" then - "\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh" - else - abort "Test condition not provided."; + lib.removeSuffix "/" fullPath; - absDotDir = lib.optionalString (!lib.hasPrefix home dotDir) "${home}/" + dotDir; - relDotDir = lib.removePrefix home dotDir; + # Calculate relative path for file location assertions + relDotDir = + let + # Use the normalized absDotDir to determine relative location + rawRel = lib.removePrefix home absDotDir; + in + if lib.hasPrefix "/" rawRel then lib.removePrefix "/" rawRel else rawRel; + + isRelative = lib.elem case [ + "relative" + "rel-no-slash" + "rel-slash" + ]; in { config = { @@ -33,33 +55,37 @@ in inherit dotDir; }; - test.stubs.zsh = { }; + test = { + stubs.zsh = { }; - test.asserts.warnings.expected = lib.optionals (case == "relative") [ - '' - Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release. - Current dotDir: subdir/subdir2 - Consider using absolute paths or home-manager config options instead. - You can replace relative paths or environment variables with options like: - - config.home.homeDirectory (user's home directory) - - config.xdg.configHome (XDG config directory) - - config.xdg.dataHome (XDG data directory) - - config.xdg.cacheHome (XDG cache directory) - '' - ]; + asserts = { + assertions.expected = lib.optionals (case == "shell-variable") [ + '' + programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time. + Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh + Consider using an absolute path or home-manager config options instead. + You can replace shell variables with options like: + - config.home.homeDirectory (user's home directory) + - config.xdg.configHome (XDG config directory) + - config.xdg.dataHome (XDG data directory) + - config.xdg.cacheHome (XDG cache directory) + '' + ]; - test.asserts.assertions.expected = lib.optionals (case == "shell-variable") [ - '' - programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time. - Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh - Consider using an absolute path or home-manager config options instead. - You can replace shell variables with options like: - - config.home.homeDirectory (user's home directory) - - config.xdg.configHome (XDG config directory) - - config.xdg.dataHome (XDG data directory) - - config.xdg.cacheHome (XDG cache directory) - '' - ]; + warnings.expected = lib.optionals isRelative [ + '' + Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release. + Current dotDir: ${dotDir} + Consider using absolute paths or home-manager config options instead. + You can replace relative paths or environment variables with options like: + - config.home.homeDirectory (user's home directory) + - config.xdg.configHome (XDG config directory) + - config.xdg.dataHome (XDG data directory) + - config.xdg.cacheHome (XDG cache directory) + '' + ]; + }; + }; nmt.script = if case == "shell-variable" then @@ -70,17 +96,15 @@ in else lib.concatStringsSep "\n" [ # check dotDir entrypoint exists - "assertFileExists home-files/${relDotDir}/.zshenv" + "assertFileExists 'home-files/${if relDotDir == "" then "" else "${relDotDir}/"}.zshenv'" # for non-default dotDir only: - (lib.optionalString (case != "default") '' + (lib.optionalString (case != "default" && case != "root-slash" && case != "root-no-slash") '' # check .zshenv in homeDirectory sources .zshenv in dotDir - assertFileRegex home-files/.zshenv \ - "source [\"']\?${absDotDir}/.zshenv[\"']\?" + assertFileRegex home-files/.zshenv "source ${lib.escapeShellArg "${absDotDir}/.zshenv"}" # check that .zshenv in dotDir exports ZDOTDIR - assertFileRegex home-files/${relDotDir}/.zshenv \ - "export ZDOTDIR=[\"']\?${absDotDir}[\"']\?" + assertFileRegex 'home-files/${relDotDir}/.zshenv' "export ZDOTDIR=\"${absDotDir}\"" '') ]; };