1
0
Fork 0
selfhostblocks/test/modules/lib.nix

131 lines
2.9 KiB
Nix
Raw Normal View History

2024-03-01 00:34:53 +01:00
{ pkgs, lib, ... }:
let
shblib = pkgs.callPackage ../../lib {};
2024-10-22 00:51:08 +02:00
inherit (lib) nameValuePair;
2024-03-01 00:34:53 +01:00
in
{
# Tests that withReplacements can:
# - recurse in attrs and lists
# - .source field is understood
# - .transform field is understood
# - if .source field is found, ignores other fields
testLibWithReplacements = {
expected =
let
item = root: {
a = "A";
b = "%SECRET_${root}B%";
c = "%SECRET_${root}C%";
};
in
(item "") // {
nestedAttr = item "NESTEDATTR_";
nestedList = [ (item "NESTEDLIST_0_") ];
doubleNestedList = [ { n = (item "DOUBLENESTEDLIST_0_N_"); } ];
};
expr =
let
item = {
a = "A";
b.source = "/path/B";
b.transform = null;
c.source = "/path/C";
c.transform = v: "prefix-${v}-suffix";
c.other = "other";
};
in
shblib.withReplacements (
item // {
nestedAttr = item;
nestedList = [ item ];
doubleNestedList = [ { n = item; } ];
}
);
};
testLibWithReplacementsRootList = {
expected =
let
item = root: {
a = "A";
b = "%SECRET_${root}B%";
c = "%SECRET_${root}C%";
};
in
[
(item "0_")
(item "1_")
[ (item "2_0_") ]
[ { n = (item "3_0_N_"); } ]
];
expr =
let
item = {
a = "A";
b.source = "/path/B";
b.transform = null;
c.source = "/path/C";
c.transform = v: "prefix-${v}-suffix";
c.other = "other";
};
in
shblib.withReplacements [
item
item
[ item ]
[ { n = item; } ]
];
};
testLibGetReplacements = {
expected =
let
2024-10-22 00:51:08 +02:00
secrets = root: [
(nameValuePair "%SECRET_${root}B%" "$(cat /path/B)")
(nameValuePair "%SECRET_${root}C%" "prefix-$(cat /path/C)-suffix")
];
2024-03-01 00:34:53 +01:00
in
2024-10-22 00:51:08 +02:00
(secrets "") ++
(secrets "DOUBLENESTEDLIST_0_N_") ++
(secrets "NESTEDATTR_") ++
(secrets "NESTEDLIST_0_");
2024-03-01 00:34:53 +01:00
expr =
let
item = {
a = "A";
b.source = "/path/B";
b.transform = null;
c.source = "/path/C";
c.transform = v: "prefix-${v}-suffix";
c.other = "other";
};
in
2024-10-22 00:51:08 +02:00
map shblib.genReplacement (shblib.getReplacements (
2024-03-01 00:34:53 +01:00
item // {
nestedAttr = item;
nestedList = [ item ];
doubleNestedList = [ { n = item; } ];
}
2024-10-22 00:51:08 +02:00
));
2024-03-01 00:34:53 +01:00
};
testParseXML = {
expected = {
"a" = {
"b" = "1";
"c" = {
"d" = "1";
};
};
};
expr = shblib.parseXML ''
<a>
<b>1</b>
<c><d>1</d></c>
</a>
'';
};
2024-03-01 00:34:53 +01:00
}