Skip to content

Commit 9d70404

Browse files
committed
tests/zsh: add more test cases for dotdir
Adds tests for various dotDir configurations including trailing slashes and spaces. Signed-off-by: Austin Horstman <[email protected]>
1 parent 6c58157 commit 9d70404

File tree

2 files changed

+73
-45
lines changed

2 files changed

+73
-45
lines changed

tests/modules/programs/zsh/default.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
zsh-aliases = ./aliases.nix;
44
zsh-dotdir-absolute = import ./dotdir.nix "absolute";
55
zsh-dotdir-default = import ./dotdir.nix "default";
6+
zsh-dotdir-path-normalization-abs-no-slash = import ./dotdir.nix "abs-no-slash";
7+
zsh-dotdir-path-normalization-abs-slash = import ./dotdir.nix "abs-slash";
8+
zsh-dotdir-path-normalization-rel-no-slash = import ./dotdir.nix "rel-no-slash";
9+
zsh-dotdir-path-normalization-rel-slash = import ./dotdir.nix "rel-slash";
10+
zsh-dotdir-path-normalization-root-no-slash = import ./dotdir.nix "root-no-slash";
11+
zsh-dotdir-path-normalization-root-slash = import ./dotdir.nix "root-slash";
12+
zsh-dotdir-path-normalization-abs-space = import ./dotdir.nix "abs-space";
613
zsh-dotdir-relative = import ./dotdir.nix "relative";
714
zsh-dotdir-shell-variable = import ./dotdir.nix "shell-variable";
815
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;

tests/modules/programs/zsh/dotdir.nix

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,43 @@ case:
77
}:
88
let
99
home = config.home.homeDirectory;
10+
subDir = "subdir/subdir2";
1011

11-
dotDir =
12+
dotDirCases = {
13+
absolute = "${home}/${subDir}";
14+
relative = subDir;
15+
inherit (options.programs.zsh.dotDir) default;
16+
shell-variable = "\${XDG_CONFIG_HOME:-$HOME/.config}/zsh";
17+
18+
# Path normalization cases
19+
abs-no-slash = "${home}/subdir";
20+
abs-slash = "${home}/subdir/";
21+
rel-no-slash = "subdir";
22+
rel-slash = "subdir/";
23+
root-no-slash = "${home}";
24+
root-slash = "${home}/";
25+
abs-space = "${home}/subdir with space";
26+
};
27+
28+
dotDir = dotDirCases.${case} or (abort "Unknown case: ${case}");
29+
30+
# Normalize absolute path to match module behavior (no trailing slash)
31+
absDotDir =
1232
let
13-
subDir = "subdir/subdir2";
33+
fullPath = if lib.hasPrefix "/" dotDir then dotDir else "${home}/${dotDir}";
1434
in
15-
if case == "absolute" then
16-
"${home}/${subDir}"
17-
else if case == "relative" then
18-
subDir
19-
else if case == "default" then
20-
options.programs.zsh.dotDir.default
21-
else if case == "shell-variable" then
22-
"\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh"
23-
else
24-
abort "Test condition not provided.";
35+
lib.removeSuffix "/" fullPath;
36+
37+
# Calculate relative path for file location assertions
38+
relDotDir =
39+
let
40+
# Use the normalized absDotDir to determine relative location
41+
rawRel = lib.removePrefix home absDotDir;
42+
in
43+
if lib.hasPrefix "/" rawRel then lib.removePrefix "/" rawRel else rawRel;
44+
45+
isRelative = case == "relative" || case == "rel-no-slash" || case == "rel-slash";
2546

26-
absDotDir = lib.optionalString (!lib.hasPrefix home dotDir) "${home}/" + dotDir;
27-
relDotDir = lib.removePrefix home dotDir;
2847
in
2948
{
3049
config = {
@@ -33,33 +52,37 @@ in
3352
inherit dotDir;
3453
};
3554

36-
test.stubs.zsh = { };
55+
test = {
56+
stubs.zsh = { };
3757

38-
test.asserts.warnings.expected = lib.optionals (case == "relative") [
39-
''
40-
Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release.
41-
Current dotDir: subdir/subdir2
42-
Consider using absolute paths or home-manager config options instead.
43-
You can replace relative paths or environment variables with options like:
44-
- config.home.homeDirectory (user's home directory)
45-
- config.xdg.configHome (XDG config directory)
46-
- config.xdg.dataHome (XDG data directory)
47-
- config.xdg.cacheHome (XDG cache directory)
48-
''
49-
];
58+
asserts = {
59+
assertions.expected = lib.optionals (case == "shell-variable") [
60+
''
61+
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
62+
Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh
63+
Consider using an absolute path or home-manager config options instead.
64+
You can replace shell variables with options like:
65+
- config.home.homeDirectory (user's home directory)
66+
- config.xdg.configHome (XDG config directory)
67+
- config.xdg.dataHome (XDG data directory)
68+
- config.xdg.cacheHome (XDG cache directory)
69+
''
70+
];
5071

51-
test.asserts.assertions.expected = lib.optionals (case == "shell-variable") [
52-
''
53-
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
54-
Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh
55-
Consider using an absolute path or home-manager config options instead.
56-
You can replace shell variables with options like:
57-
- config.home.homeDirectory (user's home directory)
58-
- config.xdg.configHome (XDG config directory)
59-
- config.xdg.dataHome (XDG data directory)
60-
- config.xdg.cacheHome (XDG cache directory)
61-
''
62-
];
72+
warnings.expected = lib.optionals isRelative [
73+
''
74+
Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release.
75+
Current dotDir: ${dotDir}
76+
Consider using absolute paths or home-manager config options instead.
77+
You can replace relative paths or environment variables with options like:
78+
- config.home.homeDirectory (user's home directory)
79+
- config.xdg.configHome (XDG config directory)
80+
- config.xdg.dataHome (XDG data directory)
81+
- config.xdg.cacheHome (XDG cache directory)
82+
''
83+
];
84+
};
85+
};
6386

6487
nmt.script =
6588
if case == "shell-variable" then
@@ -70,17 +93,15 @@ in
7093
else
7194
lib.concatStringsSep "\n" [
7295
# check dotDir entrypoint exists
73-
"assertFileExists home-files/${relDotDir}/.zshenv"
96+
"assertFileExists 'home-files/${if relDotDir == "" then "" else "${relDotDir}/"}.zshenv'"
7497

7598
# for non-default dotDir only:
76-
(lib.optionalString (case != "default") ''
99+
(lib.optionalString (case != "default" && case != "root-slash" && case != "root-no-slash") ''
77100
# check .zshenv in homeDirectory sources .zshenv in dotDir
78-
assertFileRegex home-files/.zshenv \
79-
"source [\"']\?${absDotDir}/.zshenv[\"']\?"
101+
assertFileRegex home-files/.zshenv "source ${lib.escapeShellArg absDotDir}/.zshenv"
80102
81103
# check that .zshenv in dotDir exports ZDOTDIR
82-
assertFileRegex home-files/${relDotDir}/.zshenv \
83-
"export ZDOTDIR=[\"']\?${absDotDir}[\"']\?"
104+
assertFileRegex 'home-files/${relDotDir}/.zshenv' "export ZDOTDIR=${lib.escapeShellArg absDotDir}"
84105
'')
85106
];
86107
};

0 commit comments

Comments
 (0)