2024-04-04 14:36:07 +00:00
|
|
|
/**
|
|
|
|
Nix evaluation tests for various lib functions.
|
2023-07-18 15:20:28 +00:00
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
Since these tests are implemented with Nix evaluation,
|
|
|
|
error checking is limited to what `builtins.tryEval` can detect,
|
|
|
|
which is `throw`'s and `abort`'s, without error messages.
|
2023-07-18 15:20:28 +00:00
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
If you need to test error messages or more complex evaluations, see
|
|
|
|
`lib/tests/modules.sh`, `lib/tests/sources.sh` or `lib/tests/filesystem.sh` as examples.
|
2023-07-18 15:20:28 +00:00
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
To run these tests:
|
2023-07-18 15:20:28 +00:00
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
[nixpkgs]$ nix-instantiate --eval --strict lib/tests/misc.nix
|
2023-07-18 15:20:28 +00:00
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
If the resulting list is empty, all tests passed.
|
|
|
|
Alternatively, to run all `lib` tests:
|
|
|
|
|
|
|
|
[nixpkgs]$ nix-build lib/tests/release.nix
|
2023-07-18 15:20:28 +00:00
|
|
|
*/
|
2009-03-06 23:21:17 +00:00
|
|
|
|
2020-01-27 22:03:38 +00:00
|
|
|
let
|
2024-03-11 23:30:31 +00:00
|
|
|
lib = import ../default.nix;
|
|
|
|
|
|
|
|
inherit (lib)
|
|
|
|
allUnique
|
|
|
|
and
|
|
|
|
attrNames
|
|
|
|
attrsets
|
|
|
|
attrsToList
|
|
|
|
bitAnd
|
|
|
|
bitOr
|
|
|
|
bitXor
|
|
|
|
boolToString
|
|
|
|
callPackagesWith
|
|
|
|
callPackageWith
|
2024-04-15 17:13:22 +00:00
|
|
|
cartesianProduct
|
2024-03-11 23:30:31 +00:00
|
|
|
cli
|
|
|
|
composeExtensions
|
|
|
|
composeManyExtensions
|
|
|
|
concatLines
|
|
|
|
concatMapAttrs
|
2024-07-25 19:00:09 +00:00
|
|
|
concatMapAttrsStringSep
|
2024-03-11 23:30:31 +00:00
|
|
|
concatMapStrings
|
|
|
|
concatStrings
|
|
|
|
concatStringsSep
|
|
|
|
const
|
|
|
|
escapeXML
|
|
|
|
evalModules
|
2024-07-04 20:37:01 +00:00
|
|
|
extends
|
2024-03-11 23:30:31 +00:00
|
|
|
filter
|
2024-09-30 16:05:50 +00:00
|
|
|
filterAttrs
|
2024-03-11 23:30:31 +00:00
|
|
|
fix
|
|
|
|
fold
|
|
|
|
foldAttrs
|
|
|
|
foldl
|
|
|
|
foldl'
|
|
|
|
foldlAttrs
|
|
|
|
foldr
|
|
|
|
functionArgs
|
|
|
|
generators
|
|
|
|
genList
|
|
|
|
getExe
|
|
|
|
getExe'
|
2024-06-26 00:42:44 +00:00
|
|
|
getLicenseFromSpdxIdOr
|
2024-03-11 23:30:31 +00:00
|
|
|
groupBy
|
|
|
|
groupBy'
|
|
|
|
hasAttrByPath
|
|
|
|
hasInfix
|
|
|
|
id
|
2023-02-01 17:27:23 +00:00
|
|
|
ifilter0
|
2024-03-11 23:30:31 +00:00
|
|
|
isStorePath
|
|
|
|
lazyDerivation
|
2023-02-01 17:27:23 +00:00
|
|
|
length
|
2024-03-11 23:30:31 +00:00
|
|
|
lists
|
|
|
|
listToAttrs
|
|
|
|
makeExtensible
|
2024-04-02 17:01:05 +00:00
|
|
|
makeIncludePath
|
2024-03-11 23:30:31 +00:00
|
|
|
makeOverridable
|
|
|
|
mapAttrs
|
2024-04-15 17:16:15 +00:00
|
|
|
mapCartesianProduct
|
2024-03-11 23:30:31 +00:00
|
|
|
matchAttrs
|
|
|
|
mergeAttrs
|
|
|
|
meta
|
|
|
|
mod
|
|
|
|
nameValuePair
|
|
|
|
optionalDrvAttr
|
|
|
|
optionAttrSetToDocList
|
|
|
|
overrideExisting
|
|
|
|
packagesFromDirectoryRecursive
|
|
|
|
pipe
|
|
|
|
range
|
|
|
|
recursiveUpdateUntil
|
|
|
|
removePrefix
|
|
|
|
replicate
|
|
|
|
runTests
|
|
|
|
setFunctionArgs
|
|
|
|
showAttrPath
|
|
|
|
sort
|
|
|
|
sortOn
|
|
|
|
stringLength
|
|
|
|
strings
|
|
|
|
stringToCharacters
|
|
|
|
systems
|
|
|
|
tail
|
|
|
|
take
|
|
|
|
testAllTrue
|
|
|
|
toBaseDigits
|
2024-07-04 20:37:01 +00:00
|
|
|
toExtension
|
2024-03-11 23:30:31 +00:00
|
|
|
toHexString
|
2023-11-10 17:40:00 +00:00
|
|
|
fromHexString
|
2024-03-11 23:30:31 +00:00
|
|
|
toInt
|
|
|
|
toIntBase10
|
|
|
|
toShellVars
|
|
|
|
types
|
|
|
|
updateManyAttrsByPath
|
|
|
|
versions
|
2024-04-04 14:05:18 +00:00
|
|
|
xor
|
2024-03-11 23:30:31 +00:00
|
|
|
;
|
|
|
|
|
2023-04-29 16:35:07 +00:00
|
|
|
testingThrow = expr: {
|
|
|
|
expr = (builtins.tryEval (builtins.seq expr "didn't throw"));
|
|
|
|
expected = { success = false; value = false; };
|
|
|
|
};
|
2023-09-10 15:56:29 +00:00
|
|
|
testingEval = expr: {
|
|
|
|
expr = (builtins.tryEval expr).success;
|
|
|
|
expected = true;
|
|
|
|
};
|
2020-01-27 22:03:38 +00:00
|
|
|
|
|
|
|
testSanitizeDerivationName = { name, expected }:
|
|
|
|
let
|
|
|
|
drv = derivation {
|
|
|
|
name = strings.sanitizeDerivationName name;
|
|
|
|
builder = "x";
|
|
|
|
system = "x";
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
# Evaluate the derivation so an invalid name would be caught
|
|
|
|
expr = builtins.seq drv.drvPath drv.name;
|
|
|
|
inherit expected;
|
|
|
|
};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
2009-03-31 13:03:50 +00:00
|
|
|
runTests {
|
2009-03-06 23:21:17 +00:00
|
|
|
|
2023-10-11 20:56:53 +00:00
|
|
|
# CUSTOMIZATION
|
|
|
|
|
|
|
|
testFunctionArgsMakeOverridable = {
|
|
|
|
expr = functionArgs (makeOverridable ({ a, b, c ? null}: {}));
|
|
|
|
expected = { a = false; b = false; c = true; };
|
|
|
|
};
|
|
|
|
|
|
|
|
testFunctionArgsMakeOverridableOverride = {
|
|
|
|
expr = functionArgs (makeOverridable ({ a, b, c ? null }: {}) { a = 1; b = 2; }).override;
|
|
|
|
expected = { a = false; b = false; c = true; };
|
|
|
|
};
|
|
|
|
|
2022-06-17 05:42:04 +00:00
|
|
|
testCallPackageWithOverridePreservesArguments =
|
|
|
|
let
|
|
|
|
f = { a ? 0, b }: {};
|
|
|
|
f' = callPackageWith { a = 1; b = 2; } f {};
|
|
|
|
in {
|
|
|
|
expr = functionArgs f'.override;
|
|
|
|
expected = functionArgs f;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCallPackagesWithOverridePreservesArguments =
|
|
|
|
let
|
|
|
|
f = { a ? 0, b }: { nested = {}; };
|
|
|
|
f' = callPackagesWith { a = 1; b = 2; } f {};
|
|
|
|
in {
|
|
|
|
expr = functionArgs f'.nested.override;
|
|
|
|
expected = functionArgs f;
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
# TRIVIAL
|
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testId = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = id 1;
|
|
|
|
expected = 1;
|
|
|
|
};
|
2015-11-24 09:00:44 +00:00
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testConst = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = const 2 3;
|
|
|
|
expected = 2;
|
|
|
|
};
|
2012-05-13 19:10:57 +00:00
|
|
|
|
2019-10-18 15:57:33 +00:00
|
|
|
testPipe = {
|
|
|
|
expr = pipe 2 [
|
|
|
|
(x: x + 2) # 2 + 2 = 4
|
|
|
|
(x: x * 2) # 4 * 2 = 8
|
|
|
|
];
|
|
|
|
expected = 8;
|
|
|
|
};
|
|
|
|
|
|
|
|
testPipeEmpty = {
|
|
|
|
expr = pipe 2 [];
|
|
|
|
expected = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
testPipeStrings = {
|
|
|
|
expr = pipe [ 3 4 ] [
|
|
|
|
(map toString)
|
|
|
|
(map (s: s + "\n"))
|
|
|
|
concatStrings
|
|
|
|
];
|
|
|
|
expected = ''
|
|
|
|
3
|
|
|
|
4
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-05-13 19:10:57 +00:00
|
|
|
/*
|
2024-04-04 14:36:07 +00:00
|
|
|
testOr = {
|
|
|
|
expr = or true false;
|
|
|
|
expected = true;
|
|
|
|
};
|
2012-05-13 19:10:57 +00:00
|
|
|
*/
|
2015-11-24 09:00:44 +00:00
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testAnd = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = and true false;
|
|
|
|
expected = false;
|
|
|
|
};
|
2015-11-24 09:00:44 +00:00
|
|
|
|
2024-04-04 14:05:18 +00:00
|
|
|
testXor = {
|
|
|
|
expr = [
|
|
|
|
(xor true false)
|
|
|
|
(xor true true)
|
|
|
|
(xor false false)
|
|
|
|
(xor false true)
|
|
|
|
];
|
|
|
|
expected = [
|
|
|
|
true
|
|
|
|
false
|
|
|
|
false
|
|
|
|
true
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
testComposeExtensions = {
|
|
|
|
expr = let obj = makeExtensible (self: { foo = self.bar; });
|
|
|
|
f = self: super: { bar = false; baz = true; };
|
|
|
|
g = self: super: { bar = super.baz or false; };
|
|
|
|
f_o_g = composeExtensions f g;
|
|
|
|
composed = obj.extend f_o_g;
|
|
|
|
in composed.foo;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
2020-11-11 02:36:19 +00:00
|
|
|
testComposeManyExtensions0 = {
|
|
|
|
expr = let obj = makeExtensible (self: { foo = true; });
|
|
|
|
emptyComposition = composeManyExtensions [];
|
|
|
|
composed = obj.extend emptyComposition;
|
|
|
|
in composed.foo;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testComposeManyExtensions =
|
|
|
|
let f = self: super: { bar = false; baz = true; };
|
|
|
|
g = self: super: { bar = super.baz or false; };
|
|
|
|
h = self: super: { qux = super.bar or false; };
|
|
|
|
obj = makeExtensible (self: { foo = self.qux; });
|
|
|
|
in {
|
|
|
|
expr = let composition = composeManyExtensions [f g h];
|
|
|
|
composed = obj.extend composition;
|
|
|
|
in composed.foo;
|
|
|
|
expected = (obj.extend (composeExtensions f (composeExtensions g h))).foo;
|
|
|
|
};
|
|
|
|
|
2018-06-10 19:25:48 +00:00
|
|
|
testBitAnd = {
|
|
|
|
expr = (bitAnd 3 10);
|
|
|
|
expected = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
testBitOr = {
|
|
|
|
expr = (bitOr 3 10);
|
|
|
|
expected = 11;
|
|
|
|
};
|
|
|
|
|
|
|
|
testBitXor = {
|
|
|
|
expr = (bitXor 3 10);
|
|
|
|
expected = 9;
|
|
|
|
};
|
|
|
|
|
2020-07-20 11:14:19 +00:00
|
|
|
testToHexString = {
|
|
|
|
expr = toHexString 250;
|
2020-04-20 10:00:23 +00:00
|
|
|
expected = "FA";
|
|
|
|
};
|
|
|
|
|
2023-11-10 17:40:00 +00:00
|
|
|
testFromHexStringFirstExample = {
|
|
|
|
expr = fromHexString "FF";
|
|
|
|
expected = 255;
|
|
|
|
};
|
|
|
|
|
|
|
|
testFromHexStringSecondExample = {
|
|
|
|
expr = fromHexString (builtins.hashString "sha256" "test");
|
|
|
|
expected = 9223372036854775807;
|
|
|
|
};
|
|
|
|
|
|
|
|
testFromHexStringWithPrefix = {
|
|
|
|
expr = fromHexString "0Xf";
|
|
|
|
expected = 15;
|
|
|
|
};
|
|
|
|
|
2020-07-20 11:14:19 +00:00
|
|
|
testToBaseDigits = {
|
|
|
|
expr = toBaseDigits 2 6;
|
2020-04-20 10:00:23 +00:00
|
|
|
expected = [ 1 1 0 ];
|
|
|
|
};
|
|
|
|
|
2021-07-23 19:10:31 +00:00
|
|
|
testFunctionArgsFunctor = {
|
|
|
|
expr = functionArgs { __functor = self: { a, b }: null; };
|
|
|
|
expected = { a = false; b = false; };
|
|
|
|
};
|
|
|
|
|
|
|
|
testFunctionArgsSetFunctionArgs = {
|
|
|
|
expr = functionArgs (setFunctionArgs (args: args.x) { x = false; });
|
|
|
|
expected = { x = false; };
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
# STRINGS
|
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testConcatMapStrings = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = concatMapStrings (x: x + ";") ["a" "b" "c"];
|
|
|
|
expected = "a;b;c;";
|
|
|
|
};
|
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testConcatStringsSep = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = concatStringsSep "," ["a" "b" "c"];
|
|
|
|
expected = "a,b,c";
|
|
|
|
};
|
|
|
|
|
2024-07-25 19:00:09 +00:00
|
|
|
testConcatMapAttrsStringSepExamples = {
|
|
|
|
expr = concatMapAttrsStringSep "\n" (name: value: "${name}: foo-${value}") { a = "0.1.0"; b = "0.2.0"; };
|
|
|
|
expected = "a: foo-0.1.0\nb: foo-0.2.0";
|
|
|
|
};
|
|
|
|
|
2022-12-10 14:56:30 +00:00
|
|
|
testConcatLines = {
|
|
|
|
expr = concatLines ["a" "b" "c"];
|
|
|
|
expected = "a\nb\nc\n";
|
|
|
|
};
|
|
|
|
|
2024-04-02 17:01:05 +00:00
|
|
|
testMakeIncludePathWithPkgs = {
|
|
|
|
expr = (makeIncludePath [
|
|
|
|
# makeIncludePath preferably selects the "dev" output
|
|
|
|
{ dev.outPath = "/dev"; out.outPath = "/out"; outPath = "/default"; }
|
|
|
|
# "out" is used if "dev" is not found
|
|
|
|
{ out.outPath = "/out"; outPath = "/default"; }
|
|
|
|
# And it returns the derivation directly if there's no "out" either
|
|
|
|
{ outPath = "/default"; }
|
|
|
|
# Same if the output is specified explicitly, even if there's a "dev"
|
|
|
|
{ dev.outPath = "/dev"; outPath = "/default"; outputSpecified = true; }
|
|
|
|
]);
|
|
|
|
expected = "/dev/include:/out/include:/default/include:/default/include";
|
|
|
|
};
|
|
|
|
|
|
|
|
testMakeIncludePathWithEmptyList = {
|
|
|
|
expr = (makeIncludePath [ ]);
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
testMakeIncludePathWithOneString = {
|
|
|
|
expr = (makeIncludePath [ "/usr" ]);
|
|
|
|
expected = "/usr/include";
|
|
|
|
};
|
|
|
|
|
|
|
|
testMakeIncludePathWithManyString = {
|
|
|
|
expr = (makeIncludePath [ "/usr" "/usr/local" ]);
|
|
|
|
expected = "/usr/include:/usr/local/include";
|
|
|
|
};
|
|
|
|
|
2023-10-16 23:12:49 +00:00
|
|
|
testReplicateString = {
|
|
|
|
expr = strings.replicate 5 "hello";
|
|
|
|
expected = "hellohellohellohellohello";
|
|
|
|
};
|
|
|
|
|
2024-05-28 17:22:43 +00:00
|
|
|
# Test various strings are trimmed correctly
|
|
|
|
testTrimString = {
|
|
|
|
expr =
|
|
|
|
let
|
|
|
|
testValues = f: mapAttrs (_: f) {
|
|
|
|
empty = "";
|
|
|
|
cr = "\r";
|
|
|
|
lf = "\n";
|
|
|
|
tab = "\t";
|
|
|
|
spaces = " ";
|
|
|
|
leading = " Hello, world";
|
|
|
|
trailing = "Hello, world ";
|
|
|
|
mixed = " Hello, world ";
|
|
|
|
mixed-tabs = " \t\tHello, world \t \t ";
|
|
|
|
multiline = " Hello,\n world! ";
|
|
|
|
multiline-crlf = " Hello,\r\n world! ";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
leading = testValues (strings.trimWith { start = true; });
|
|
|
|
trailing = testValues (strings.trimWith { end = true; });
|
|
|
|
both = testValues strings.trim;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
leading = {
|
|
|
|
empty = "";
|
|
|
|
cr = "";
|
|
|
|
lf = "";
|
|
|
|
tab = "";
|
|
|
|
spaces = "";
|
|
|
|
leading = "Hello, world";
|
|
|
|
trailing = "Hello, world ";
|
|
|
|
mixed = "Hello, world ";
|
|
|
|
mixed-tabs = "Hello, world \t \t ";
|
|
|
|
multiline = "Hello,\n world! ";
|
|
|
|
multiline-crlf = "Hello,\r\n world! ";
|
|
|
|
};
|
|
|
|
trailing = {
|
|
|
|
empty = "";
|
|
|
|
cr = "";
|
|
|
|
lf = "";
|
|
|
|
tab = "";
|
|
|
|
spaces = "";
|
|
|
|
leading = " Hello, world";
|
|
|
|
trailing = "Hello, world";
|
|
|
|
mixed = " Hello, world";
|
|
|
|
mixed-tabs = " \t\tHello, world";
|
|
|
|
multiline = " Hello,\n world!";
|
|
|
|
multiline-crlf = " Hello,\r\n world!";
|
|
|
|
};
|
|
|
|
both = {
|
|
|
|
empty = "";
|
|
|
|
cr = "";
|
|
|
|
lf = "";
|
|
|
|
tab = "";
|
|
|
|
spaces = "";
|
|
|
|
leading = "Hello, world";
|
|
|
|
trailing = "Hello, world";
|
|
|
|
mixed = "Hello, world";
|
|
|
|
mixed-tabs = "Hello, world";
|
|
|
|
multiline = "Hello,\n world!";
|
|
|
|
multiline-crlf = "Hello,\r\n world!";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
testSplitStringsSimple = {
|
|
|
|
expr = strings.splitString "." "a.b.c.d";
|
|
|
|
expected = [ "a" "b" "c" "d" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsEmpty = {
|
|
|
|
expr = strings.splitString "." "a..b";
|
|
|
|
expected = [ "a" "" "b" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsOne = {
|
|
|
|
expr = strings.splitString ":" "a.b";
|
|
|
|
expected = [ "a.b" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsNone = {
|
|
|
|
expr = strings.splitString "." "";
|
|
|
|
expected = [ "" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsFirstEmpty = {
|
|
|
|
expr = strings.splitString "/" "/a/b/c";
|
|
|
|
expected = [ "" "a" "b" "c" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsLastEmpty = {
|
|
|
|
expr = strings.splitString ":" "2001:db8:0:0042::8a2e:370:";
|
|
|
|
expected = [ "2001" "db8" "0" "0042" "" "8a2e" "370" "" ];
|
|
|
|
};
|
2019-09-24 09:27:14 +00:00
|
|
|
|
2020-10-18 10:15:40 +00:00
|
|
|
testSplitStringsRegex = {
|
|
|
|
expr = strings.splitString "\\[{}]()^$?*+|." "A\\[{}]()^$?*+|.B";
|
|
|
|
expected = [ "A" "B" ];
|
|
|
|
};
|
|
|
|
|
2024-08-16 14:26:08 +00:00
|
|
|
testEscapeShellArg = {
|
|
|
|
expr = strings.escapeShellArg "esc'ape\nme";
|
|
|
|
expected = "'esc'\\''ape\nme'";
|
|
|
|
};
|
|
|
|
|
|
|
|
testEscapeShellArgEmpty = {
|
|
|
|
expr = strings.escapeShellArg "";
|
|
|
|
expected = "''";
|
|
|
|
};
|
|
|
|
|
|
|
|
testEscapeShellArgs = {
|
|
|
|
expr = strings.escapeShellArgs ["one" "two three" "four'five"];
|
|
|
|
expected = "one 'two three' 'four'\\''five'";
|
|
|
|
};
|
|
|
|
|
|
|
|
testEscapeShellArgsUnicode = {
|
|
|
|
expr = strings.escapeShellArg "á";
|
|
|
|
expected = "'á'";
|
|
|
|
};
|
|
|
|
|
2020-10-18 10:15:40 +00:00
|
|
|
testSplitStringsDerivation = {
|
|
|
|
expr = take 3 (strings.splitString "/" (derivation {
|
|
|
|
name = "name";
|
|
|
|
builder = "builder";
|
|
|
|
system = "system";
|
|
|
|
}));
|
|
|
|
expected = ["" "nix" "store"];
|
|
|
|
};
|
|
|
|
|
2019-09-24 09:27:14 +00:00
|
|
|
testSplitVersionSingle = {
|
|
|
|
expr = versions.splitVersion "1";
|
|
|
|
expected = [ "1" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitVersionDouble = {
|
|
|
|
expr = versions.splitVersion "1.2";
|
|
|
|
expected = [ "1" "2" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitVersionTriple = {
|
|
|
|
expr = versions.splitVersion "1.2.3";
|
|
|
|
expected = [ "1" "2" "3" ];
|
|
|
|
};
|
2017-04-11 22:43:52 +00:00
|
|
|
|
2022-12-20 17:38:47 +00:00
|
|
|
testPadVersionLess = {
|
|
|
|
expr = versions.pad 3 "1.2";
|
|
|
|
expected = "1.2.0";
|
|
|
|
};
|
|
|
|
|
|
|
|
testPadVersionLessExtra = {
|
|
|
|
expr = versions.pad 3 "1.3-rc1";
|
|
|
|
expected = "1.3.0-rc1";
|
|
|
|
};
|
|
|
|
|
|
|
|
testPadVersionMore = {
|
|
|
|
expr = versions.pad 3 "1.2.3.4";
|
|
|
|
expected = "1.2.3";
|
|
|
|
};
|
|
|
|
|
2017-05-30 19:48:32 +00:00
|
|
|
testIsStorePath = {
|
|
|
|
expr =
|
|
|
|
let goodPath =
|
2017-06-09 21:30:10 +00:00
|
|
|
"${builtins.storeDir}/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11";
|
2017-05-30 19:48:32 +00:00
|
|
|
in {
|
|
|
|
storePath = isStorePath goodPath;
|
2020-02-10 15:20:41 +00:00
|
|
|
storePathDerivation = isStorePath (import ../.. { system = "x86_64-linux"; }).hello;
|
2017-05-30 19:48:32 +00:00
|
|
|
storePathAppendix = isStorePath
|
|
|
|
"${goodPath}/bin/python";
|
|
|
|
nonAbsolute = isStorePath (concatStrings (tail (stringToCharacters goodPath)));
|
2018-10-20 11:23:58 +00:00
|
|
|
asPath = isStorePath (/. + goodPath);
|
2017-05-30 19:48:32 +00:00
|
|
|
otherPath = isStorePath "/something/else";
|
|
|
|
otherVals = {
|
|
|
|
attrset = isStorePath {};
|
|
|
|
list = isStorePath [];
|
|
|
|
int = isStorePath 42;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
storePath = true;
|
2018-03-09 22:21:12 +00:00
|
|
|
storePathDerivation = true;
|
2017-05-30 19:48:32 +00:00
|
|
|
storePathAppendix = false;
|
|
|
|
nonAbsolute = false;
|
|
|
|
asPath = true;
|
|
|
|
otherPath = false;
|
|
|
|
otherVals = {
|
|
|
|
attrset = false;
|
|
|
|
list = false;
|
|
|
|
int = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-03 09:28:03 +00:00
|
|
|
testEscapeXML = {
|
|
|
|
expr = escapeXML ''"test" 'test' < & >'';
|
|
|
|
expected = ""test" 'test' < & >";
|
|
|
|
};
|
|
|
|
|
2022-04-27 08:02:49 +00:00
|
|
|
testToShellVars = {
|
|
|
|
expr = ''
|
|
|
|
${toShellVars {
|
|
|
|
STRing01 = "just a 'string'";
|
|
|
|
_array_ = [ "with" "more strings" ];
|
|
|
|
assoc."with some" = ''
|
|
|
|
strings
|
|
|
|
possibly newlines
|
|
|
|
'';
|
2022-05-07 11:37:03 +00:00
|
|
|
drv = {
|
|
|
|
outPath = "/drv";
|
|
|
|
foo = "ignored attribute";
|
|
|
|
};
|
|
|
|
path = /path;
|
|
|
|
stringable = {
|
|
|
|
__toString = _: "hello toString";
|
|
|
|
bar = "ignored attribute";
|
|
|
|
};
|
2022-04-27 08:02:49 +00:00
|
|
|
}}
|
|
|
|
'';
|
|
|
|
expected = ''
|
|
|
|
STRing01='just a '\'''string'\''''
|
2024-08-16 14:26:08 +00:00
|
|
|
declare -a _array_=(with 'more strings')
|
2022-04-27 08:02:49 +00:00
|
|
|
declare -A assoc=(['with some']='strings
|
|
|
|
possibly newlines
|
|
|
|
')
|
2024-08-16 14:26:08 +00:00
|
|
|
drv=/drv
|
|
|
|
path=/path
|
2022-05-07 11:37:03 +00:00
|
|
|
stringable='hello toString'
|
2022-04-27 08:02:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-04-27 06:58:20 +00:00
|
|
|
testHasInfixFalse = {
|
|
|
|
expr = hasInfix "c" "abde";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasInfixTrue = {
|
|
|
|
expr = hasInfix "c" "abcde";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasInfixDerivation = {
|
|
|
|
expr = hasInfix "hello" (import ../.. { system = "x86_64-linux"; }).hello;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasInfixPath = {
|
|
|
|
expr = hasInfix "tests" ./.;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasInfixPathStoreDir = {
|
|
|
|
expr = hasInfix builtins.storeDir ./.;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasInfixToString = {
|
|
|
|
expr = hasInfix "a" { __toString = _: "a"; };
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
2023-08-14 21:29:00 +00:00
|
|
|
testRemovePrefixExample1 = {
|
|
|
|
expr = removePrefix "foo." "foo.bar.baz";
|
|
|
|
expected = "bar.baz";
|
|
|
|
};
|
|
|
|
testRemovePrefixExample2 = {
|
|
|
|
expr = removePrefix "xxx" "foo.bar.baz";
|
|
|
|
expected = "foo.bar.baz";
|
|
|
|
};
|
|
|
|
testRemovePrefixEmptyPrefix = {
|
|
|
|
expr = removePrefix "" "foo";
|
|
|
|
expected = "foo";
|
|
|
|
};
|
|
|
|
testRemovePrefixEmptyString = {
|
|
|
|
expr = removePrefix "foo" "";
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
testRemovePrefixEmptyBoth = {
|
|
|
|
expr = removePrefix "" "";
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
|
2022-06-23 20:46:23 +00:00
|
|
|
testNormalizePath = {
|
|
|
|
expr = strings.normalizePath "//a/b//c////d/";
|
|
|
|
expected = "/a/b/c/d/";
|
|
|
|
};
|
|
|
|
|
|
|
|
testCharToInt = {
|
|
|
|
expr = strings.charToInt "A";
|
|
|
|
expected = 65;
|
|
|
|
};
|
|
|
|
|
|
|
|
testEscapeC = {
|
|
|
|
expr = strings.escapeC [ " " ] "Hello World";
|
|
|
|
expected = "Hello\\x20World";
|
|
|
|
};
|
|
|
|
|
2023-03-02 20:41:51 +00:00
|
|
|
testEscapeURL = testAllTrue [
|
|
|
|
("" == strings.escapeURL "")
|
|
|
|
("Hello" == strings.escapeURL "Hello")
|
|
|
|
("Hello%20World" == strings.escapeURL "Hello World")
|
|
|
|
("Hello%2FWorld" == strings.escapeURL "Hello/World")
|
|
|
|
("42%25" == strings.escapeURL "42%")
|
|
|
|
("%20%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%09%3A%2F%40%24%27%28%29%2A%2C%3B" == strings.escapeURL " ?&=#+%!<>#\"{}|\\^[]`\t:/@$'()*,;")
|
|
|
|
];
|
|
|
|
|
2022-05-22 02:34:11 +00:00
|
|
|
testToInt = testAllTrue [
|
|
|
|
# Naive
|
|
|
|
(123 == toInt "123")
|
|
|
|
(0 == toInt "0")
|
|
|
|
# Whitespace Padding
|
|
|
|
(123 == toInt " 123")
|
|
|
|
(123 == toInt "123 ")
|
|
|
|
(123 == toInt " 123 ")
|
|
|
|
(123 == toInt " 123 ")
|
|
|
|
(0 == toInt " 0")
|
|
|
|
(0 == toInt "0 ")
|
|
|
|
(0 == toInt " 0 ")
|
2022-12-10 12:02:40 +00:00
|
|
|
(-1 == toInt "-1")
|
|
|
|
(-1 == toInt " -1 ")
|
2022-05-22 02:34:11 +00:00
|
|
|
];
|
|
|
|
|
2022-05-23 07:39:48 +00:00
|
|
|
testToIntFails = testAllTrue [
|
|
|
|
( builtins.tryEval (toInt "") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt "123 123") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt "0 123") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt " 0d ") == { success = false; value = false; } )
|
2022-07-10 00:12:31 +00:00
|
|
|
( builtins.tryEval (toInt " 1d ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt " d0 ") == { success = false; value = false; } )
|
2022-07-08 23:37:45 +00:00
|
|
|
( builtins.tryEval (toInt "00") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt "01") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt "002") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt " 002 ") == { success = false; value = false; } )
|
2022-05-23 07:39:48 +00:00
|
|
|
( builtins.tryEval (toInt " foo ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toInt " foo 123 ") == { success = false; value = false; } )
|
2022-07-08 23:37:45 +00:00
|
|
|
( builtins.tryEval (toInt " foo123 ") == { success = false; value = false; } )
|
|
|
|
];
|
|
|
|
|
|
|
|
testToIntBase10 = testAllTrue [
|
|
|
|
# Naive
|
|
|
|
(123 == toIntBase10 "123")
|
|
|
|
(0 == toIntBase10 "0")
|
|
|
|
# Whitespace Padding
|
|
|
|
(123 == toIntBase10 " 123")
|
|
|
|
(123 == toIntBase10 "123 ")
|
|
|
|
(123 == toIntBase10 " 123 ")
|
|
|
|
(123 == toIntBase10 " 123 ")
|
|
|
|
(0 == toIntBase10 " 0")
|
|
|
|
(0 == toIntBase10 "0 ")
|
|
|
|
(0 == toIntBase10 " 0 ")
|
|
|
|
# Zero Padding
|
|
|
|
(123 == toIntBase10 "0123")
|
|
|
|
(123 == toIntBase10 "0000123")
|
|
|
|
(0 == toIntBase10 "000000")
|
|
|
|
# Whitespace and Zero Padding
|
|
|
|
(123 == toIntBase10 " 0123")
|
|
|
|
(123 == toIntBase10 "0123 ")
|
|
|
|
(123 == toIntBase10 " 0123 ")
|
|
|
|
(123 == toIntBase10 " 0000123")
|
|
|
|
(123 == toIntBase10 "0000123 ")
|
|
|
|
(123 == toIntBase10 " 0000123 ")
|
|
|
|
(0 == toIntBase10 " 000000")
|
|
|
|
(0 == toIntBase10 "000000 ")
|
|
|
|
(0 == toIntBase10 " 000000 ")
|
2022-12-10 12:02:40 +00:00
|
|
|
(-1 == toIntBase10 "-1")
|
|
|
|
(-1 == toIntBase10 " -1 ")
|
2022-07-08 23:37:45 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
testToIntBase10Fails = testAllTrue [
|
|
|
|
( builtins.tryEval (toIntBase10 "") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 "123 123") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 "0 123") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 " 0d ") == { success = false; value = false; } )
|
2022-07-10 00:12:31 +00:00
|
|
|
( builtins.tryEval (toIntBase10 " 1d ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 " d0 ") == { success = false; value = false; } )
|
2022-07-08 23:37:45 +00:00
|
|
|
( builtins.tryEval (toIntBase10 " foo ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 " foo 123 ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 " foo 00123 ") == { success = false; value = false; } )
|
|
|
|
( builtins.tryEval (toIntBase10 " foo00123 ") == { success = false; value = false; } )
|
2022-05-23 07:39:48 +00:00
|
|
|
];
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
# LISTS
|
|
|
|
|
2009-11-22 21:28:41 +00:00
|
|
|
testFilter = {
|
2009-03-31 13:03:50 +00:00
|
|
|
expr = filter (x: x != "a") ["a" "b" "c" "a"];
|
|
|
|
expected = ["b" "c"];
|
|
|
|
};
|
|
|
|
|
2023-02-01 17:27:23 +00:00
|
|
|
testIfilter0Example = {
|
|
|
|
expr = ifilter0 (i: v: i == 0 || v > 2) [ 1 2 3 ];
|
|
|
|
expected = [ 1 3 ];
|
|
|
|
};
|
|
|
|
testIfilter0Empty = {
|
|
|
|
expr = ifilter0 (i: v: abort "shouldn't be evaluated!") [ ];
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
testIfilter0IndexOnly = {
|
|
|
|
expr = length (ifilter0 (i: v: mod i 2 == 0) [ (throw "0") (throw "1") (throw "2") (throw "3")]);
|
|
|
|
expected = 2;
|
|
|
|
};
|
|
|
|
testIfilter0All = {
|
|
|
|
expr = ifilter0 (i: v: true) [ 10 11 12 13 14 15 ];
|
|
|
|
expected = [ 10 11 12 13 14 15 ];
|
|
|
|
};
|
|
|
|
testIfilter0First = {
|
|
|
|
expr = ifilter0 (i: v: i == 0) [ 10 11 12 13 14 15 ];
|
|
|
|
expected = [ 10 ];
|
|
|
|
};
|
|
|
|
testIfilter0Last = {
|
|
|
|
expr = ifilter0 (i: v: i == 5) [ 10 11 12 13 14 15 ];
|
|
|
|
expected = [ 15 ];
|
|
|
|
};
|
|
|
|
|
2017-03-18 19:41:02 +00:00
|
|
|
testFold =
|
|
|
|
let
|
|
|
|
f = op: fold: fold op 0 (range 0 100);
|
|
|
|
# fold with associative operator
|
|
|
|
assoc = f builtins.add;
|
|
|
|
# fold with non-associative operator
|
|
|
|
nonAssoc = f builtins.sub;
|
|
|
|
in {
|
|
|
|
expr = {
|
|
|
|
assocRight = assoc foldr;
|
|
|
|
# right fold with assoc operator is same as left fold
|
|
|
|
assocRightIsLeft = assoc foldr == assoc foldl;
|
|
|
|
nonAssocRight = nonAssoc foldr;
|
|
|
|
nonAssocLeft = nonAssoc foldl;
|
|
|
|
# with non-assoc operator the fold results are not the same
|
|
|
|
nonAssocRightIsNotLeft = nonAssoc foldl != nonAssoc foldr;
|
|
|
|
# fold is an alias for foldr
|
|
|
|
foldIsRight = nonAssoc fold == nonAssoc foldr;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
assocRight = 5050;
|
|
|
|
assocRightIsLeft = true;
|
|
|
|
nonAssocRight = 50;
|
|
|
|
nonAssocLeft = (-5050);
|
|
|
|
nonAssocRightIsNotLeft = true;
|
|
|
|
foldIsRight = true;
|
|
|
|
};
|
|
|
|
};
|
2009-03-31 13:03:50 +00:00
|
|
|
|
2023-09-22 00:38:15 +00:00
|
|
|
testFoldl'Empty = {
|
|
|
|
expr = foldl' (acc: el: abort "operation not called") 0 [ ];
|
|
|
|
expected = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
testFoldl'IntegerAdding = {
|
|
|
|
expr = foldl' (acc: el: acc + el) 0 [ 1 2 3 ];
|
|
|
|
expected = 6;
|
|
|
|
};
|
|
|
|
|
|
|
|
# The accumulator isn't forced deeply
|
|
|
|
testFoldl'NonDeep = {
|
|
|
|
expr = take 3 (foldl'
|
|
|
|
(acc: el: [ el ] ++ acc)
|
|
|
|
[ (abort "unevaluated list entry") ]
|
|
|
|
[ 1 2 3 ]);
|
|
|
|
expected = [ 3 2 1 ];
|
|
|
|
};
|
|
|
|
|
2023-09-22 10:22:04 +00:00
|
|
|
# Compared to builtins.foldl', lib.foldl' evaluates the first accumulator strictly too
|
2023-09-22 00:38:15 +00:00
|
|
|
testFoldl'StrictInitial = {
|
|
|
|
expr = (builtins.tryEval (foldl' (acc: el: el) (throw "hello") [])).success;
|
2023-09-22 10:22:04 +00:00
|
|
|
expected = false;
|
2023-09-22 00:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Make sure we don't get a stack overflow for large lists
|
|
|
|
# This number of elements would notably cause a stack overflow if it was implemented without the `foldl'` builtin
|
|
|
|
testFoldl'Large = {
|
|
|
|
expr = foldl' (acc: el: acc + el) 0 (range 0 100000);
|
|
|
|
expected = 5000050000;
|
|
|
|
};
|
|
|
|
|
2009-12-08 21:47:14 +00:00
|
|
|
testTake = testAllTrue [
|
|
|
|
([] == (take 0 [ 1 2 3 ]))
|
|
|
|
([1] == (take 1 [ 1 2 3 ]))
|
|
|
|
([ 1 2 ] == (take 2 [ 1 2 3 ]))
|
|
|
|
([ 1 2 3 ] == (take 3 [ 1 2 3 ]))
|
|
|
|
([ 1 2 3 ] == (take 4 [ 1 2 3 ]))
|
|
|
|
];
|
|
|
|
|
2023-07-14 16:18:48 +00:00
|
|
|
testListHasPrefixExample1 = {
|
|
|
|
expr = lists.hasPrefix [ 1 2 ] [ 1 2 3 4 ];
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
testListHasPrefixExample2 = {
|
|
|
|
expr = lists.hasPrefix [ 0 1 ] [ 1 2 3 4 ];
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
testListHasPrefixLazy = {
|
|
|
|
expr = lists.hasPrefix [ 1 ] [ 1 (abort "lib.lists.hasPrefix is not lazy") ];
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
testListHasPrefixEmptyPrefix = {
|
|
|
|
expr = lists.hasPrefix [ ] [ 1 2 ];
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
testListHasPrefixEmptyList = {
|
|
|
|
expr = lists.hasPrefix [ 1 2 ] [ ];
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2023-07-14 16:28:38 +00:00
|
|
|
testListRemovePrefixExample1 = {
|
|
|
|
expr = lists.removePrefix [ 1 2 ] [ 1 2 3 4 ];
|
|
|
|
expected = [ 3 4 ];
|
|
|
|
};
|
|
|
|
testListRemovePrefixExample2 = {
|
|
|
|
expr = (builtins.tryEval (lists.removePrefix [ 0 1 ] [ 1 2 3 4 ])).success;
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
testListRemovePrefixEmptyPrefix = {
|
|
|
|
expr = lists.removePrefix [ ] [ 1 2 ];
|
|
|
|
expected = [ 1 2 ];
|
|
|
|
};
|
|
|
|
testListRemovePrefixEmptyList = {
|
|
|
|
expr = (builtins.tryEval (lists.removePrefix [ 1 2 ] [ ])).success;
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2012-08-28 12:40:24 +00:00
|
|
|
testFoldAttrs = {
|
|
|
|
expr = foldAttrs (n: a: [n] ++ a) [] [
|
|
|
|
{ a = 2; b = 7; }
|
|
|
|
{ a = 3; c = 8; }
|
|
|
|
];
|
|
|
|
expected = { a = [ 2 3 ]; b = [7]; c = [8];};
|
|
|
|
};
|
2009-12-08 21:47:14 +00:00
|
|
|
|
2023-07-14 17:26:08 +00:00
|
|
|
testListCommonPrefixExample1 = {
|
|
|
|
expr = lists.commonPrefix [ 1 2 3 4 5 6 ] [ 1 2 4 8 ];
|
|
|
|
expected = [ 1 2 ];
|
|
|
|
};
|
|
|
|
testListCommonPrefixExample2 = {
|
|
|
|
expr = lists.commonPrefix [ 1 2 3 ] [ 1 2 3 4 5 ];
|
|
|
|
expected = [ 1 2 3 ];
|
|
|
|
};
|
|
|
|
testListCommonPrefixExample3 = {
|
|
|
|
expr = lists.commonPrefix [ 1 2 3 ] [ 4 5 6 ];
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
testListCommonPrefixEmpty = {
|
|
|
|
expr = lists.commonPrefix [ ] [ 1 2 3 ];
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
testListCommonPrefixSame = {
|
|
|
|
expr = lists.commonPrefix [ 1 2 3 ] [ 1 2 3 ];
|
|
|
|
expected = [ 1 2 3 ];
|
|
|
|
};
|
|
|
|
testListCommonPrefixLazy = {
|
|
|
|
expr = lists.commonPrefix [ 1 ] [ 1 (abort "lib.lists.commonPrefix shouldn't evaluate this")];
|
|
|
|
expected = [ 1 ];
|
|
|
|
};
|
|
|
|
# This would stack overflow if `commonPrefix` were implemented using recursion
|
|
|
|
testListCommonPrefixLong =
|
|
|
|
let
|
|
|
|
longList = genList (n: n) 100000;
|
|
|
|
in {
|
|
|
|
expr = lists.commonPrefix longList longList;
|
|
|
|
expected = longList;
|
|
|
|
};
|
|
|
|
|
2012-11-20 12:54:38 +00:00
|
|
|
testSort = {
|
|
|
|
expr = sort builtins.lessThan [ 40 2 30 42 ];
|
|
|
|
expected = [2 30 40 42];
|
|
|
|
};
|
2015-11-24 09:00:44 +00:00
|
|
|
|
2023-12-07 18:51:07 +00:00
|
|
|
testSortOn = {
|
|
|
|
expr = sortOn stringLength [ "aa" "b" "cccc" ];
|
|
|
|
expected = [ "b" "aa" "cccc" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSortOnEmpty = {
|
|
|
|
expr = sortOn (throw "nope") [ ];
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSortOnIncomparable = {
|
|
|
|
expr =
|
|
|
|
map
|
|
|
|
(x: x.f x.ok)
|
|
|
|
(sortOn (x: x.ok) [
|
|
|
|
{ ok = 1; f = x: x; }
|
|
|
|
{ ok = 3; f = x: x + 3; }
|
|
|
|
{ ok = 2; f = x: x; }
|
|
|
|
]);
|
|
|
|
expected = [ 1 2 6 ];
|
|
|
|
};
|
|
|
|
|
2022-12-17 20:57:17 +00:00
|
|
|
testReplicate = {
|
|
|
|
expr = replicate 3 "a";
|
|
|
|
expected = ["a" "a" "a"];
|
|
|
|
};
|
|
|
|
|
2015-11-24 09:00:44 +00:00
|
|
|
testToIntShouldConvertStringToInt = {
|
|
|
|
expr = toInt "27";
|
|
|
|
expected = 27;
|
|
|
|
};
|
|
|
|
|
|
|
|
testToIntShouldThrowErrorIfItCouldNotConvertToInt = {
|
|
|
|
expr = builtins.tryEval (toInt "\"foo\"");
|
|
|
|
expected = { success = false; value = false; };
|
|
|
|
};
|
|
|
|
|
2015-12-04 15:17:45 +00:00
|
|
|
testHasAttrByPathTrue = {
|
|
|
|
expr = hasAttrByPath ["a" "b"] { a = { b = "yey"; }; };
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasAttrByPathFalse = {
|
|
|
|
expr = hasAttrByPath ["a" "b"] { a = { c = "yey"; }; };
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2023-12-08 22:15:53 +00:00
|
|
|
testHasAttrByPathNonStrict = {
|
|
|
|
expr = hasAttrByPath [] (throw "do not use");
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
2023-12-08 21:59:40 +00:00
|
|
|
testLongestValidPathPrefix_empty_empty = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ ] { };
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_empty_nonStrict = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ ] (throw "do not use");
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_zero = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "a" (throw "do not use") ] { d = null; };
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_zero_b = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "z" "z" ] "remarkably harmonious";
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_one = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a = null; };
|
|
|
|
expected = [ "a" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_two = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b = null; };
|
|
|
|
expected = [ "a" "b" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_three = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b.c = null; };
|
|
|
|
expected = [ "a" "b" "c" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLongestValidPathPrefix_three_extra = {
|
|
|
|
expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b.c.d = throw "nope"; };
|
|
|
|
expected = [ "a" "b" "c" ];
|
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexExample1 = {
|
|
|
|
expr = lists.findFirstIndex (x: x > 3) (abort "index found, so a default must not be evaluated") [ 1 6 4 ];
|
|
|
|
expected = 1;
|
2023-05-31 20:50:00 +00:00
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexExample2 = {
|
|
|
|
expr = lists.findFirstIndex (x: x > 9) "a very specific default" [ 1 6 4 ];
|
|
|
|
expected = "a very specific default";
|
2023-05-31 20:50:00 +00:00
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexEmpty = {
|
|
|
|
expr = lists.findFirstIndex (abort "when the list is empty, the predicate is not needed") null [];
|
2023-05-31 20:50:00 +00:00
|
|
|
expected = null;
|
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexSingleMatch = {
|
|
|
|
expr = lists.findFirstIndex (x: x == 5) null [ 5 ];
|
|
|
|
expected = 0;
|
2023-05-31 20:50:00 +00:00
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexSingleDefault = {
|
|
|
|
expr = lists.findFirstIndex (x: false) null [ (abort "if the predicate doesn't access the value, it must not be evaluated") ];
|
2023-05-31 20:50:00 +00:00
|
|
|
expected = null;
|
|
|
|
};
|
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexNone = {
|
|
|
|
expr = builtins.tryEval (lists.findFirstIndex (x: x == 2) null [ 1 (throw "the last element must be evaluated when there's no match") ]);
|
2023-05-31 20:50:00 +00:00
|
|
|
expected = { success = false; value = false; };
|
|
|
|
};
|
|
|
|
|
|
|
|
# Makes sure that the implementation doesn't cause a stack overflow
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexBig = {
|
|
|
|
expr = lists.findFirstIndex (x: x == 1000000) null (range 0 1000000);
|
2023-05-31 20:50:00 +00:00
|
|
|
expected = 1000000;
|
|
|
|
};
|
2016-11-06 00:51:13 +00:00
|
|
|
|
2023-07-19 14:44:11 +00:00
|
|
|
testFindFirstIndexLazy = {
|
|
|
|
expr = lists.findFirstIndex (x: x == 1) null [ 1 (abort "list elements after the match must not be evaluated") ];
|
|
|
|
expected = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
testFindFirstExample1 = {
|
|
|
|
expr = lists.findFirst (x: x > 3) 7 [ 1 6 4 ];
|
|
|
|
expected = 6;
|
|
|
|
};
|
|
|
|
|
|
|
|
testFindFirstExample2 = {
|
|
|
|
expr = lists.findFirst (x: x > 9) 7 [ 1 6 4 ];
|
|
|
|
expected = 7;
|
2023-05-31 20:53:50 +00:00
|
|
|
};
|
|
|
|
|
2023-06-25 11:16:06 +00:00
|
|
|
testAllUnique_true = {
|
|
|
|
expr = allUnique [ 3 2 4 1 ];
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
testAllUnique_false = {
|
|
|
|
expr = allUnique [ 3 2 3 4 ];
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2018-08-14 22:13:32 +00:00
|
|
|
# ATTRSETS
|
|
|
|
|
2022-11-16 17:20:38 +00:00
|
|
|
testConcatMapAttrs = {
|
|
|
|
expr = concatMapAttrs
|
|
|
|
(name: value: {
|
|
|
|
${name} = value;
|
|
|
|
${name + value} = value;
|
|
|
|
})
|
|
|
|
{
|
|
|
|
foo = "bar";
|
|
|
|
foobar = "baz";
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
foo = "bar";
|
|
|
|
foobar = "baz";
|
|
|
|
foobarbaz = "baz";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-30 16:05:50 +00:00
|
|
|
testFilterAttrs = {
|
|
|
|
expr = filterAttrs (n: v: n != "a" && (v.hello or false) == true) {
|
|
|
|
a.hello = true;
|
|
|
|
b.hello = true;
|
|
|
|
c = {
|
|
|
|
hello = true;
|
|
|
|
world = false;
|
|
|
|
};
|
|
|
|
d.hello = false;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
b.hello = true;
|
|
|
|
c = {
|
|
|
|
hello = true;
|
|
|
|
world = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-28 10:04:19 +00:00
|
|
|
# code from example
|
|
|
|
testFoldlAttrs = {
|
|
|
|
expr = {
|
|
|
|
example = foldlAttrs
|
|
|
|
(acc: name: value: {
|
|
|
|
sum = acc.sum + value;
|
|
|
|
names = acc.names ++ [ name ];
|
|
|
|
})
|
|
|
|
{ sum = 0; names = [ ]; }
|
|
|
|
{
|
|
|
|
foo = 1;
|
|
|
|
bar = 10;
|
|
|
|
};
|
|
|
|
# should just return the initial value
|
|
|
|
emptySet = foldlAttrs (throw "function not needed") 123 { };
|
|
|
|
# should just evaluate to the last value
|
2023-09-22 10:24:27 +00:00
|
|
|
valuesNotNeeded = foldlAttrs (acc: _name: _v: acc) 3 { z = throw "value z not needed"; a = throw "value a not needed"; };
|
2023-02-28 10:04:19 +00:00
|
|
|
# the accumulator doesnt have to be an attrset it can be as trivial as being just a number or string
|
|
|
|
trivialAcc = foldlAttrs (acc: _name: v: acc * 10 + v) 1 { z = 1; a = 2; };
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
example = {
|
|
|
|
sum = 11;
|
|
|
|
names = [ "bar" "foo" ];
|
|
|
|
};
|
|
|
|
emptySet = 123;
|
2023-09-22 10:24:27 +00:00
|
|
|
valuesNotNeeded = 3;
|
2023-02-28 10:04:19 +00:00
|
|
|
trivialAcc = 121;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-07-18 16:24:45 +00:00
|
|
|
|
|
|
|
testMergeAttrsListExample1 = {
|
|
|
|
expr = attrsets.mergeAttrsList [ { a = 0; b = 1; } { c = 2; d = 3; } ];
|
|
|
|
expected = { a = 0; b = 1; c = 2; d = 3; };
|
|
|
|
};
|
|
|
|
testMergeAttrsListExample2 = {
|
|
|
|
expr = attrsets.mergeAttrsList [ { a = 0; } { a = 1; } ];
|
|
|
|
expected = { a = 1; };
|
|
|
|
};
|
|
|
|
testMergeAttrsListExampleMany =
|
|
|
|
let
|
|
|
|
list = genList (n:
|
|
|
|
listToAttrs (genList (m:
|
|
|
|
let
|
|
|
|
# Integer divide n by two to create duplicate attributes
|
|
|
|
str = "halfn${toString (n / 2)}m${toString m}";
|
|
|
|
in
|
|
|
|
nameValuePair str str
|
|
|
|
) 100)
|
|
|
|
) 100;
|
|
|
|
in {
|
|
|
|
expr = attrsets.mergeAttrsList list;
|
|
|
|
expected = foldl' mergeAttrs { } list;
|
|
|
|
};
|
|
|
|
|
2018-08-14 22:13:32 +00:00
|
|
|
# code from the example
|
|
|
|
testRecursiveUpdateUntil = {
|
|
|
|
expr = recursiveUpdateUntil (path: l: r: path == ["foo"]) {
|
|
|
|
# first attribute set
|
|
|
|
foo.bar = 1;
|
|
|
|
foo.baz = 2;
|
|
|
|
bar = 3;
|
|
|
|
} {
|
|
|
|
#second attribute set
|
|
|
|
foo.bar = 1;
|
|
|
|
foo.quz = 2;
|
|
|
|
baz = 4;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
foo.bar = 1; # 'foo.*' from the second set
|
|
|
|
foo.quz = 2; #
|
|
|
|
bar = 3; # 'bar' from the first set
|
|
|
|
baz = 4; # 'baz' from the second set
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
lib.attrsets.matchAttrs: Avoid some list allocations when walking structure
Benchmarks (`nix-instantiate ./. -A python3`):
- Before:
``` json
{
"cpuTime": 0.29049500823020935,
"envs": {
"bytes": 4484216,
"elements": 221443,
"number": 169542
},
"gc": {
"heapSize": 402915328,
"totalBytes": 53086800
},
"list": {
"bytes": 749424,
"concats": 4242,
"elements": 93678
},
"nrAvoided": 253991,
"nrFunctionCalls": 149848,
"nrLookups": 49612,
"nrOpUpdateValuesCopied": 1587837,
"nrOpUpdates": 10104,
"nrPrimOpCalls": 130356,
"nrThunks": 358981,
"sets": {
"bytes": 30423600,
"elements": 1859999,
"number": 41476
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236145,
"number": 24453
},
"values": {
"bytes": 10502520,
"number": 437605
}
}
```
- After:
``` json
{
"cpuTime": 0.2946169972419739,
"envs": {
"bytes": 3315224,
"elements": 172735,
"number": 120834
},
"gc": {
"heapSize": 402915328,
"totalBytes": 48718432
},
"list": {
"bytes": 347568,
"concats": 4242,
"elements": 43446
},
"nrAvoided": 173252,
"nrFunctionCalls": 101140,
"nrLookups": 73595,
"nrOpUpdateValuesCopied": 1587837,
"nrOpUpdates": 10104,
"nrPrimOpCalls": 83067,
"nrThunks": 304216,
"sets": {
"bytes": 29704096,
"elements": 1831673,
"number": 24833
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236145,
"number": 24453
},
"values": {
"bytes": 8961552,
"number": 373398
}
}
```
2023-11-24 03:10:13 +00:00
|
|
|
testMatchAttrsMatchingExact = {
|
|
|
|
expr = matchAttrs { cpu = { bits = 64; }; } { cpu = { bits = 64; }; };
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testMatchAttrsMismatch = {
|
|
|
|
expr = matchAttrs { cpu = { bits = 128; }; } { cpu = { bits = 64; }; };
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testMatchAttrsMatchingImplicit = {
|
|
|
|
expr = matchAttrs { cpu = { }; } { cpu = { bits = 64; }; };
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testMatchAttrsMissingAttrs = {
|
|
|
|
expr = matchAttrs { cpu = {}; } { };
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2018-09-17 20:40:08 +00:00
|
|
|
testOverrideExistingEmpty = {
|
|
|
|
expr = overrideExisting {} { a = 1; };
|
|
|
|
expected = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
testOverrideExistingDisjoint = {
|
|
|
|
expr = overrideExisting { b = 2; } { a = 1; };
|
|
|
|
expected = { b = 2; };
|
|
|
|
};
|
|
|
|
|
|
|
|
testOverrideExistingOverride = {
|
|
|
|
expr = overrideExisting { a = 3; b = 2; } { a = 1; };
|
|
|
|
expected = { a = 1; b = 2; };
|
|
|
|
};
|
2018-08-14 22:13:32 +00:00
|
|
|
|
2023-09-10 15:56:29 +00:00
|
|
|
testListAttrsReverse = let
|
|
|
|
exampleAttrs = {foo=1; bar="asdf"; baz = [1 3 3 7]; fnord=null;};
|
|
|
|
exampleSingletonList = [{name="foo"; value=1;}];
|
|
|
|
in {
|
|
|
|
expr = {
|
|
|
|
isReverseToListToAttrs = builtins.listToAttrs (attrsToList exampleAttrs) == exampleAttrs;
|
|
|
|
isReverseToAttrsToList = attrsToList (builtins.listToAttrs exampleSingletonList) == exampleSingletonList;
|
|
|
|
testDuplicatePruningBehaviour = attrsToList (builtins.listToAttrs [{name="a"; value=2;} {name="a"; value=1;}]);
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
isReverseToAttrsToList = true;
|
|
|
|
isReverseToListToAttrs = true;
|
|
|
|
testDuplicatePruningBehaviour = [{name="a"; value=2;}];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testAttrsToListsCanDealWithFunctions = testingEval (
|
|
|
|
attrsToList { someFunc= a: a + 1;}
|
|
|
|
);
|
|
|
|
|
2024-07-06 05:48:32 +00:00
|
|
|
# FIXED-POINTS
|
|
|
|
|
|
|
|
testFix = {
|
|
|
|
expr = fix (x: {a = if x ? a then "a" else "b";});
|
|
|
|
expected = {a = "a";};
|
|
|
|
};
|
|
|
|
|
2024-07-04 20:37:01 +00:00
|
|
|
testToExtension = {
|
|
|
|
expr = [
|
|
|
|
(fix (final: { a = 0; c = final.a; }))
|
|
|
|
(fix (extends (toExtension { a = 1; b = 2; }) (final: { a = 0; c = final.a; })))
|
|
|
|
(fix (extends (toExtension (prev: { a = 1; b = prev.a; })) (final: { a = 0; c = final.a; })))
|
|
|
|
(fix (extends (toExtension (final: prev: { a = 1; b = prev.a; c = final.a + 1; })) (final: { a = 0; c = final.a; })))
|
|
|
|
];
|
|
|
|
expected = [
|
|
|
|
{ a = 0; c = 0; }
|
|
|
|
{ a = 1; b = 2; c = 1; }
|
|
|
|
{ a = 1; b = 0; c = 1; }
|
|
|
|
{ a = 1; b = 0; c = 2; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:43:52 +00:00
|
|
|
# GENERATORS
|
|
|
|
# these tests assume attributes are converted to lists
|
|
|
|
# in alphabetical order
|
2016-11-06 00:51:13 +00:00
|
|
|
|
2016-12-06 22:19:34 +00:00
|
|
|
testMkKeyValueDefault = {
|
2017-11-09 14:58:14 +00:00
|
|
|
expr = generators.mkKeyValueDefault {} ":" "f:oo" "bar";
|
2016-12-04 21:11:24 +00:00
|
|
|
expected = ''f\:oo:bar'';
|
|
|
|
};
|
|
|
|
|
2018-03-26 15:31:05 +00:00
|
|
|
testMkValueString = {
|
|
|
|
expr = let
|
|
|
|
vals = {
|
|
|
|
int = 42;
|
|
|
|
string = ''fo"o'';
|
|
|
|
bool = true;
|
|
|
|
bool2 = false;
|
|
|
|
null = null;
|
|
|
|
# float = 42.23; # floats are strange
|
|
|
|
};
|
|
|
|
in mapAttrs
|
|
|
|
(const (generators.mkValueStringDefault {}))
|
|
|
|
vals;
|
|
|
|
expected = {
|
|
|
|
int = "42";
|
|
|
|
string = ''fo"o'';
|
|
|
|
bool = "true";
|
|
|
|
bool2 = "false";
|
|
|
|
null = "null";
|
|
|
|
# float = "42.23" true false [ "bar" ] ]'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-12-04 21:11:24 +00:00
|
|
|
testToKeyValue = {
|
|
|
|
expr = generators.toKeyValue {} {
|
|
|
|
key = "value";
|
|
|
|
"other=key" = "baz";
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
key=value
|
|
|
|
other\=key=baz
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-11-06 00:51:13 +00:00
|
|
|
testToINIEmpty = {
|
|
|
|
expr = generators.toINI {} {};
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIEmptySection = {
|
|
|
|
expr = generators.toINI {} { foo = {}; bar = {}; };
|
|
|
|
expected = ''
|
|
|
|
[bar]
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-03-10 01:38:28 +00:00
|
|
|
testToINIDuplicateKeys = {
|
|
|
|
expr = generators.toINI { listsAsDuplicateKeys = true; } { foo.bar = true; baz.qux = [ 1 false ]; };
|
|
|
|
expected = ''
|
|
|
|
[baz]
|
|
|
|
qux=1
|
|
|
|
qux=false
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
bar=true
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-11-06 00:51:13 +00:00
|
|
|
testToINIDefaultEscapes = {
|
|
|
|
expr = generators.toINI {} {
|
|
|
|
"no [ and ] allowed unescaped" = {
|
|
|
|
"and also no = in keys" = 42;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
[no \[ and \] allowed unescaped]
|
|
|
|
and also no \= in keys=42
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIDefaultFull = {
|
|
|
|
expr = generators.toINI {} {
|
|
|
|
"section 1" = {
|
|
|
|
attribute1 = 5;
|
|
|
|
x = "Me-se JarJar Binx";
|
2018-03-26 15:31:05 +00:00
|
|
|
# booleans are converted verbatim by default
|
|
|
|
boolean = false;
|
2016-11-06 00:51:13 +00:00
|
|
|
};
|
|
|
|
"foo[]" = {
|
|
|
|
"he\\h=he" = "this is okay";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
[foo\[\]]
|
|
|
|
he\h\=he=this is okay
|
|
|
|
|
|
|
|
[section 1]
|
|
|
|
attribute1=5
|
2018-03-26 15:31:05 +00:00
|
|
|
boolean=false
|
2016-11-06 00:51:13 +00:00
|
|
|
x=Me-se JarJar Binx
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-14 09:14:18 +00:00
|
|
|
testToINIWithGlobalSectionEmpty = {
|
|
|
|
expr = generators.toINIWithGlobalSection {} {
|
|
|
|
globalSection = {
|
|
|
|
};
|
|
|
|
sections = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIWithGlobalSectionGlobalEmptyIsTheSameAsToINI =
|
|
|
|
let
|
|
|
|
sections = {
|
|
|
|
"section 1" = {
|
|
|
|
attribute1 = 5;
|
|
|
|
x = "Me-se JarJar Binx";
|
|
|
|
};
|
|
|
|
"foo" = {
|
|
|
|
"he\\h=he" = "this is okay";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr =
|
|
|
|
generators.toINIWithGlobalSection {} {
|
|
|
|
globalSection = {};
|
|
|
|
sections = sections;
|
|
|
|
};
|
|
|
|
expected = generators.toINI {} sections;
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIWithGlobalSectionFull = {
|
|
|
|
expr = generators.toINIWithGlobalSection {} {
|
|
|
|
globalSection = {
|
|
|
|
foo = "bar";
|
|
|
|
test = false;
|
|
|
|
};
|
|
|
|
sections = {
|
|
|
|
"section 1" = {
|
|
|
|
attribute1 = 5;
|
|
|
|
x = "Me-se JarJar Binx";
|
|
|
|
};
|
|
|
|
"foo" = {
|
|
|
|
"he\\h=he" = "this is okay";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
foo=bar
|
|
|
|
test=false
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
he\h\=he=this is okay
|
|
|
|
|
|
|
|
[section 1]
|
|
|
|
attribute1=5
|
|
|
|
x=Me-se JarJar Binx
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-08-21 11:12:40 +00:00
|
|
|
testToGitINI = {
|
|
|
|
expr = generators.toGitINI {
|
|
|
|
user = {
|
|
|
|
email = "user@example.org";
|
|
|
|
name = "John Doe";
|
|
|
|
signingKey = "00112233445566778899AABBCCDDEEFF";
|
|
|
|
};
|
|
|
|
gpg.program = "path-to-gpg";
|
|
|
|
tag.gpgSign = true;
|
|
|
|
include.path = "~/path/to/config.inc";
|
|
|
|
includeIf."gitdif:~/src/dir".path = "~/path/to/conditional.inc";
|
|
|
|
extra = {
|
|
|
|
boolean = true;
|
|
|
|
integer = 38;
|
|
|
|
name = "value";
|
|
|
|
subsection.value = "test";
|
|
|
|
};};
|
|
|
|
expected = ''
|
|
|
|
[extra]
|
|
|
|
${"\t"}boolean = true
|
|
|
|
${"\t"}integer = 38
|
|
|
|
${"\t"}name = "value"
|
|
|
|
|
|
|
|
[extra "subsection"]
|
|
|
|
${"\t"}value = "test"
|
|
|
|
|
|
|
|
[gpg]
|
|
|
|
${"\t"}program = "path-to-gpg"
|
|
|
|
|
|
|
|
[include]
|
|
|
|
${"\t"}path = "~/path/to/config.inc"
|
|
|
|
|
|
|
|
[includeIf "gitdif:~/src/dir"]
|
|
|
|
${"\t"}path = "~/path/to/conditional.inc"
|
|
|
|
|
|
|
|
[tag]
|
|
|
|
${"\t"}gpgSign = true
|
|
|
|
|
|
|
|
[user]
|
|
|
|
${"\t"}email = "user@example.org"
|
|
|
|
${"\t"}name = "John Doe"
|
|
|
|
${"\t"}signingKey = "00112233445566778899AABBCCDDEEFF"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
# right now only invocation check
|
2016-11-06 13:14:24 +00:00
|
|
|
testToJSONSimple =
|
|
|
|
let val = {
|
|
|
|
foobar = [ "baz" 1 2 3 ];
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = generators.toJSON {} val;
|
2017-04-19 19:41:28 +00:00
|
|
|
# trivial implementation
|
2016-11-06 13:14:24 +00:00
|
|
|
expected = builtins.toJSON val;
|
|
|
|
};
|
|
|
|
|
2024-04-04 14:36:07 +00:00
|
|
|
# right now only invocation check
|
2016-11-06 13:14:24 +00:00
|
|
|
testToYAMLSimple =
|
|
|
|
let val = {
|
|
|
|
list = [ { one = 1; } { two = 2; } ];
|
|
|
|
all = 42;
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = generators.toYAML {} val;
|
2017-04-19 19:41:28 +00:00
|
|
|
# trivial implementation
|
2016-11-06 13:14:24 +00:00
|
|
|
expected = builtins.toJSON val;
|
|
|
|
};
|
|
|
|
|
2020-09-17 16:08:44 +00:00
|
|
|
testToPretty =
|
|
|
|
let
|
2021-12-13 11:03:32 +00:00
|
|
|
deriv = derivation { name = "test"; builder = "/bin/sh"; system = "aarch64-linux"; };
|
2020-09-17 16:08:44 +00:00
|
|
|
in {
|
2020-09-04 15:46:12 +00:00
|
|
|
expr = mapAttrs (const (generators.toPretty { multiline = false; })) rec {
|
2017-06-06 20:41:22 +00:00
|
|
|
int = 42;
|
2018-10-13 17:55:00 +00:00
|
|
|
float = 0.1337;
|
2017-06-06 20:41:22 +00:00
|
|
|
bool = true;
|
2020-09-17 15:58:04 +00:00
|
|
|
emptystring = "";
|
2022-11-07 12:01:13 +00:00
|
|
|
string = "fn\${o}\"r\\d";
|
2020-09-17 15:58:04 +00:00
|
|
|
newlinestring = "\n";
|
2018-05-22 20:42:02 +00:00
|
|
|
path = /. + "/foo";
|
2017-06-06 20:41:22 +00:00
|
|
|
null_ = null;
|
|
|
|
function = x: x;
|
2017-06-12 05:07:59 +00:00
|
|
|
functionArgs = { arg ? 4, foo }: arg;
|
2017-06-06 20:41:22 +00:00
|
|
|
list = [ 3 4 function [ false ] ];
|
2020-09-17 16:14:18 +00:00
|
|
|
emptylist = [];
|
2022-11-07 12:01:13 +00:00
|
|
|
attrs = { foo = null; "foo b/ar" = "baz"; };
|
2020-09-17 16:14:18 +00:00
|
|
|
emptyattrs = {};
|
2020-09-17 16:08:44 +00:00
|
|
|
drv = deriv;
|
2017-06-06 20:41:22 +00:00
|
|
|
};
|
|
|
|
expected = rec {
|
|
|
|
int = "42";
|
2022-11-03 14:56:27 +00:00
|
|
|
float = "0.1337";
|
2017-06-06 20:41:22 +00:00
|
|
|
bool = "true";
|
2020-09-17 15:58:04 +00:00
|
|
|
emptystring = ''""'';
|
2022-11-07 12:01:13 +00:00
|
|
|
string = ''"fn\''${o}\"r\\d"'';
|
2020-09-17 15:58:04 +00:00
|
|
|
newlinestring = "\"\\n\"";
|
2018-04-25 13:04:30 +00:00
|
|
|
path = "/foo";
|
2017-06-06 20:41:22 +00:00
|
|
|
null_ = "null";
|
2020-09-17 16:08:44 +00:00
|
|
|
function = "<function>";
|
|
|
|
functionArgs = "<function, args: {arg?, foo}>";
|
2017-06-06 20:41:22 +00:00
|
|
|
list = "[ 3 4 ${function} [ false ] ]";
|
2020-09-17 16:14:18 +00:00
|
|
|
emptylist = "[ ]";
|
2022-11-07 12:01:13 +00:00
|
|
|
attrs = "{ foo = null; \"foo b/ar\" = \"baz\"; }";
|
2020-09-17 16:14:18 +00:00
|
|
|
emptyattrs = "{ }";
|
2022-11-04 16:31:41 +00:00
|
|
|
drv = "<derivation ${deriv.name}>";
|
2017-06-06 20:41:22 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-07-23 08:43:44 +00:00
|
|
|
testToPrettyLimit =
|
|
|
|
let
|
|
|
|
a.b = 1;
|
|
|
|
a.c = a;
|
|
|
|
in {
|
2021-08-25 22:28:49 +00:00
|
|
|
expr = generators.toPretty { } (generators.withRecursion { throwOnDepthLimit = false; depthLimit = 2; } a);
|
|
|
|
expected = "{\n b = 1;\n c = {\n b = \"<unevaluated>\";\n c = {\n b = \"<unevaluated>\";\n c = \"<unevaluated>\";\n };\n };\n}";
|
2021-07-23 08:43:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
testToPrettyLimitThrow =
|
|
|
|
let
|
|
|
|
a.b = 1;
|
|
|
|
a.c = a;
|
|
|
|
in {
|
2021-08-25 22:28:49 +00:00
|
|
|
expr = (builtins.tryEval
|
|
|
|
(generators.toPretty { } (generators.withRecursion { depthLimit = 2; } a))).success;
|
2021-07-23 08:43:44 +00:00
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2022-04-12 10:26:25 +00:00
|
|
|
testWithRecursionDealsWithFunctors =
|
|
|
|
let
|
|
|
|
functor = {
|
|
|
|
__functor = self: { a, b, }: null;
|
|
|
|
};
|
|
|
|
a = {
|
|
|
|
value = "1234";
|
|
|
|
b = functor;
|
|
|
|
c.d = functor;
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = generators.toPretty { } (generators.withRecursion { depthLimit = 1; throwOnDepthLimit = false; } a);
|
|
|
|
expected = "{\n b = <function, args: {a, b}>;\n c = {\n d = \"<unevaluated>\";\n };\n value = \"<unevaluated>\";\n}";
|
|
|
|
};
|
|
|
|
|
2020-09-04 15:46:12 +00:00
|
|
|
testToPrettyMultiline = {
|
2024-04-15 17:18:41 +00:00
|
|
|
expr = mapAttrs (const (generators.toPretty { })) {
|
2020-09-04 15:46:12 +00:00
|
|
|
list = [ 3 4 [ false ] ];
|
|
|
|
attrs = { foo = null; bar.foo = "baz"; };
|
2020-09-17 15:58:04 +00:00
|
|
|
newlinestring = "\n";
|
|
|
|
multilinestring = ''
|
|
|
|
hello
|
2022-11-07 12:01:13 +00:00
|
|
|
''${there}
|
|
|
|
te'''st
|
2020-09-17 15:58:04 +00:00
|
|
|
'';
|
|
|
|
multilinestring' = ''
|
|
|
|
hello
|
|
|
|
there
|
|
|
|
test'';
|
2020-09-04 15:46:12 +00:00
|
|
|
};
|
2024-04-15 17:18:41 +00:00
|
|
|
expected = {
|
2020-09-04 15:46:12 +00:00
|
|
|
list = ''
|
|
|
|
[
|
|
|
|
3
|
|
|
|
4
|
|
|
|
[
|
|
|
|
false
|
|
|
|
]
|
|
|
|
]'';
|
|
|
|
attrs = ''
|
|
|
|
{
|
|
|
|
bar = {
|
|
|
|
foo = "baz";
|
|
|
|
};
|
|
|
|
foo = null;
|
|
|
|
}'';
|
2020-09-17 15:58:04 +00:00
|
|
|
newlinestring = "''\n \n''";
|
|
|
|
multilinestring = ''
|
|
|
|
'''
|
|
|
|
hello
|
2022-11-07 12:01:13 +00:00
|
|
|
'''''${there}
|
|
|
|
te''''st
|
2020-09-17 15:58:04 +00:00
|
|
|
''''';
|
|
|
|
multilinestring' = ''
|
|
|
|
'''
|
|
|
|
hello
|
|
|
|
there
|
|
|
|
test''''';
|
|
|
|
|
2017-06-06 20:41:22 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testToPrettyAllowPrettyValues = {
|
|
|
|
expr = generators.toPretty { allowPrettyValues = true; }
|
|
|
|
{ __pretty = v: "«" + v + "»"; val = "foo"; };
|
|
|
|
expected = "«foo»";
|
|
|
|
};
|
|
|
|
|
2024-04-15 17:18:41 +00:00
|
|
|
testToPlist = {
|
2023-03-27 14:27:07 +00:00
|
|
|
expr = mapAttrs (const (generators.toPlist { })) {
|
|
|
|
value = {
|
2024-04-15 17:18:41 +00:00
|
|
|
nested.values = {
|
2023-03-27 14:27:07 +00:00
|
|
|
int = 42;
|
|
|
|
float = 0.1337;
|
|
|
|
bool = true;
|
|
|
|
emptystring = "";
|
|
|
|
string = "fn\${o}\"r\\d";
|
|
|
|
newlinestring = "\n";
|
|
|
|
path = /. + "/foo";
|
|
|
|
null_ = null;
|
|
|
|
list = [ 3 4 "test" ];
|
|
|
|
emptylist = [];
|
|
|
|
attrs = { foo = null; "foo b/ar" = "baz"; };
|
|
|
|
emptyattrs = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-03-27 15:06:12 +00:00
|
|
|
expected = { value = builtins.readFile ./test-to-plist-expected.plist; };
|
2023-03-27 14:27:07 +00:00
|
|
|
};
|
2020-01-22 22:24:06 +00:00
|
|
|
|
2023-04-09 20:21:36 +00:00
|
|
|
testToLuaEmptyAttrSet = {
|
|
|
|
expr = generators.toLua {} {};
|
|
|
|
expected = ''{}'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaEmptyList = {
|
|
|
|
expr = generators.toLua {} [];
|
|
|
|
expected = ''{}'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaListOfVariousTypes = {
|
|
|
|
expr = generators.toLua {} [ null 43 3.14159 true ];
|
|
|
|
expected = ''
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
43,
|
|
|
|
3.14159,
|
|
|
|
true
|
|
|
|
}'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaString = {
|
|
|
|
expr = generators.toLua {} ''double-quote (") and single quotes (')'';
|
|
|
|
expected = ''"double-quote (\") and single quotes (')"'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaAttrsetWithLuaInline = {
|
|
|
|
expr = generators.toLua {} { x = generators.mkLuaInline ''"abc" .. "def"''; };
|
|
|
|
expected = ''
|
|
|
|
{
|
|
|
|
["x"] = ("abc" .. "def")
|
|
|
|
}'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaAttrsetWithSpaceInKey = {
|
2023-04-23 17:35:52 +00:00
|
|
|
expr = generators.toLua {} { "some space and double-quote (\")" = 42; };
|
2023-04-09 20:21:36 +00:00
|
|
|
expected = ''
|
|
|
|
{
|
2023-04-23 17:35:52 +00:00
|
|
|
["some space and double-quote (\")"] = 42
|
2023-04-09 20:21:36 +00:00
|
|
|
}'';
|
|
|
|
};
|
|
|
|
|
2023-04-23 17:35:52 +00:00
|
|
|
testToLuaWithoutMultiline = {
|
|
|
|
expr = generators.toLua { multiline = false; } [ 41 43 ];
|
|
|
|
expected = ''{ 41, 43 }'';
|
|
|
|
};
|
|
|
|
|
2023-04-29 16:35:07 +00:00
|
|
|
testToLuaEmptyBindings = {
|
|
|
|
expr = generators.toLua { asBindings = true; } {};
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaBindings = {
|
|
|
|
expr = generators.toLua { asBindings = true; } { x1 = 41; _y = { a = 43; }; };
|
|
|
|
expected = ''
|
|
|
|
_y = {
|
|
|
|
["a"] = 43
|
|
|
|
}
|
|
|
|
x1 = 41
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaPartialTableBindings = {
|
|
|
|
expr = generators.toLua { asBindings = true; } { "x.y" = 42; };
|
|
|
|
expected = ''
|
|
|
|
x.y = 42
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaIndentedBindings = {
|
|
|
|
expr = generators.toLua { asBindings = true; indent = " "; } { x = { y = 42; }; };
|
|
|
|
expected = " x = {\n [\"y\"] = 42\n }\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
testToLuaBindingsWithSpace = testingThrow (
|
|
|
|
generators.toLua { asBindings = true; } { "with space" = 42; }
|
|
|
|
);
|
|
|
|
|
|
|
|
testToLuaBindingsWithLeadingDigit = testingThrow (
|
|
|
|
generators.toLua { asBindings = true; } { "11eleven" = 42; }
|
|
|
|
);
|
|
|
|
|
2023-04-09 20:21:36 +00:00
|
|
|
testToLuaBasicExample = {
|
|
|
|
expr = generators.toLua {} {
|
|
|
|
cmd = [ "typescript-language-server" "--stdio" ];
|
|
|
|
settings.workspace.library = generators.mkLuaInline ''vim.api.nvim_get_runtime_file("", true)'';
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
{
|
|
|
|
["cmd"] = {
|
|
|
|
"typescript-language-server",
|
|
|
|
"--stdio"
|
|
|
|
},
|
|
|
|
["settings"] = {
|
|
|
|
["workspace"] = {
|
|
|
|
["library"] = (vim.api.nvim_get_runtime_file("", true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'';
|
|
|
|
};
|
|
|
|
|
2020-01-22 22:24:06 +00:00
|
|
|
# CLI
|
|
|
|
|
|
|
|
testToGNUCommandLine = {
|
2020-01-22 22:37:10 +00:00
|
|
|
expr = cli.toGNUCommandLine {} {
|
|
|
|
data = builtins.toJSON { id = 0; };
|
|
|
|
X = "PUT";
|
|
|
|
retry = 3;
|
|
|
|
retry-delay = null;
|
|
|
|
url = [ "https://example.com/foo" "https://example.com/bar" ];
|
|
|
|
silent = false;
|
|
|
|
verbose = true;
|
|
|
|
};
|
2020-01-22 22:24:06 +00:00
|
|
|
|
2020-01-22 22:37:10 +00:00
|
|
|
expected = [
|
|
|
|
"-X" "PUT"
|
|
|
|
"--data" "{\"id\":0}"
|
|
|
|
"--retry" "3"
|
|
|
|
"--url" "https://example.com/foo"
|
|
|
|
"--url" "https://example.com/bar"
|
|
|
|
"--verbose"
|
|
|
|
];
|
2020-01-22 22:24:06 +00:00
|
|
|
};
|
|
|
|
|
2024-05-06 11:41:46 +00:00
|
|
|
testToGNUCommandLineSeparator = {
|
|
|
|
expr = cli.toGNUCommandLine { optionValueSeparator = "="; } {
|
|
|
|
data = builtins.toJSON { id = 0; };
|
|
|
|
X = "PUT";
|
|
|
|
retry = 3;
|
|
|
|
retry-delay = null;
|
|
|
|
url = [ "https://example.com/foo" "https://example.com/bar" ];
|
|
|
|
silent = false;
|
|
|
|
verbose = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
"-X=PUT"
|
|
|
|
"--data={\"id\":0}"
|
|
|
|
"--retry=3"
|
|
|
|
"--url=https://example.com/foo"
|
|
|
|
"--url=https://example.com/bar"
|
|
|
|
"--verbose"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2020-01-22 22:24:06 +00:00
|
|
|
testToGNUCommandLineShell = {
|
2020-01-22 22:37:10 +00:00
|
|
|
expr = cli.toGNUCommandLineShell {} {
|
|
|
|
data = builtins.toJSON { id = 0; };
|
|
|
|
X = "PUT";
|
|
|
|
retry = 3;
|
|
|
|
retry-delay = null;
|
|
|
|
url = [ "https://example.com/foo" "https://example.com/bar" ];
|
|
|
|
silent = false;
|
|
|
|
verbose = true;
|
|
|
|
};
|
2019-12-12 00:30:05 +00:00
|
|
|
|
2024-08-16 14:26:08 +00:00
|
|
|
expected = "-X PUT --data '{\"id\":0}' --retry 3 --url https://example.com/foo --url https://example.com/bar --verbose";
|
2019-12-12 00:30:05 +00:00
|
|
|
};
|
2020-01-27 22:03:38 +00:00
|
|
|
|
|
|
|
testSanitizeDerivationNameLeadingDots = testSanitizeDerivationName {
|
|
|
|
name = "..foo";
|
|
|
|
expected = "foo";
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:20:44 +00:00
|
|
|
testSanitizeDerivationNameUnicode = testSanitizeDerivationName {
|
|
|
|
name = "fö";
|
|
|
|
expected = "f-";
|
|
|
|
};
|
|
|
|
|
2020-01-27 22:03:38 +00:00
|
|
|
testSanitizeDerivationNameAscii = testSanitizeDerivationName {
|
|
|
|
name = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
|
|
|
expected = "-+--.-0123456789-=-?-ABCDEFGHIJKLMNOPQRSTUVWXYZ-_-abcdefghijklmnopqrstuvwxyz-";
|
|
|
|
};
|
|
|
|
|
|
|
|
testSanitizeDerivationNameTooLong = testSanitizeDerivationName {
|
|
|
|
name = "This string is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";
|
|
|
|
expected = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";
|
|
|
|
};
|
|
|
|
|
|
|
|
testSanitizeDerivationNameTooLongWithInvalid = testSanitizeDerivationName {
|
|
|
|
name = "Hello there aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&&&&&&&";
|
|
|
|
expected = "there-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-";
|
|
|
|
};
|
|
|
|
|
|
|
|
testSanitizeDerivationNameEmpty = testSanitizeDerivationName {
|
|
|
|
name = "";
|
|
|
|
expected = "unknown";
|
|
|
|
};
|
2020-09-04 13:50:13 +00:00
|
|
|
|
|
|
|
testFreeformOptions = {
|
|
|
|
expr =
|
|
|
|
let
|
|
|
|
submodule = { lib, ... }: {
|
|
|
|
freeformType = lib.types.attrsOf (lib.types.submodule {
|
|
|
|
options.bar = lib.mkOption {};
|
|
|
|
});
|
|
|
|
options.bar = lib.mkOption {};
|
|
|
|
};
|
|
|
|
|
|
|
|
module = { lib, ... }: {
|
|
|
|
options.foo = lib.mkOption {
|
|
|
|
type = lib.types.submodule submodule;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
options = (evalModules {
|
|
|
|
modules = [ module ];
|
|
|
|
}).options;
|
|
|
|
|
2020-12-18 15:42:42 +00:00
|
|
|
locs = filter (o: ! o.internal) (optionAttrSetToDocList options);
|
2020-09-04 13:50:13 +00:00
|
|
|
in map (o: o.loc) locs;
|
2022-03-24 01:44:43 +00:00
|
|
|
expected = [ [ "_module" "args" ] [ "foo" ] [ "foo" "<name>" "bar" ] [ "foo" "bar" ] ];
|
2020-09-04 13:50:13 +00:00
|
|
|
};
|
|
|
|
|
2021-01-25 15:59:46 +00:00
|
|
|
testCartesianProductOfEmptySet = {
|
2024-04-15 17:13:22 +00:00
|
|
|
expr = cartesianProduct {};
|
2021-01-25 15:59:46 +00:00
|
|
|
expected = [ {} ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testCartesianProductOfOneSet = {
|
2024-04-15 17:13:22 +00:00
|
|
|
expr = cartesianProduct { a = [ 1 2 3 ]; };
|
2021-01-25 15:59:46 +00:00
|
|
|
expected = [ { a = 1; } { a = 2; } { a = 3; } ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testCartesianProductOfTwoSets = {
|
2024-04-15 17:13:22 +00:00
|
|
|
expr = cartesianProduct { a = [ 1 ]; b = [ 10 20 ]; };
|
2021-01-25 15:59:46 +00:00
|
|
|
expected = [
|
|
|
|
{ a = 1; b = 10; }
|
|
|
|
{ a = 1; b = 20; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testCartesianProductOfTwoSetsWithOneEmpty = {
|
2024-04-15 17:13:22 +00:00
|
|
|
expr = cartesianProduct { a = [ ]; b = [ 10 20 ]; };
|
2021-01-25 15:59:46 +00:00
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testCartesianProductOfThreeSets = {
|
2024-04-15 17:13:22 +00:00
|
|
|
expr = cartesianProduct {
|
2021-01-25 15:59:46 +00:00
|
|
|
a = [ 1 2 3 ];
|
|
|
|
b = [ 10 20 30 ];
|
|
|
|
c = [ 100 200 300 ];
|
|
|
|
};
|
|
|
|
expected = [
|
|
|
|
{ a = 1; b = 10; c = 100; }
|
|
|
|
{ a = 1; b = 10; c = 200; }
|
|
|
|
{ a = 1; b = 10; c = 300; }
|
|
|
|
|
|
|
|
{ a = 1; b = 20; c = 100; }
|
|
|
|
{ a = 1; b = 20; c = 200; }
|
|
|
|
{ a = 1; b = 20; c = 300; }
|
|
|
|
|
|
|
|
{ a = 1; b = 30; c = 100; }
|
|
|
|
{ a = 1; b = 30; c = 200; }
|
|
|
|
{ a = 1; b = 30; c = 300; }
|
|
|
|
|
|
|
|
{ a = 2; b = 10; c = 100; }
|
|
|
|
{ a = 2; b = 10; c = 200; }
|
|
|
|
{ a = 2; b = 10; c = 300; }
|
|
|
|
|
|
|
|
{ a = 2; b = 20; c = 100; }
|
|
|
|
{ a = 2; b = 20; c = 200; }
|
|
|
|
{ a = 2; b = 20; c = 300; }
|
|
|
|
|
|
|
|
{ a = 2; b = 30; c = 100; }
|
|
|
|
{ a = 2; b = 30; c = 200; }
|
|
|
|
{ a = 2; b = 30; c = 300; }
|
|
|
|
|
|
|
|
{ a = 3; b = 10; c = 100; }
|
|
|
|
{ a = 3; b = 10; c = 200; }
|
|
|
|
{ a = 3; b = 10; c = 300; }
|
|
|
|
|
|
|
|
{ a = 3; b = 20; c = 100; }
|
|
|
|
{ a = 3; b = 20; c = 200; }
|
|
|
|
{ a = 3; b = 20; c = 300; }
|
|
|
|
|
|
|
|
{ a = 3; b = 30; c = 100; }
|
|
|
|
{ a = 3; b = 30; c = 200; }
|
|
|
|
{ a = 3; b = 30; c = 300; }
|
|
|
|
];
|
|
|
|
};
|
2021-12-02 17:12:51 +00:00
|
|
|
|
2024-04-15 17:16:15 +00:00
|
|
|
testMapCartesianProductOfOneSet = {
|
|
|
|
expr = mapCartesianProduct ({a}: a * 2) { a = [ 1 2 3 ]; };
|
|
|
|
expected = [ 2 4 6 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testMapCartesianProductOfTwoSets = {
|
|
|
|
expr = mapCartesianProduct ({a,b}: a + b) { a = [ 1 ]; b = [ 10 20 ]; };
|
|
|
|
expected = [ 11 21 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testMapCartesianProcutOfTwoSetsWithOneEmpty = {
|
|
|
|
expr = mapCartesianProduct (x: x.a + x.b) { a = [ ]; b = [ 10 20 ]; };
|
|
|
|
expected = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testMapCartesianProductOfThreeSets = {
|
|
|
|
expr = mapCartesianProduct ({a,b,c}: a + b + c) {
|
|
|
|
a = [ 1 2 3 ];
|
|
|
|
b = [ 10 20 30 ];
|
|
|
|
c = [ 100 200 300 ];
|
|
|
|
};
|
|
|
|
expected = [ 111 211 311 121 221 321 131 231 331 112 212 312 122 222 322 132 232 332 113 213 313 123 223 323 133 233 333 ];
|
|
|
|
};
|
|
|
|
|
2021-12-02 17:12:51 +00:00
|
|
|
# The example from the showAttrPath documentation
|
|
|
|
testShowAttrPathExample = {
|
|
|
|
expr = showAttrPath [ "foo" "10" "bar" ];
|
|
|
|
expected = "foo.\"10\".bar";
|
|
|
|
};
|
|
|
|
|
|
|
|
testShowAttrPathEmpty = {
|
|
|
|
expr = showAttrPath [];
|
|
|
|
expected = "<root attribute path>";
|
|
|
|
};
|
|
|
|
|
|
|
|
testShowAttrPathVarious = {
|
|
|
|
expr = showAttrPath [
|
|
|
|
"."
|
|
|
|
"foo"
|
|
|
|
"2"
|
|
|
|
"a2-b"
|
|
|
|
"_bc'de"
|
|
|
|
];
|
2021-12-02 18:34:08 +00:00
|
|
|
expected = ''".".foo."2".a2-b._bc'de'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testGroupBy = {
|
|
|
|
expr = groupBy (n: toString (mod n 5)) (range 0 16);
|
|
|
|
expected = {
|
|
|
|
"0" = [ 0 5 10 15 ];
|
|
|
|
"1" = [ 1 6 11 16 ];
|
|
|
|
"2" = [ 2 7 12 ];
|
|
|
|
"3" = [ 3 8 13 ];
|
|
|
|
"4" = [ 4 9 14 ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testGroupBy' = {
|
|
|
|
expr = groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ];
|
|
|
|
expected = { false = 3; true = 12; };
|
2021-12-02 17:12:51 +00:00
|
|
|
};
|
2021-11-22 19:34:01 +00:00
|
|
|
|
|
|
|
# The example from the updateManyAttrsByPath documentation
|
|
|
|
testUpdateManyAttrsByPathExample = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ "a" "b" ];
|
|
|
|
update = old: { d = old.c; };
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ "a" "b" "c" ];
|
|
|
|
update = old: old + 1;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ "x" "y" ];
|
|
|
|
update = old: "xy";
|
|
|
|
}
|
|
|
|
] { a.b.c = 0; };
|
|
|
|
expected = { a = { b = { d = 1; }; }; x = { y = "xy"; }; };
|
|
|
|
};
|
|
|
|
|
|
|
|
# If there are no updates, the value is passed through
|
|
|
|
testUpdateManyAttrsByPathNone = {
|
|
|
|
expr = updateManyAttrsByPath [] "something";
|
|
|
|
expected = "something";
|
|
|
|
};
|
|
|
|
|
|
|
|
# A single update to the root path is just like applying the function directly
|
|
|
|
testUpdateManyAttrsByPathSingleIncrement = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: old + 1;
|
|
|
|
}
|
|
|
|
] 0;
|
|
|
|
expected = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Multiple updates can be applied are done in order
|
|
|
|
testUpdateManyAttrsByPathMultipleIncrements = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: old + "a";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: old + "b";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: old + "c";
|
|
|
|
}
|
|
|
|
] "";
|
|
|
|
expected = "abc";
|
|
|
|
};
|
|
|
|
|
|
|
|
# If an update doesn't use the value, all previous updates are not evaluated
|
|
|
|
testUpdateManyAttrsByPathLazy = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: old + throw "nope";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ ];
|
|
|
|
update = old: "untainted";
|
|
|
|
}
|
|
|
|
] (throw "start");
|
|
|
|
expected = "untainted";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Deeply nested attributes can be updated without affecting others
|
|
|
|
testUpdateManyAttrsByPathDeep = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ "a" "b" "c" ];
|
|
|
|
update = old: old + 1;
|
|
|
|
}
|
|
|
|
] {
|
|
|
|
a.b.c = 0;
|
|
|
|
|
|
|
|
a.b.z = 0;
|
|
|
|
a.y.z = 0;
|
|
|
|
x.y.z = 0;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
a.b.c = 1;
|
|
|
|
|
|
|
|
a.b.z = 0;
|
|
|
|
a.y.z = 0;
|
|
|
|
x.y.z = 0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Nested attributes are updated first
|
|
|
|
testUpdateManyAttrsByPathNestedBeforehand = {
|
|
|
|
expr = updateManyAttrsByPath [
|
|
|
|
{
|
|
|
|
path = [ "a" ];
|
|
|
|
update = old: old // { x = old.b; };
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = [ "a" "b" ];
|
|
|
|
update = old: old + 1;
|
|
|
|
}
|
|
|
|
] {
|
|
|
|
a.b = 0;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
a.b = 1;
|
|
|
|
a.x = 1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-25 17:54:19 +00:00
|
|
|
## Levenshtein distance functions and co.
|
|
|
|
testCommonPrefixLengthEmpty = {
|
|
|
|
expr = strings.commonPrefixLength "" "hello";
|
|
|
|
expected = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCommonPrefixLengthSame = {
|
|
|
|
expr = strings.commonPrefixLength "hello" "hello";
|
|
|
|
expected = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCommonPrefixLengthDiffering = {
|
|
|
|
expr = strings.commonPrefixLength "hello" "hey";
|
|
|
|
expected = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCommonSuffixLengthEmpty = {
|
|
|
|
expr = strings.commonSuffixLength "" "hello";
|
|
|
|
expected = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCommonSuffixLengthSame = {
|
|
|
|
expr = strings.commonSuffixLength "hello" "hello";
|
|
|
|
expected = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
testCommonSuffixLengthDiffering = {
|
|
|
|
expr = strings.commonSuffixLength "test" "rest";
|
|
|
|
expected = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinEmpty = {
|
|
|
|
expr = strings.levenshtein "" "";
|
|
|
|
expected = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinOnlyAdd = {
|
|
|
|
expr = strings.levenshtein "" "hello there";
|
|
|
|
expected = 11;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinOnlyRemove = {
|
|
|
|
expr = strings.levenshtein "hello there" "";
|
|
|
|
expected = 11;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinOnlyTransform = {
|
|
|
|
expr = strings.levenshtein "abcdef" "ghijkl";
|
|
|
|
expected = 6;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinMixed = {
|
|
|
|
expr = strings.levenshtein "kitchen" "sitting";
|
|
|
|
expected = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostZeroFalse = {
|
|
|
|
expr = strings.levenshteinAtMost 0 "foo" "boo";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostZeroTrue = {
|
|
|
|
expr = strings.levenshteinAtMost 0 "foo" "foo";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostOneFalse = {
|
|
|
|
expr = strings.levenshteinAtMost 1 "car" "ct";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostOneTrue = {
|
|
|
|
expr = strings.levenshteinAtMost 1 "car" "cr";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
# We test levenshteinAtMost 2 particularly well because it uses a complicated
|
|
|
|
# implementation
|
|
|
|
testLevenshteinAtMostTwoIsEmpty = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "" "";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoIsZero = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "abcdef";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoIsOne = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "abddef";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff0False = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "aczyef";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff0Outer = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "zbcdez";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff0DelLeft = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "bcdefz";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff0DelRight = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "zabcde";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff1False = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "bddez";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff1DelLeft = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "bcdez";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff1DelRight = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "abcdef" "zbcde";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff2False = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "hello" "hxo";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff2True = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "hello" "heo";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostTwoDiff3 = {
|
|
|
|
expr = strings.levenshteinAtMost 2 "hello" "ho";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostThreeFalse = {
|
|
|
|
expr = strings.levenshteinAtMost 3 "hello" "Holla!";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testLevenshteinAtMostThreeTrue = {
|
|
|
|
expr = strings.levenshteinAtMost 3 "hello" "Holla";
|
|
|
|
expected = true;
|
|
|
|
};
|
2022-09-15 16:29:38 +00:00
|
|
|
|
2024-01-30 22:29:47 +00:00
|
|
|
# DERIVATIONS
|
2022-06-25 11:53:02 +00:00
|
|
|
|
|
|
|
testLazyDerivationIsLazyInDerivationForAttrNames = {
|
|
|
|
expr = attrNames (lazyDerivation {
|
|
|
|
derivation = throw "not lazy enough";
|
|
|
|
});
|
|
|
|
# It's ok to add attribute names here when lazyDerivation is improved
|
|
|
|
# in accordance with its inline comments.
|
|
|
|
expected = [ "drvPath" "meta" "name" "out" "outPath" "outputName" "outputs" "system" "type" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testLazyDerivationIsLazyInDerivationForPassthruAttr = {
|
|
|
|
expr = (lazyDerivation {
|
|
|
|
derivation = throw "not lazy enough";
|
|
|
|
passthru.tests = "whatever is in tests";
|
|
|
|
}).tests;
|
|
|
|
expected = "whatever is in tests";
|
|
|
|
};
|
|
|
|
|
|
|
|
testLazyDerivationIsLazyInDerivationForPassthruAttr2 = {
|
|
|
|
# passthru.tests is not a special case. It works for any attr.
|
|
|
|
expr = (lazyDerivation {
|
|
|
|
derivation = throw "not lazy enough";
|
|
|
|
passthru.foo = "whatever is in foo";
|
|
|
|
}).foo;
|
|
|
|
expected = "whatever is in foo";
|
|
|
|
};
|
|
|
|
|
|
|
|
testLazyDerivationIsLazyInDerivationForMeta = {
|
|
|
|
expr = (lazyDerivation {
|
|
|
|
derivation = throw "not lazy enough";
|
|
|
|
meta = "whatever is in meta";
|
|
|
|
}).meta;
|
|
|
|
expected = "whatever is in meta";
|
|
|
|
};
|
|
|
|
|
|
|
|
testLazyDerivationReturnsDerivationAttrs = let
|
|
|
|
derivation = {
|
|
|
|
type = "derivation";
|
|
|
|
outputs = ["out"];
|
|
|
|
out = "test out";
|
|
|
|
outPath = "test outPath";
|
|
|
|
outputName = "out";
|
|
|
|
drvPath = "test drvPath";
|
|
|
|
name = "test name";
|
|
|
|
system = "test system";
|
|
|
|
meta = "test meta";
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = lazyDerivation { inherit derivation; };
|
|
|
|
expected = derivation;
|
|
|
|
};
|
|
|
|
|
2024-01-30 22:29:47 +00:00
|
|
|
testOptionalDrvAttr = let
|
|
|
|
mkDerivation = args: derivation (args // {
|
|
|
|
builder = "builder";
|
|
|
|
system = "system";
|
|
|
|
__ignoreNulls = true;
|
|
|
|
});
|
|
|
|
in {
|
|
|
|
expr = (mkDerivation {
|
|
|
|
name = "foo";
|
|
|
|
x = optionalDrvAttr true 1;
|
|
|
|
y = optionalDrvAttr false 1;
|
|
|
|
}).drvPath;
|
|
|
|
expected = (mkDerivation {
|
|
|
|
name = "foo";
|
|
|
|
x = 1;
|
|
|
|
}).drvPath;
|
|
|
|
};
|
|
|
|
|
2024-02-28 22:01:43 +00:00
|
|
|
testLazyDerivationMultiOutputReturnsDerivationAttrs = let
|
|
|
|
derivation = {
|
|
|
|
type = "derivation";
|
|
|
|
outputs = ["out" "dev"];
|
|
|
|
dev = "test dev";
|
|
|
|
out = "test out";
|
|
|
|
outPath = "test outPath";
|
|
|
|
outputName = "out";
|
|
|
|
drvPath = "test drvPath";
|
|
|
|
name = "test name";
|
|
|
|
system = "test system";
|
|
|
|
meta.position = "/hi:23";
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = lazyDerivation { inherit derivation; outputs = ["out" "dev"]; passthru.meta.position = "/hi:23"; };
|
|
|
|
expected = derivation;
|
|
|
|
};
|
|
|
|
|
2022-09-15 16:29:38 +00:00
|
|
|
testTypeDescriptionInt = {
|
|
|
|
expr = (with types; int).description;
|
|
|
|
expected = "signed integer";
|
|
|
|
};
|
2023-12-23 10:40:16 +00:00
|
|
|
testTypeDescriptionIntsPositive = {
|
|
|
|
expr = (with types; ints.positive).description;
|
|
|
|
expected = "positive integer, meaning >0";
|
|
|
|
};
|
|
|
|
testTypeDescriptionIntsPositiveOrEnumAuto = {
|
|
|
|
expr = (with types; either ints.positive (enum ["auto"])).description;
|
|
|
|
expected = ''positive integer, meaning >0, or value "auto" (singular enum)'';
|
|
|
|
};
|
|
|
|
testTypeDescriptionListOfPositive = {
|
|
|
|
expr = (with types; listOf ints.positive).description;
|
|
|
|
expected = "list of (positive integer, meaning >0)";
|
|
|
|
};
|
2022-09-15 16:29:38 +00:00
|
|
|
testTypeDescriptionListOfInt = {
|
|
|
|
expr = (with types; listOf int).description;
|
|
|
|
expected = "list of signed integer";
|
|
|
|
};
|
|
|
|
testTypeDescriptionListOfListOfInt = {
|
|
|
|
expr = (with types; listOf (listOf int)).description;
|
|
|
|
expected = "list of list of signed integer";
|
|
|
|
};
|
|
|
|
testTypeDescriptionListOfEitherStrOrBool = {
|
|
|
|
expr = (with types; listOf (either str bool)).description;
|
|
|
|
expected = "list of (string or boolean)";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherListOfStrOrBool = {
|
|
|
|
expr = (with types; either (listOf bool) str).description;
|
|
|
|
expected = "(list of boolean) or string";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherStrOrListOfBool = {
|
|
|
|
expr = (with types; either str (listOf bool)).description;
|
|
|
|
expected = "string or list of boolean";
|
|
|
|
};
|
|
|
|
testTypeDescriptionOneOfListOfStrOrBool = {
|
|
|
|
expr = (with types; oneOf [ (listOf bool) str ]).description;
|
|
|
|
expected = "(list of boolean) or string";
|
|
|
|
};
|
|
|
|
testTypeDescriptionOneOfListOfStrOrBoolOrNumber = {
|
|
|
|
expr = (with types; oneOf [ (listOf bool) str number ]).description;
|
|
|
|
expected = "(list of boolean) or string or signed integer or floating point number";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherListOfBoolOrEitherStringOrNumber = {
|
|
|
|
expr = (with types; either (listOf bool) (either str number)).description;
|
|
|
|
expected = "(list of boolean) or string or signed integer or floating point number";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherEitherListOfBoolOrStringOrNumber = {
|
|
|
|
expr = (with types; either (either (listOf bool) str) number).description;
|
|
|
|
expected = "(list of boolean) or string or signed integer or floating point number";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherNullOrBoolOrString = {
|
|
|
|
expr = (with types; either (nullOr bool) str).description;
|
|
|
|
expected = "null or boolean or string";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherListOfEitherBoolOrStrOrInt = {
|
|
|
|
expr = (with types; either (listOf (either bool str)) int).description;
|
|
|
|
expected = "(list of (boolean or string)) or signed integer";
|
|
|
|
};
|
|
|
|
testTypeDescriptionEitherIntOrListOrEitherBoolOrStr = {
|
|
|
|
expr = (with types; either int (listOf (either bool str))).description;
|
|
|
|
expected = "signed integer or list of (boolean or string)";
|
|
|
|
};
|
2023-10-16 22:55:38 +00:00
|
|
|
|
|
|
|
# Meta
|
|
|
|
testGetExe'Output = {
|
|
|
|
expr = getExe' {
|
|
|
|
type = "derivation";
|
|
|
|
out = "somelonghash";
|
|
|
|
bin = "somelonghash";
|
|
|
|
} "executable";
|
|
|
|
expected = "somelonghash/bin/executable";
|
|
|
|
};
|
|
|
|
|
|
|
|
testGetExeOutput = {
|
|
|
|
expr = getExe {
|
|
|
|
type = "derivation";
|
|
|
|
out = "somelonghash";
|
|
|
|
bin = "somelonghash";
|
|
|
|
meta.mainProgram = "mainProgram";
|
|
|
|
};
|
|
|
|
expected = "somelonghash/bin/mainProgram";
|
|
|
|
};
|
|
|
|
|
|
|
|
testGetExe'FailureFirstArg = testingThrow (
|
|
|
|
getExe' "not a derivation" "executable"
|
|
|
|
);
|
|
|
|
|
|
|
|
testGetExe'FailureSecondArg = testingThrow (
|
|
|
|
getExe' { type = "derivation"; } "dir/executable"
|
|
|
|
);
|
lib.meta: Avoid attrset allocation in platformMatch
Benchmarks (`nix-instantiate ./. -A python3`)
- Before
``` json
{
"cpuTime": 0.30625399947166443,
"envs": {
"bytes": 4484216,
"elements": 221443,
"number": 169542
},
"gc": {
"heapSize": 402915328,
"totalBytes": 53091024
},
"list": {
"bytes": 749424,
"concats": 4242,
"elements": 93678
},
"nrAvoided": 253991,
"nrFunctionCalls": 149848,
"nrLookups": 49614,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 130356,
"nrThunks": 359013,
"sets": {
"bytes": 30432320,
"elements": 1860540,
"number": 41480
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 10504632,
"number": 437693
}
}
```
- After
```
{
"cpuTime": 0.29695799946784973,
"envs": {
"bytes": 3296712,
"elements": 169055,
"number": 121517
},
"gc": {
"heapSize": 402915328,
"totalBytes": 49044992
},
"list": {
"bytes": 504928,
"concats": 4242,
"elements": 63116
},
"nrAvoided": 175403,
"nrFunctionCalls": 110554,
"nrLookups": 44907,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 82330,
"nrThunks": 306625,
"sets": {
"bytes": 29943328,
"elements": 1843076,
"number": 28382
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 9037752,
"number": 376573
}
}
```
2023-11-24 12:58:52 +00:00
|
|
|
|
2024-06-26 00:42:44 +00:00
|
|
|
testGetLicenseFromSpdxIdOrExamples = {
|
|
|
|
expr = [
|
|
|
|
(getLicenseFromSpdxIdOr "MIT" null)
|
|
|
|
(getLicenseFromSpdxIdOr "mIt" null)
|
|
|
|
(getLicenseFromSpdxIdOr "MY LICENSE" lib.licenses.free)
|
|
|
|
(getLicenseFromSpdxIdOr "MY LICENSE" null)
|
|
|
|
];
|
|
|
|
expected = [
|
|
|
|
lib.licenses.mit
|
|
|
|
lib.licenses.mit
|
|
|
|
lib.licenses.free
|
|
|
|
null
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
testGetLicenseFromSpdxIdOrThrow = testingThrow (
|
|
|
|
getLicenseFromSpdxIdOr "MY LICENSE" (throw "No SPDX ID matches MY LICENSE")
|
|
|
|
);
|
|
|
|
|
lib.meta: Avoid attrset allocation in platformMatch
Benchmarks (`nix-instantiate ./. -A python3`)
- Before
``` json
{
"cpuTime": 0.30625399947166443,
"envs": {
"bytes": 4484216,
"elements": 221443,
"number": 169542
},
"gc": {
"heapSize": 402915328,
"totalBytes": 53091024
},
"list": {
"bytes": 749424,
"concats": 4242,
"elements": 93678
},
"nrAvoided": 253991,
"nrFunctionCalls": 149848,
"nrLookups": 49614,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 130356,
"nrThunks": 359013,
"sets": {
"bytes": 30432320,
"elements": 1860540,
"number": 41480
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 10504632,
"number": 437693
}
}
```
- After
```
{
"cpuTime": 0.29695799946784973,
"envs": {
"bytes": 3296712,
"elements": 169055,
"number": 121517
},
"gc": {
"heapSize": 402915328,
"totalBytes": 49044992
},
"list": {
"bytes": 504928,
"concats": 4242,
"elements": 63116
},
"nrAvoided": 175403,
"nrFunctionCalls": 110554,
"nrLookups": 44907,
"nrOpUpdateValuesCopied": 1588326,
"nrOpUpdates": 10106,
"nrPrimOpCalls": 82330,
"nrThunks": 306625,
"sets": {
"bytes": 29943328,
"elements": 1843076,
"number": 28382
},
"sizes": {
"Attr": 16,
"Bindings": 16,
"Env": 16,
"Value": 24
},
"symbols": {
"bytes": 236218,
"number": 24459
},
"values": {
"bytes": 9037752,
"number": 376573
}
}
```
2023-11-24 12:58:52 +00:00
|
|
|
testPlatformMatch = {
|
|
|
|
expr = meta.platformMatch { system = "x86_64-linux"; } "x86_64-linux";
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testPlatformMatchAttrs = {
|
|
|
|
expr = meta.platformMatch (systems.elaborate "x86_64-linux") (systems.elaborate "x86_64-linux").parsed;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testPlatformMatchNoMatch = {
|
|
|
|
expr = meta.platformMatch { system = "x86_64-darwin"; } "x86_64-linux";
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
testPlatformMatchMissingSystem = {
|
|
|
|
expr = meta.platformMatch { } "x86_64-linux";
|
|
|
|
expected = false;
|
|
|
|
};
|
2023-11-28 01:14:36 +00:00
|
|
|
|
|
|
|
testPackagesFromDirectoryRecursive = {
|
|
|
|
expr = packagesFromDirectoryRecursive {
|
|
|
|
callPackage = path: overrides: import path overrides;
|
|
|
|
directory = ./packages-from-directory;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
a = "a";
|
|
|
|
b = "b";
|
|
|
|
# Note: Other files/directories in `./test-data/c/` are ignored and can be
|
|
|
|
# used by `package.nix`.
|
|
|
|
c = "c";
|
|
|
|
my-namespace = {
|
|
|
|
d = "d";
|
|
|
|
e = "e";
|
|
|
|
f = "f";
|
|
|
|
my-sub-namespace = {
|
|
|
|
g = "g";
|
|
|
|
h = "h";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Check that `packagesFromDirectoryRecursive` can process a directory with a
|
|
|
|
# top-level `package.nix` file into a single package.
|
|
|
|
testPackagesFromDirectoryRecursiveTopLevelPackageNix = {
|
|
|
|
expr = packagesFromDirectoryRecursive {
|
|
|
|
callPackage = path: overrides: import path overrides;
|
|
|
|
directory = ./packages-from-directory/c;
|
|
|
|
};
|
|
|
|
expected = "c";
|
|
|
|
};
|
2009-03-31 13:03:50 +00:00
|
|
|
}
|