3 inherit (pkgs) lib formats;
5 # merging allows us to add metadata to the input
6 # this makes error messages more readable during development
7 mergeInput = name: format: input:
10 # explicitly throw here to trigger the code path that prints the error message for users
11 value = lib.throwIfNot (format.type.check input) (builtins.trace input "definition does not pass the type's check function") input;
13 file = "format-test-${name}";
17 # run a diff between expected and real output
18 runDiff = name: drv: expected: pkgs.runCommand name {
19 passAsFile = ["expected"];
22 if diff -u "$expectedPath" "$drv"; then
26 echo "Got different values than expected; diff above."
31 # use this to check for proper serialization
32 # in practice you do not have to supply the name parameter as this one will be added by runBuildTests
33 shouldPass = { format, input, expected }: name: {
34 name = "pass-${name}";
35 path = runDiff "test-format-${name}" (format.generate "test-format-${name}" (mergeInput name format input)) expected;
38 # use this function to assert that a type check must fail
39 # in practice you do not have to supply the name parameter as this one will be added by runBuildTests
40 # note that as per 352e7d330a26 and 352e7d330a26 the type checking of attrsets and lists are not strict
41 # this means that the code below needs to properly merge the module type definition and also evaluate the (lazy) return value
42 shouldFail = { format, input }: name:
44 # trigger a deep type check using the module system
45 typeCheck = lib.modules.mergeDefinitions
50 file = "format-test-${name}";
54 # actually use the return value to trigger the evaluation
55 eval = builtins.tryEval (typeCheck.mergedValue == input);
56 # the check failing is what we want, so don't do anything here
57 typeFails = pkgs.runCommand "test-format-${name}" {} "touch $out";
58 # bail with some verbose information in case the type check passes
59 typeSucceeds = pkgs.runCommand "test-format-${name}" {
60 passAsFile = [ "inputText" ];
62 # this will fail if the input contains functions as values
63 # however that should get caught by the type check already
64 inputText = builtins.toJSON input;
67 echo "Type check $testName passed when it shouldn't."
68 echo "The following data was used as input:"
74 name = "fail-${name}";
75 path = if eval.success then typeSucceeds else typeFails;
78 # this function creates a linkFarm for all the tests below such that the results are easily visible in the filesystem after a build
79 # the parameters are an attrset of name: test pairs where the name is automatically passed to the test
80 # the test therefore is an invocation of ShouldPass or shouldFail with the attrset parameters but *not* the name (which this adds for convenience)
81 runBuildTests = (lib.flip lib.pipe) [
82 (lib.mapAttrsToList (name: value: value name))
83 (pkgs.linkFarm "nixpkgs-pkgs-lib-format-tests")
88 jsonAtoms = shouldPass {
89 format = formats.json {};
114 "path": "${./formats.nix}",
121 yamlAtoms = shouldPass {
122 format = formats.yaml {};
130 list = [ null null ];
131 path = ./formats.nix;
142 path: ${./formats.nix}
148 iniAtoms = shouldPass {
149 format = formats.ini {};
167 iniInvalidAtom = shouldFail {
168 format = formats.ini {};
176 iniDuplicateKeysWithoutList = shouldFail {
177 format = formats.ini {};
180 bar = [ null true "test" 1.2 10 ];
187 iniDuplicateKeys = shouldPass {
188 format = formats.ini { listsAsDuplicateKeys = true; };
191 bar = [ null true "test" 1.2 10 ];
208 iniListToValue = shouldPass {
209 format = formats.ini { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); };
212 bar = [ null true "test" 1.2 10 ];
219 bar=null, true, test, 1.200000, 10
225 iniWithGlobalNoSections = shouldPass {
226 format = formats.iniWithGlobalSection {};
231 iniWithGlobalOnlySections = shouldPass {
232 format = formats.iniWithGlobalSection {};
246 iniWithGlobalOnlyGlobal = shouldPass {
247 format = formats.iniWithGlobalSection {};
259 iniWithGlobalWrongSections = shouldFail {
260 format = formats.iniWithGlobalSection {};
266 iniWithGlobalEverything = shouldPass {
267 format = formats.iniWithGlobalSection {};
292 iniWithGlobalListToValue = shouldPass {
293 format = formats.iniWithGlobalSection { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); };
296 bar = [ null true "test" 1.2 10 ];
302 bar = [ null true "test" 1.2 10 ];
309 bar=null, true, test, 1.200000, 10
314 bar=null, true, test, 1.200000, 10
320 keyValueAtoms = shouldPass {
321 format = formats.keyValue {};
336 keyValueDuplicateKeys = shouldPass {
337 format = formats.keyValue { listsAsDuplicateKeys = true; };
339 bar = [ null true "test" 1.2 10 ];
354 keyValueListToValue = shouldPass {
355 format = formats.keyValue { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); };
357 bar = [ null true "test" 1.2 10 ];
362 bar=null, true, test, 1.200000, 10
368 tomlAtoms = shouldPass {
369 format = formats.toml {};
378 level1.level2.level3.level4 = "deep";
390 [level1.level2.level3]
395 # This test is responsible for
396 # 1. testing type coercions
397 # 2. providing a more readable example test
398 # Whereas java-properties/default.nix tests the low level escaping, etc.
399 javaProperties = shouldPass {
400 format = formats.javaProperties {};
404 contradictions = false;
406 # # Disallowed at eval time, because it's ambiguous:
407 # # add to store or convert to string?
410 package = pkgs.hello;
412 # NB: Some editors (vscode) show this _whole_ line in right-to-left order
413 "الجبر" = "أكثر من مجرد أرقام";
419 contradictions = false
422 package = ${pkgs.hello}
424 \u00fctf\ 8 = d\u00fbh
425 \u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645
429 phpAtoms = shouldPass rec {
430 format = formats.php { finalVariable = "config"; };
438 str_special = "foo\ntesthello'''";
440 list = [ null null ];
441 mixed = format.lib.mkMixedArray [ 10 3.141 ] {
445 raw = format.lib.mkRaw "random_function()";
449 declare(strict_types=1);
450 $config = ['attrs' => ['foo' => null], 'false' => false, 'float' => 3.141000, 'int' => 10, 'list' => [null, null], 'mixed' => [10, 3.141000, 'attrs' => ['foo' => null], 'str' => 'foo'], 'null' => null, 'raw' => random_function(), 'str' => 'foo', 'str_special' => 'foo
451 testhello\'\'\'${"'"}, 'true' => true];
455 phpReturn = shouldPass {
456 format = formats.php { };
461 str_special = "foo\ntesthello'''";
466 declare(strict_types=1);
467 return ['attrs' => ['foo' => null], 'float' => 3.141000, 'int' => 10, 'str' => 'foo', 'str_special' => 'foo
468 testhello\'\'\'${"'"}];