jsonschema: migrate tests

This commit is contained in:
Johannes Kirschbauer
2024-11-12 13:23:31 +01:00
parent 499bc4743b
commit b9ef080950
5 changed files with 144 additions and 28 deletions

View File

@@ -108,7 +108,7 @@ rec {
# Can be customized if needed # Can be customized if needed
# By default the header is not added to the schema # By default the header is not added to the schema
addHeader ? true, addHeader ? true,
path ? [ "<root>" ], path ? [ ],
}: }:
let let
options' = filterInvisibleOpts (filterExcludedAttrs (clean options)); options' = filterInvisibleOpts (filterExcludedAttrs (clean options));

View File

@@ -1,32 +1,39 @@
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"$exportedModuleInfo": { "path": [] },
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"name": { "name": {
"$exportedModuleInfo": { "path": ["name"] },
"type": "string", "type": "string",
"default": "John Doe", "default": "John Doe",
"description": "The name of the user" "description": "The name of the user"
}, },
"age": { "age": {
"$exportedModuleInfo": { "path": ["age"] },
"type": "integer", "type": "integer",
"default": 42, "default": 42,
"description": "The age of the user" "description": "The age of the user"
}, },
"isAdmin": { "isAdmin": {
"$exportedModuleInfo": { "path": ["isAdmin"] },
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Is the user an admin?" "description": "Is the user an admin?"
}, },
"kernelModules": { "kernelModules": {
"$exportedModuleInfo": { "path": ["kernelModules"] },
"type": "array", "type": "array",
"items": { "items": {
"$exportedModuleInfo": { "path": ["kernelModules"] },
"type": "string" "type": "string"
}, },
"default": ["nvme", "xhci_pci", "ahci"], "default": ["nvme", "xhci_pci", "ahci"],
"description": "A list of enabled kernel modules" "description": "A list of enabled kernel modules"
}, },
"userIds": { "userIds": {
"$exportedModuleInfo": { "path": ["userIds"] },
"type": "object", "type": "object",
"default": { "default": {
"horst": 1, "horst": 1,
@@ -34,15 +41,18 @@
"albrecht": 3 "albrecht": 3
}, },
"additionalProperties": { "additionalProperties": {
"$exportedModuleInfo": { "path": ["userIds"] },
"type": "integer" "type": "integer"
}, },
"description": "Some attributes" "description": "Some attributes"
}, },
"services": { "services": {
"$exportedModuleInfo": { "path": ["services"] },
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"opt": { "opt": {
"$exportedModuleInfo": { "path": ["services", "opt"] },
"type": "string", "type": "string",
"default": "foo", "default": "foo",
"description": "A submodule option" "description": "A submodule option"
@@ -50,14 +60,22 @@
} }
}, },
"destinations": { "destinations": {
"$exportedModuleInfo": { "path": ["destinations"] },
"additionalProperties": { "additionalProperties": {
"$exportedModuleInfo": { "path": ["destinations", "<name>"] },
"properties": { "properties": {
"name": { "name": {
"$exportedModuleInfo": {
"path": ["destinations", "<name>", "name"]
},
"default": "name", "default": "name",
"description": "the name of the backup job", "description": "the name of the backup job",
"type": "string" "type": "string"
}, },
"repo": { "repo": {
"$exportedModuleInfo": {
"path": ["destinations", "<name>", "repo"]
},
"description": "the borgbackup repository to backup to", "description": "the borgbackup repository to backup to",
"type": "string" "type": "string"
} }

View File

@@ -4,6 +4,6 @@
slib ? (import ./. { inherit lib; } { }), slib ? (import ./. { inherit lib; } { }),
}: }:
{ {
parseOption = import ./test_parseOption.nix { inherit lib slib; }; # parseOption = import ./test_parseOption.nix { inherit lib slib; };
parseOptions = import ./test_parseOptions.nix { inherit lib slib; }; parseOptions = import ./test_parseOptions.nix { inherit lib slib; };
} }

View File

@@ -23,6 +23,13 @@ let
}; };
in in
evaledConfig.options.opt; evaledConfig.options.opt;
# All options should have the same path
commonModuleInfo = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
};
in in
{ {
testNoDefaultNoDescription = testNoDefaultNoDescription =
@@ -33,7 +40,7 @@ in
in in
{ {
expr = slib.parseOption evaledConfig.options.opt; expr = slib.parseOption evaledConfig.options.opt;
expected = { expected = commonModuleInfo // {
type = "boolean"; type = "boolean";
}; };
}; };
@@ -56,7 +63,7 @@ in
in in
{ {
expr = slib.parseOption evaledConfig.options.opt; expr = slib.parseOption evaledConfig.options.opt;
expected = { expected = commonModuleInfo // {
type = "boolean"; type = "boolean";
inherit description; inherit description;
}; };
@@ -68,7 +75,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.bool default); expr = slib.parseOption (evalType lib.types.bool default);
expected = { expected = commonModuleInfo // {
type = "boolean"; type = "boolean";
inherit default description; inherit default description;
}; };
@@ -80,7 +87,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.str default); expr = slib.parseOption (evalType lib.types.str default);
expected = { expected = commonModuleInfo // {
type = "string"; type = "string";
inherit default description; inherit default description;
}; };
@@ -92,7 +99,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.int default); expr = slib.parseOption (evalType lib.types.int default);
expected = { expected = commonModuleInfo // {
type = "integer"; type = "integer";
inherit default description; inherit default description;
}; };
@@ -104,7 +111,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.float default); expr = slib.parseOption (evalType lib.types.float default);
expected = { expected = commonModuleInfo // {
type = "number"; type = "number";
inherit default description; inherit default description;
}; };
@@ -121,7 +128,7 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.enum values) default); expr = slib.parseOption (evalType (lib.types.enum values) default);
expected = { expected = commonModuleInfo // {
enum = values; enum = values;
inherit default description; inherit default description;
}; };
@@ -137,10 +144,13 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf lib.types.int) default); expr = slib.parseOption (evalType (lib.types.listOf lib.types.int) default);
expected = { expected = commonModuleInfo // {
type = "array"; type = "array";
items = { items = {
type = "integer"; type = "integer";
"$exportedModuleInfo" = {
path = [ "opt" ];
};
}; };
inherit default description; inherit default description;
}; };
@@ -156,9 +166,12 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default); expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default);
expected = { expected = commonModuleInfo // {
type = "array"; type = "array";
items = { items = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = [ type = [
"boolean" "boolean"
"integer" "integer"
@@ -183,7 +196,7 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrs) default); expr = slib.parseOption (evalType (lib.types.attrs) default);
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = true; additionalProperties = true;
inherit default description; inherit default description;
@@ -200,9 +213,12 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrsOf lib.types.int) default); expr = slib.parseOption (evalType (lib.types.attrsOf lib.types.int) default);
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "integer"; type = "integer";
}; };
inherit default description; inherit default description;
@@ -219,9 +235,12 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default); expr = slib.parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default);
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "integer"; type = "integer";
}; };
inherit default description; inherit default description;
@@ -234,10 +253,15 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default); expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default);
expected = { expected = commonModuleInfo // {
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ type = "boolean"; } {
type = "boolean";
"$exportedModuleInfo" = {
path = [ "opt" ];
};
}
]; ];
inherit default description; inherit default description;
}; };
@@ -249,13 +273,21 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default); expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
expected = { expected = commonModuleInfo // {
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ type = "boolean"; } {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "boolean";
}
]; ];
} }
]; ];
@@ -275,7 +307,7 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
description = "Test Description"; description = "Test Description";
@@ -284,6 +316,12 @@ in
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"opt"
];
};
}; };
}; };
}; };
@@ -300,7 +338,7 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
description = "Test Description"; description = "Test Description";
@@ -308,6 +346,12 @@ in
opt = { opt = {
type = "boolean"; type = "boolean";
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"opt"
];
};
}; };
}; };
required = [ "opt" ]; required = [ "opt" ];
@@ -330,13 +374,26 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default); expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default);
expected = { expected = commonModuleInfo // {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [
"opt"
"<name>"
];
};
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
properties = { properties = {
opt = { opt = {
"$exportedModuleInfo" = {
path = [
"opt"
"<name>"
"opt"
];
};
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
@@ -363,9 +420,12 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default); expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default);
expected = { expected = commonModuleInfo // {
type = "array"; type = "array";
items = { items = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
properties = { properties = {
@@ -373,6 +433,13 @@ in
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"*"
"opt"
];
};
}; };
}; };
}; };
@@ -386,10 +453,20 @@ in
in in
{ {
expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default); expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
expected = { expected = commonModuleInfo // {
oneOf = [ oneOf = [
{ type = "boolean"; } {
{ type = "string"; } "$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "boolean";
}
{
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "string";
}
]; ];
inherit default description; inherit default description;
}; };
@@ -401,7 +478,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.anything default); expr = slib.parseOption (evalType lib.types.anything default);
expected = { expected = commonModuleInfo // {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"
@@ -421,7 +498,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.unspecified default); expr = slib.parseOption (evalType lib.types.unspecified default);
expected = { expected = commonModuleInfo // {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"
@@ -441,7 +518,7 @@ in
in in
{ {
expr = slib.parseOption (evalType lib.types.raw default); expr = slib.parseOption (evalType lib.types.raw default);
expected = { expected = commonModuleInfo // {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"

View File

@@ -20,12 +20,24 @@
expr = slib.parseOptions evaled.options { }; expr = slib.parseOptions evaled.options { };
expected = { expected = {
"$schema" = "http://json-schema.org/draft-07/schema#"; "$schema" = "http://json-schema.org/draft-07/schema#";
"$exportedModuleInfo" = {
path = [ ];
};
additionalProperties = false; additionalProperties = false;
properties = { properties = {
foo = { foo = {
"$exportedModuleInfo" = {
path = [ "foo" ];
};
additionalProperties = false; additionalProperties = false;
properties = { properties = {
bar = { bar = {
"$exportedModuleInfo" = {
path = [
"foo"
"bar"
];
};
type = "boolean"; type = "boolean";
}; };
}; };
@@ -58,11 +70,20 @@
}).options { }; }).options { };
expected = { expected = {
"$schema" = "http://json-schema.org/draft-07/schema#"; "$schema" = "http://json-schema.org/draft-07/schema#";
"$exportedModuleInfo" = {
path = [ ];
};
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ ];
};
type = "integer"; type = "integer";
}; };
properties = { properties = {
enable = { enable = {
"$exportedModuleInfo" = {
path = [ "enable" ];
};
default = false; default = false;
description = "Whether to enable enable this."; description = "Whether to enable enable this.";
examples = [ true ]; examples = [ true ];