3 inherit (pkgs) lib formats;
8 evalFormat = format: args: def:
10 formatSet = format args;
11 config = formatSet.type.merge [] (imap1 (n: def: {
12 # We check the input values, so that
13 # - we don't write nonsensical tests that will impede progress
14 # - the test author has a slightly more realistic view of the
15 # final format during development.
16 value = lib.throwIfNot (formatSet.type.check def) (builtins.trace def "definition does not pass the type's check function") def;
17 file = "def${toString n}";
19 in formatSet.generate "test-format-file" config;
21 runBuildTest = name: { drv, expected }: pkgs.runCommand name {
22 passAsFile = ["expected"];
25 if diff -u "$expectedPath" "$drv"; then
29 echo "Got different values than expected; diff above."
34 runBuildTests = tests: pkgs.linkFarm "nixpkgs-pkgs-lib-format-tests" (mapAttrsToList (name: value: { inherit name; path = runBuildTest name value; }) (filterAttrs (name: value: value != null) tests));
39 drv = evalFormat formats.json {} {
63 "path": "${./formats.nix}",
71 drv = evalFormat formats.yaml {} {
90 path: ${./formats.nix}
97 drv = evalFormat formats.ini {} {
114 testIniDuplicateKeys = {
115 drv = evalFormat formats.ini { listsAsDuplicateKeys = true; } {
117 bar = [ null true "test" 1.2 10 ];
134 testIniListToValue = {
135 drv = evalFormat formats.ini { listToValue = concatMapStringsSep ", " (generators.mkValueStringDefault {}); } {
137 bar = [ null true "test" 1.2 10 ];
144 bar=null, true, test, 1.200000, 10
150 testKeyValueAtoms = {
151 drv = evalFormat formats.keyValue {} {
165 testKeyValueDuplicateKeys = {
166 drv = evalFormat formats.keyValue { listsAsDuplicateKeys = true; } {
167 bar = [ null true "test" 1.2 10 ];
182 testKeyValueListToValue = {
183 drv = evalFormat formats.keyValue { listToValue = concatMapStringsSep ", " (generators.mkValueStringDefault {}); } {
184 bar = [ null true "test" 1.2 10 ];
189 bar=null, true, test, 1.200000, 10
196 drv = evalFormat formats.toml {} {
204 level1.level2.level3.level4 = "deep";
216 [level1.level2.level3]
221 # This test is responsible for
222 # 1. testing type coercions
223 # 2. providing a more readable example test
224 # Whereas java-properties/default.nix tests the low level escaping, etc.
225 testJavaProperties = {
226 drv = evalFormat formats.javaProperties {} {
229 contradictions = false;
231 # # Disallowed at eval time, because it's ambiguous:
232 # # add to store or convert to string?
235 package = pkgs.hello;
237 # NB: Some editors (vscode) show this _whole_ line in right-to-left order
238 "الجبر" = "أكثر من مجرد أرقام";
244 contradictions = false
247 package = ${pkgs.hello}
249 \u00fctf\ 8 = d\u00fbh
250 \u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645