2023-08-02 18:04:16 +00:00
|
|
|
# tests for the nixos options to jsonschema converter
|
|
|
|
# run these tests via `nix-unit ./test.nix`
|
2024-03-17 18:48:49 +00:00
|
|
|
{
|
|
|
|
lib ? (import <nixpkgs> { }).lib,
|
|
|
|
slib ? import ./. { inherit lib; },
|
2023-08-02 18:04:16 +00:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
description = "Test Description";
|
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
evalType =
|
|
|
|
type: default:
|
2023-08-02 18:04:16 +00:00
|
|
|
let
|
|
|
|
evaledConfig = lib.evalModules {
|
2024-03-17 18:48:49 +00:00
|
|
|
modules = [
|
|
|
|
{
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
inherit type;
|
|
|
|
inherit default;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
2023-08-02 18:04:16 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
evaledConfig.options.opt;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
testNoDefaultNoDescription =
|
|
|
|
let
|
|
|
|
evaledConfig = lib.evalModules {
|
2024-03-17 18:48:49 +00:00
|
|
|
modules = [ { options.opt = lib.mkOption { type = lib.types.bool; }; } ];
|
2023-08-02 18:04:16 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption evaledConfig.options.opt;
|
|
|
|
expected = {
|
|
|
|
type = "boolean";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-21 10:29:06 +00:00
|
|
|
testDescriptionIsAttrs =
|
|
|
|
let
|
|
|
|
evaledConfig = lib.evalModules {
|
2024-03-17 18:48:49 +00:00
|
|
|
modules = [
|
|
|
|
{
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = {
|
|
|
|
_type = "mdDoc";
|
|
|
|
text = description;
|
|
|
|
};
|
2023-11-21 10:29:06 +00:00
|
|
|
};
|
2024-03-17 18:48:49 +00:00
|
|
|
}
|
|
|
|
];
|
2023-11-21 10:29:06 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption evaledConfig.options.opt;
|
|
|
|
expected = {
|
|
|
|
type = "boolean";
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-02 18:04:16 +00:00
|
|
|
testBool =
|
|
|
|
let
|
|
|
|
default = false;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.bool default);
|
|
|
|
expected = {
|
|
|
|
type = "boolean";
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testString =
|
|
|
|
let
|
|
|
|
default = "hello";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.str default);
|
|
|
|
expected = {
|
|
|
|
type = "string";
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testInteger =
|
|
|
|
let
|
|
|
|
default = 42;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.int default);
|
|
|
|
expected = {
|
|
|
|
type = "integer";
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testFloat =
|
|
|
|
let
|
|
|
|
default = 42.42;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.float default);
|
|
|
|
expected = {
|
|
|
|
type = "number";
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testEnum =
|
|
|
|
let
|
|
|
|
default = "foo";
|
2024-03-17 18:48:49 +00:00
|
|
|
values = [
|
|
|
|
"foo"
|
|
|
|
"bar"
|
|
|
|
"baz"
|
|
|
|
];
|
2023-08-02 18:04:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.enum values) default);
|
|
|
|
expected = {
|
|
|
|
enum = values;
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testListOfInt =
|
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
default = [
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
];
|
2023-08-02 18:04:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.listOf lib.types.int) default);
|
|
|
|
expected = {
|
|
|
|
type = "array";
|
|
|
|
items = {
|
|
|
|
type = "integer";
|
|
|
|
};
|
|
|
|
inherit default description;
|
2023-11-20 10:01:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-20 12:40:08 +00:00
|
|
|
testListOfUnspecified =
|
2023-11-20 10:01:16 +00:00
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
default = [
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
];
|
2023-11-20 10:01:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default);
|
|
|
|
expected = {
|
|
|
|
type = "array";
|
2023-11-20 12:40:08 +00:00
|
|
|
items = {
|
2024-03-17 18:48:49 +00:00
|
|
|
type = [
|
|
|
|
"boolean"
|
|
|
|
"integer"
|
|
|
|
"number"
|
|
|
|
"string"
|
|
|
|
"array"
|
|
|
|
"object"
|
|
|
|
"null"
|
|
|
|
];
|
2023-11-20 12:40:08 +00:00
|
|
|
};
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testAttrs =
|
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
default = {
|
|
|
|
foo = 1;
|
|
|
|
bar = 2;
|
|
|
|
baz = 3;
|
|
|
|
};
|
2023-11-20 12:40:08 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.attrs) default);
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
additionalProperties = true;
|
2023-11-20 10:01:16 +00:00
|
|
|
inherit default description;
|
2023-08-02 18:04:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testAttrsOfInt =
|
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
default = {
|
|
|
|
foo = 1;
|
|
|
|
bar = 2;
|
|
|
|
baz = 3;
|
|
|
|
};
|
2023-08-02 18:04:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.attrsOf lib.types.int) default);
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
additionalProperties = {
|
|
|
|
type = "integer";
|
|
|
|
};
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-20 12:40:08 +00:00
|
|
|
testLazyAttrsOfInt =
|
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
default = {
|
|
|
|
foo = 1;
|
|
|
|
bar = 2;
|
|
|
|
baz = 3;
|
|
|
|
};
|
2023-11-20 12:40:08 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default);
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
additionalProperties = {
|
|
|
|
type = "integer";
|
|
|
|
};
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-02 18:04:16 +00:00
|
|
|
testNullOrBool =
|
|
|
|
let
|
|
|
|
default = null; # null is a valid value for this type
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default);
|
|
|
|
expected = {
|
2023-11-20 12:40:08 +00:00
|
|
|
anyOf = [
|
|
|
|
{ type = "null"; }
|
|
|
|
{ type = "boolean"; }
|
|
|
|
];
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testNullOrNullOr =
|
|
|
|
let
|
|
|
|
default = null; # null is a valid value for this type
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
|
|
|
|
expected = {
|
|
|
|
anyOf = [
|
|
|
|
{ type = "null"; }
|
|
|
|
{
|
|
|
|
anyOf = [
|
|
|
|
{ type = "null"; }
|
|
|
|
{ type = "boolean"; }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
2023-08-02 18:04:16 +00:00
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testSubmoduleOption =
|
|
|
|
let
|
|
|
|
subModule = {
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
properties = {
|
|
|
|
opt = {
|
|
|
|
type = "boolean";
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testSubmoduleOptionWithoutDefault =
|
|
|
|
let
|
|
|
|
subModule = {
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
properties = {
|
|
|
|
opt = {
|
|
|
|
type = "boolean";
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
required = [ "opt" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testAttrsOfSubmodule =
|
|
|
|
let
|
|
|
|
subModule = {
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
2024-03-17 18:48:49 +00:00
|
|
|
default = {
|
|
|
|
foo.opt = false;
|
|
|
|
bar.opt = true;
|
|
|
|
};
|
2023-08-02 18:04:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default);
|
|
|
|
expected = {
|
|
|
|
type = "object";
|
|
|
|
additionalProperties = {
|
|
|
|
type = "object";
|
|
|
|
properties = {
|
|
|
|
opt = {
|
|
|
|
type = "boolean";
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testListOfSubmodule =
|
|
|
|
let
|
|
|
|
subModule = {
|
|
|
|
options.opt = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
2024-03-17 18:48:49 +00:00
|
|
|
default = [
|
|
|
|
{ opt = false; }
|
|
|
|
{ opt = true; }
|
|
|
|
];
|
2023-08-02 18:04:16 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default);
|
|
|
|
expected = {
|
|
|
|
type = "array";
|
|
|
|
items = {
|
|
|
|
type = "object";
|
|
|
|
properties = {
|
|
|
|
opt = {
|
|
|
|
type = "boolean";
|
|
|
|
default = true;
|
|
|
|
inherit description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
2023-11-20 12:40:08 +00:00
|
|
|
|
|
|
|
testEither =
|
|
|
|
let
|
|
|
|
default = "foo";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
|
|
|
|
expected = {
|
|
|
|
anyOf = [
|
|
|
|
{ type = "boolean"; }
|
|
|
|
{ type = "string"; }
|
|
|
|
];
|
|
|
|
inherit default description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testAnything =
|
|
|
|
let
|
|
|
|
default = "foo";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.anything default);
|
|
|
|
expected = {
|
|
|
|
inherit default description;
|
2024-03-17 18:48:49 +00:00
|
|
|
type = [
|
|
|
|
"boolean"
|
|
|
|
"integer"
|
|
|
|
"number"
|
|
|
|
"string"
|
|
|
|
"array"
|
|
|
|
"object"
|
|
|
|
"null"
|
|
|
|
];
|
2023-11-20 12:40:08 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testUnspecified =
|
|
|
|
let
|
|
|
|
default = "foo";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.unspecified default);
|
|
|
|
expected = {
|
|
|
|
inherit default description;
|
2024-03-17 18:48:49 +00:00
|
|
|
type = [
|
|
|
|
"boolean"
|
|
|
|
"integer"
|
|
|
|
"number"
|
|
|
|
"string"
|
|
|
|
"array"
|
|
|
|
"object"
|
|
|
|
"null"
|
|
|
|
];
|
2023-11-20 12:40:08 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testRaw =
|
|
|
|
let
|
|
|
|
default = "foo";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
expr = slib.parseOption (evalType lib.types.raw default);
|
|
|
|
expected = {
|
|
|
|
inherit default description;
|
2024-03-17 18:48:49 +00:00
|
|
|
type = [
|
|
|
|
"boolean"
|
|
|
|
"integer"
|
|
|
|
"number"
|
|
|
|
"string"
|
|
|
|
"array"
|
|
|
|
"object"
|
|
|
|
"null"
|
|
|
|
];
|
2023-11-20 12:40:08 +00:00
|
|
|
};
|
|
|
|
};
|
2023-08-02 18:04:16 +00:00
|
|
|
}
|