Skip to content

Commit 4aa2ed1

Browse files
committed
feat(lib): add nix-unit tests for lib helpers
Cover the pure khanelinix helpers (base64 decode, module capitalize / boolToNum / option builders / attr-priority helpers, file mergeAttrs, and theme constructors) with nix-unit cases kept next to the library in lib/tests. Wire them through a flake-parts module, guarding the nix-unit import the same way the git-hooks module is guarded, and route the tests output via the dev partition so `nix-unit --flake .#tests` and the generated checks.<system>.nix-unit both resolve.
1 parent 77c78dd commit 4aa2ed1

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

flake/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ in
2424
"devShells"
2525
"formatter"
2626
"templates"
27+
"tests"
2728
] (_: "dev");
2829
}

flake/dev/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ in
1313
./checks.nix
1414
./parse.nix
1515
./templates.nix
16+
./tests.nix
1617
./treefmt.nix
1718
];
1819

flake/dev/tests.nix

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
inputs,
3+
lib,
4+
self,
5+
...
6+
}:
7+
let
8+
hasNixUnit = inputs.nix-unit ? modules;
9+
in
10+
{
11+
imports = lib.optional hasNixUnit inputs.nix-unit.modules.flake.default;
12+
13+
# Pure `lib.khanelinix` helper tests, run via `nix flake check` (check
14+
# `nix-unit`) or ad hoc with `nix-unit --flake .#tests` from the dev shell.
15+
# Cases live next to the library in `lib/tests`.
16+
flake.tests = lib.mkIf hasNixUnit (import ../../lib/tests { inherit self lib; });
17+
18+
perSystem = _: {
19+
nix-unit = lib.mkIf hasNixUnit {
20+
# `checks.<system>.nix-unit` re-evaluates `self#tests` in the build
21+
# sandbox. Pass the primary inputs to avoid refetching them, and allow
22+
# network for the remaining transitive inputs this partitioned flake
23+
# drags in through its overlays (e.g. home-manager).
24+
inputs = {
25+
inherit (inputs) nixpkgs flake-parts nix-unit;
26+
};
27+
allowNetwork = true;
28+
};
29+
};
30+
}

lib/tests/default.nix

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
self,
3+
lib,
4+
}:
5+
let
6+
inherit (self.lib)
7+
base64
8+
file
9+
module
10+
theme
11+
;
12+
in
13+
{
14+
# base64.decode
15+
testBase64DecodeHello = {
16+
expr = base64.decode "aGVsbG8=";
17+
expected = "hello";
18+
};
19+
20+
testBase64DecodeNoPadding = {
21+
expr = base64.decode "Zm9vYmFy";
22+
expected = "foobar";
23+
};
24+
25+
testBase64DecodeOnePad = {
26+
expr = base64.decode "Zm9vYmE=";
27+
expected = "fooba";
28+
};
29+
30+
testBase64DecodeTwoPad = {
31+
expr = base64.decode "Zm9vYg==";
32+
expected = "foob";
33+
};
34+
35+
# module.capitalize
36+
testCapitalizeWord = {
37+
expr = module.capitalize "hello";
38+
expected = "Hello";
39+
};
40+
41+
testCapitalizeSingle = {
42+
expr = module.capitalize "a";
43+
expected = "A";
44+
};
45+
46+
testCapitalizeEmpty = {
47+
expr = module.capitalize "";
48+
expected = "";
49+
};
50+
51+
# module.boolToNum
52+
testBoolToNumTrue = {
53+
expr = module.boolToNum true;
54+
expected = 1;
55+
};
56+
57+
testBoolToNumFalse = {
58+
expr = module.boolToNum false;
59+
expected = 0;
60+
};
61+
62+
# module.enabled / module.disabled
63+
testEnabled = {
64+
expr = module.enabled;
65+
expected = {
66+
enable = true;
67+
};
68+
};
69+
70+
testDisabled = {
71+
expr = module.disabled;
72+
expected = {
73+
enable = false;
74+
};
75+
};
76+
77+
# module.enableForSystem keeps modules with no `systems` or a matching one.
78+
testEnableForSystem = {
79+
expr = module.enableForSystem "x86_64-linux" [
80+
{ name = "any"; }
81+
{
82+
name = "darwin-only";
83+
systems = [ "aarch64-darwin" ];
84+
}
85+
{
86+
name = "linux-only";
87+
systems = [ "x86_64-linux" ];
88+
}
89+
];
90+
expected = [
91+
{ name = "any"; }
92+
{
93+
name = "linux-only";
94+
systems = [ "x86_64-linux" ];
95+
}
96+
];
97+
};
98+
99+
# module.mkOpt / module.mkBoolOpt resolve to module-system options.
100+
testMkOptDefault = {
101+
expr = (module.mkOpt' lib.types.int 5).default;
102+
expected = 5;
103+
};
104+
105+
testMkBoolOptDefault = {
106+
expr = (module.mkBoolOpt' true).default;
107+
expected = true;
108+
};
109+
110+
# module.default-attrs / module.force-attrs wrap values with merge priorities.
111+
testDefaultAttrsContent = {
112+
expr = (module.default-attrs { a = 1; }).a.content;
113+
expected = 1;
114+
};
115+
116+
testDefaultAttrsPriority = {
117+
expr = (module.default-attrs { a = 1; }).a.priority;
118+
expected = 1000;
119+
};
120+
121+
testForceAttrsPriority = {
122+
expr = (module.force-attrs { a = 1; }).a.priority;
123+
expected = 50;
124+
};
125+
126+
# file.mergeAttrs (later sets win)
127+
testMergeAttrs = {
128+
expr = file.mergeAttrs [
129+
{
130+
a = 1;
131+
b = 1;
132+
}
133+
{
134+
b = 2;
135+
c = 3;
136+
}
137+
];
138+
expected = {
139+
a = 1;
140+
b = 2;
141+
c = 3;
142+
};
143+
};
144+
145+
testMergeAttrsEmpty = {
146+
expr = file.mergeAttrs [ ];
147+
expected = { };
148+
};
149+
150+
# theme helpers
151+
testThemeMkColorScheme = {
152+
expr = theme.mkColorScheme "test" { bg = "#000000"; };
153+
expected = {
154+
name = "test";
155+
colors = {
156+
bg = "#000000";
157+
};
158+
type = "colorScheme";
159+
};
160+
};
161+
162+
testThemeGetColors = {
163+
expr = theme.getColors {
164+
colors = {
165+
fg = "#ffffff";
166+
};
167+
};
168+
expected = {
169+
fg = "#ffffff";
170+
};
171+
};
172+
173+
testThemeGetColorsMissing = {
174+
expr = theme.getColors { };
175+
expected = { };
176+
};
177+
178+
testThemeVariants = {
179+
expr = theme.variants;
180+
expected = {
181+
light = "light";
182+
dark = "dark";
183+
};
184+
};
185+
}

0 commit comments

Comments
 (0)