diff options
author | Mx Kookie <kookie@spacekookie.de> | 2020-12-21 06:05:12 +0100 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2020-12-21 06:05:12 +0100 |
commit | f107be784e6d5da5f90735765a68fdff96acfbb4 (patch) | |
tree | 145573a598009fb6adbd5ef7fbce0a850681f5f0 /infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests | |
parent | 2e04b35e5ac3a9123cafffbc84494fa4d389cca0 (diff) | |
parent | e9158eca70ae59e73fae23be5d13d3fa0cfc78b4 (diff) |
Add 'infra/libkookie/nixpkgs/' from commit 'e9158eca70ae59e73fae23be5d13d3fa0cfc78b4'
git-subtree-dir: infra/libkookie/nixpkgs
git-subtree-mainline: 2e04b35e5ac3a9123cafffbc84494fa4d389cca0
git-subtree-split: e9158eca70ae59e73fae23be5d13d3fa0cfc78b4
Diffstat (limited to 'infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests')
-rw-r--r-- | infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/default.nix | 7 | ||||
-rw-r--r-- | infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/formats.nix | 155 |
2 files changed, 162 insertions, 0 deletions
diff --git a/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/default.nix b/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/default.nix new file mode 100644 index 000000000000..f3549ea9b0f2 --- /dev/null +++ b/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/default.nix @@ -0,0 +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/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/formats.nix b/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/formats.nix new file mode 100644 index 000000000000..16b760a5ada1 --- /dev/null +++ b/infra/libkookie/nixpkgs/pkgs/pkgs-lib/tests/formats.nix @@ -0,0 +1,155 @@ +{ 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 -u '${builtins.toFile "expected" expected}' '${drv}'; then + touch "$out" + else + echo + echo "Got different values than expected; diff above." + 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" + ''; + }; +} |