diff options
author | Silvan Mosberger <contact@infinisil.com> | 2020-03-26 02:56:07 +0100 |
---|---|---|
committer | Silvan Mosberger <contact@infinisil.com> | 2020-07-29 18:08:25 +0200 |
commit | 9c1565a042ecd2f03b800f14f3cdc5ba0d464b5d (patch) | |
tree | 9d4e9d0d7f44764ba41194ab6abb7891970f5a1d /pkgs/pkgs-lib | |
parent | b6c540a87cf4cdf5c37cc7975f1afb0c07f8106c (diff) |
pkgs-lib: Add tests for formats
Diffstat (limited to 'pkgs/pkgs-lib')
-rw-r--r-- | pkgs/pkgs-lib/tests/default.nix | 2 | ||||
-rw-r--r-- | pkgs/pkgs-lib/tests/formats.nix | 157 |
2 files changed, 159 insertions, 0 deletions
diff --git a/pkgs/pkgs-lib/tests/default.nix b/pkgs/pkgs-lib/tests/default.nix index ee71596a1456..f3549ea9b0f2 100644 --- a/pkgs/pkgs-lib/tests/default.nix +++ b/pkgs/pkgs-lib/tests/default.nix @@ -1,5 +1,7 @@ # Call nix-build on this file to run all tests in this directory { pkgs ? import ../../.. {} }: let + formats = import ./formats.nix { inherit pkgs; }; in pkgs.linkFarm "nixpkgs-pkgs-lib-tests" [ + { name = "formats"; path = import ./formats.nix { inherit pkgs; }; } ] diff --git a/pkgs/pkgs-lib/tests/formats.nix b/pkgs/pkgs-lib/tests/formats.nix new file mode 100644 index 000000000000..bf6be8595e1b --- /dev/null +++ b/pkgs/pkgs-lib/tests/formats.nix @@ -0,0 +1,157 @@ +{ pkgs }: +let + inherit (pkgs) lib formats; +in +with lib; +let + + evalFormat = format: args: def: + let + formatSet = format args; + config = formatSet.type.merge [] (imap1 (n: def: { + value = def; + file = "def${toString n}"; + }) [ def ]); + in formatSet.generate "test-format-file" config; + + runBuildTest = name: { drv, expected }: pkgs.runCommandNoCC name {} '' + if diff ${drv} ${builtins.toFile "expected" expected}; then + touch $out + else + echo "Got: $(cat ${drv})" + echo "Should be: ${expected}" + exit 1 + fi + ''; + + runBuildTests = tests: pkgs.linkFarm "nixpkgs-pkgs-lib-format-tests" (mapAttrsToList (name: value: { inherit name; path = runBuildTest name value; }) (filterAttrs (name: value: value != null) tests)); + +in runBuildTests { + + testJsonAtoms = { + drv = evalFormat formats.json {} { + null = null; + false = false; + true = true; + int = 10; + float = 3.141; + str = "foo"; + attrs.foo = null; + list = [ null null ]; + }; + expected = '' + { + "attrs": { + "foo": null + }, + "false": false, + "float": 3.141, + "int": 10, + "list": [ + null, + null + ], + "null": null, + "str": "foo", + "true": true + } + ''; + }; + + testYamlAtoms = { + drv = evalFormat formats.yaml {} { + null = null; + false = false; + true = true; + float = 3.141; + str = "foo"; + attrs.foo = null; + list = [ null null ]; + }; + expected = '' + { + "attrs": { + "foo": null + }, + "false": false, + "float": 3.141, + "list": [ + null, + null + ], + "null": null, + "str": "foo", + "true": true + } + ''; + }; + + testIniAtoms = { + drv = evalFormat formats.ini {} { + foo = { + bool = true; + int = 10; + float = 3.141; + str = "string"; + }; + }; + expected = '' + [foo] + bool=true + float=3.141000 + int=10 + str=string + ''; + }; + + testIniDuplicateKeys = { + drv = evalFormat formats.ini { listsAsDuplicateKeys = true; } { + foo = { + bar = [ null true "test" 1.2 10 ]; + baz = false; + qux = "qux"; + }; + }; + expected = '' + [foo] + bar=null + bar=true + bar=test + bar=1.200000 + bar=10 + baz=false + qux=qux + ''; + }; + + testTomlAtoms = { + drv = evalFormat formats.toml {} { + false = false; + true = true; + int = 10; + float = 3.141; + str = "foo"; + attrs.foo = "foo"; + list = [ 1 2 ]; + level1.level2.level3.level4 = "deep"; + }; + expected = '' + false = false + float = 3.141 + int = 10 + list = [1, 2] + str = "foo" + true = true + + [attrs] + foo = "foo" + + [level1] + + [level1.level2] + + [level1.level2.level3] + level4 = "deep" + ''; + }; +} |