2024-05-02 00:46:19 +00:00
|
|
|
|
{ pkgs }:
|
|
|
|
|
let
|
|
|
|
|
inherit (pkgs) lib formats;
|
|
|
|
|
|
|
|
|
|
# merging allows us to add metadata to the input
|
|
|
|
|
# this makes error messages more readable during development
|
2024-06-30 08:12:46 +00:00
|
|
|
|
mergeInput =
|
|
|
|
|
name: format: input:
|
|
|
|
|
format.type.merge [ ] [
|
2024-05-02 00:46:19 +00:00
|
|
|
|
{
|
|
|
|
|
# explicitly throw here to trigger the code path that prints the error message for users
|
2024-06-30 08:12:46 +00:00
|
|
|
|
value =
|
|
|
|
|
lib.throwIfNot (format.type.check input)
|
|
|
|
|
(builtins.trace input "definition does not pass the type's check function")
|
|
|
|
|
input;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
# inject the name
|
|
|
|
|
file = "format-test-${name}";
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
# run a diff between expected and real output
|
2024-06-30 08:12:46 +00:00
|
|
|
|
runDiff =
|
|
|
|
|
name: drv: expected:
|
|
|
|
|
pkgs.runCommand name
|
|
|
|
|
{
|
|
|
|
|
passAsFile = [ "expected" ];
|
|
|
|
|
inherit expected drv;
|
|
|
|
|
}
|
|
|
|
|
''
|
|
|
|
|
if diff -u "$expectedPath" "$drv"; then
|
|
|
|
|
touch "$out"
|
|
|
|
|
else
|
|
|
|
|
echo
|
|
|
|
|
echo "Got different values than expected; diff above."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
'';
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
|
|
# use this to check for proper serialization
|
|
|
|
|
# in practice you do not have to supply the name parameter as this one will be added by runBuildTests
|
2024-06-30 08:12:46 +00:00
|
|
|
|
shouldPass =
|
|
|
|
|
{
|
|
|
|
|
format,
|
|
|
|
|
input,
|
|
|
|
|
expected,
|
|
|
|
|
}:
|
|
|
|
|
name: {
|
|
|
|
|
name = "pass-${name}";
|
|
|
|
|
path = runDiff "test-format-${name}" (format.generate "test-format-${name}" (
|
|
|
|
|
mergeInput name format input
|
|
|
|
|
)) expected;
|
|
|
|
|
};
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
|
|
# use this function to assert that a type check must fail
|
|
|
|
|
# in practice you do not have to supply the name parameter as this one will be added by runBuildTests
|
|
|
|
|
# note that as per 352e7d330a26 and 352e7d330a26 the type checking of attrsets and lists are not strict
|
|
|
|
|
# this means that the code below needs to properly merge the module type definition and also evaluate the (lazy) return value
|
2024-06-30 08:12:46 +00:00
|
|
|
|
shouldFail =
|
|
|
|
|
{ format, input }:
|
|
|
|
|
name:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
let
|
|
|
|
|
# trigger a deep type check using the module system
|
2024-06-30 08:12:46 +00:00
|
|
|
|
typeCheck =
|
|
|
|
|
lib.modules.mergeDefinitions
|
|
|
|
|
[
|
|
|
|
|
"tests"
|
|
|
|
|
name
|
|
|
|
|
]
|
|
|
|
|
format.type
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
file = "format-test-${name}";
|
|
|
|
|
value = input;
|
|
|
|
|
}
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
# actually use the return value to trigger the evaluation
|
|
|
|
|
eval = builtins.tryEval (typeCheck.mergedValue == input);
|
|
|
|
|
# the check failing is what we want, so don't do anything here
|
2024-06-30 08:12:46 +00:00
|
|
|
|
typeFails = pkgs.runCommand "test-format-${name}" { } "touch $out";
|
2024-05-02 00:46:19 +00:00
|
|
|
|
# bail with some verbose information in case the type check passes
|
2024-06-30 08:12:46 +00:00
|
|
|
|
typeSucceeds =
|
|
|
|
|
pkgs.runCommand "test-format-${name}"
|
|
|
|
|
{
|
|
|
|
|
passAsFile = [ "inputText" ];
|
|
|
|
|
testName = name;
|
|
|
|
|
# this will fail if the input contains functions as values
|
|
|
|
|
# however that should get caught by the type check already
|
|
|
|
|
inputText = builtins.toJSON input;
|
|
|
|
|
}
|
|
|
|
|
''
|
|
|
|
|
echo "Type check $testName passed when it shouldn't."
|
|
|
|
|
echo "The following data was used as input:"
|
|
|
|
|
echo
|
|
|
|
|
cat "$inputTextPath"
|
|
|
|
|
exit 1
|
|
|
|
|
'';
|
|
|
|
|
in
|
|
|
|
|
{
|
2024-05-02 00:46:19 +00:00
|
|
|
|
name = "fail-${name}";
|
|
|
|
|
path = if eval.success then typeSucceeds else typeFails;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# this function creates a linkFarm for all the tests below such that the results are easily visible in the filesystem after a build
|
|
|
|
|
# the parameters are an attrset of name: test pairs where the name is automatically passed to the test
|
|
|
|
|
# the test therefore is an invocation of ShouldPass or shouldFail with the attrset parameters but *not* the name (which this adds for convenience)
|
|
|
|
|
runBuildTests = (lib.flip lib.pipe) [
|
|
|
|
|
(lib.mapAttrsToList (name: value: value name))
|
|
|
|
|
(pkgs.linkFarm "nixpkgs-pkgs-lib-format-tests")
|
|
|
|
|
];
|
|
|
|
|
|
2024-06-30 08:12:46 +00:00
|
|
|
|
in
|
|
|
|
|
runBuildTests {
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
|
|
jsonAtoms = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.json { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
null = null;
|
|
|
|
|
false = false;
|
|
|
|
|
true = true;
|
|
|
|
|
int = 10;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "foo";
|
|
|
|
|
attrs.foo = null;
|
2024-06-30 08:12:46 +00:00
|
|
|
|
list = [
|
|
|
|
|
null
|
|
|
|
|
null
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
path = ./formats.nix;
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
{
|
|
|
|
|
"attrs": {
|
|
|
|
|
"foo": null
|
|
|
|
|
},
|
|
|
|
|
"false": false,
|
|
|
|
|
"float": 3.141,
|
|
|
|
|
"int": 10,
|
|
|
|
|
"list": [
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
],
|
|
|
|
|
"null": null,
|
|
|
|
|
"path": "${./formats.nix}",
|
|
|
|
|
"str": "foo",
|
|
|
|
|
"true": true
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
yamlAtoms = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.yaml { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
null = null;
|
|
|
|
|
false = false;
|
|
|
|
|
true = true;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "foo";
|
|
|
|
|
attrs.foo = null;
|
2024-06-30 08:12:46 +00:00
|
|
|
|
list = [
|
|
|
|
|
null
|
|
|
|
|
null
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
path = ./formats.nix;
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
attrs:
|
|
|
|
|
foo: null
|
|
|
|
|
'false': false
|
|
|
|
|
float: 3.141
|
|
|
|
|
list:
|
|
|
|
|
- null
|
|
|
|
|
- null
|
|
|
|
|
'null': null
|
|
|
|
|
path: ${./formats.nix}
|
|
|
|
|
str: foo
|
|
|
|
|
'true': true
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniAtoms = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.ini { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
foo = {
|
|
|
|
|
bool = true;
|
|
|
|
|
int = 10;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "string";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
[foo]
|
|
|
|
|
bool=true
|
|
|
|
|
float=3.141000
|
|
|
|
|
int=10
|
|
|
|
|
str=string
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniInvalidAtom = shouldFail {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.ini { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
foo = {
|
|
|
|
|
function = _: 1;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniDuplicateKeysWithoutList = shouldFail {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.ini { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
foo = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniDuplicateKeys = shouldPass {
|
|
|
|
|
format = formats.ini { listsAsDuplicateKeys = true; };
|
|
|
|
|
input = {
|
|
|
|
|
foo = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
[foo]
|
|
|
|
|
bar=null
|
|
|
|
|
bar=true
|
|
|
|
|
bar=test
|
|
|
|
|
bar=1.200000
|
|
|
|
|
bar=10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniListToValue = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.ini {
|
|
|
|
|
listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault { });
|
|
|
|
|
};
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
foo = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
[foo]
|
|
|
|
|
bar=null, true, test, 1.200000, 10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalNoSections = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection { };
|
|
|
|
|
input = { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
expected = "";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalOnlySections = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
sections = {
|
|
|
|
|
foo = {
|
|
|
|
|
bar = "baz";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
[foo]
|
|
|
|
|
bar=baz
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalOnlyGlobal = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
globalSection = {
|
|
|
|
|
bar = "baz";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bar=baz
|
|
|
|
|
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalWrongSections = shouldFail {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
foo = { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalEverything = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
globalSection = {
|
|
|
|
|
bar = true;
|
|
|
|
|
};
|
|
|
|
|
sections = {
|
|
|
|
|
foo = {
|
|
|
|
|
bool = true;
|
|
|
|
|
int = 10;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "string";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bar=true
|
|
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
|
bool=true
|
|
|
|
|
float=3.141000
|
|
|
|
|
int=10
|
|
|
|
|
str=string
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
iniWithGlobalListToValue = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.iniWithGlobalSection {
|
|
|
|
|
listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault { });
|
|
|
|
|
};
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
globalSection = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
sections = {
|
|
|
|
|
foo = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bar=null, true, test, 1.200000, 10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
|
bar=null, true, test, 1.200000, 10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keyValueAtoms = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.keyValue { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
bool = true;
|
|
|
|
|
int = 10;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "string";
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bool=true
|
|
|
|
|
float=3.141000
|
|
|
|
|
int=10
|
|
|
|
|
str=string
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keyValueDuplicateKeys = shouldPass {
|
|
|
|
|
format = formats.keyValue { listsAsDuplicateKeys = true; };
|
|
|
|
|
input = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bar=null
|
|
|
|
|
bar=true
|
|
|
|
|
bar=test
|
|
|
|
|
bar=1.200000
|
|
|
|
|
bar=10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keyValueListToValue = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.keyValue {
|
|
|
|
|
listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault { });
|
|
|
|
|
};
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
bar = [
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
"test"
|
|
|
|
|
1.2
|
|
|
|
|
10
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
baz = false;
|
|
|
|
|
qux = "qux";
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
bar=null, true, test, 1.200000, 10
|
|
|
|
|
baz=false
|
|
|
|
|
qux=qux
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tomlAtoms = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.toml { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
false = false;
|
|
|
|
|
true = true;
|
|
|
|
|
int = 10;
|
|
|
|
|
float = 3.141;
|
|
|
|
|
str = "foo";
|
|
|
|
|
attrs.foo = "foo";
|
2024-06-30 08:12:46 +00:00
|
|
|
|
list = [
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
level1.level2.level3.level4 = "deep";
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
false = false
|
|
|
|
|
float = 3.141
|
|
|
|
|
int = 10
|
|
|
|
|
list = [1, 2]
|
|
|
|
|
str = "foo"
|
|
|
|
|
true = true
|
|
|
|
|
[attrs]
|
|
|
|
|
foo = "foo"
|
|
|
|
|
|
|
|
|
|
[level1.level2.level3]
|
|
|
|
|
level4 = "deep"
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# This test is responsible for
|
|
|
|
|
# 1. testing type coercions
|
|
|
|
|
# 2. providing a more readable example test
|
|
|
|
|
# Whereas java-properties/default.nix tests the low level escaping, etc.
|
|
|
|
|
javaProperties = shouldPass {
|
2024-06-30 08:12:46 +00:00
|
|
|
|
format = formats.javaProperties { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
input = {
|
|
|
|
|
floaty = 3.1415;
|
|
|
|
|
tautologies = true;
|
|
|
|
|
contradictions = false;
|
|
|
|
|
foo = "bar";
|
|
|
|
|
# # Disallowed at eval time, because it's ambiguous:
|
|
|
|
|
# # add to store or convert to string?
|
|
|
|
|
# root = /root;
|
|
|
|
|
"1" = 2;
|
|
|
|
|
package = pkgs.hello;
|
|
|
|
|
"ütf 8" = "dûh";
|
|
|
|
|
# NB: Some editors (vscode) show this _whole_ line in right-to-left order
|
|
|
|
|
"الجبر" = "أكثر من مجرد أرقام";
|
|
|
|
|
};
|
|
|
|
|
expected = ''
|
|
|
|
|
# Generated with Nix
|
|
|
|
|
|
|
|
|
|
1 = 2
|
|
|
|
|
contradictions = false
|
|
|
|
|
floaty = 3.141500
|
|
|
|
|
foo = bar
|
|
|
|
|
package = ${pkgs.hello}
|
|
|
|
|
tautologies = true
|
|
|
|
|
\u00fctf\ 8 = d\u00fbh
|
|
|
|
|
\u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
}
|