Files
clan-core/lib/jsonschema/test_parseOptions.nix
2025-09-16 17:11:19 +02:00

97 lines
2.2 KiB
Nix

# tests for the nixos options to jsonschema converter
# run these tests via `nix-unit ./test.nix`
{
lib ? (import <nixpkgs> { }).lib,
slib ? (import ./. { inherit lib; } { }),
}:
{
testParseOptions = {
expr = (slib.parseModule ./example-interface.nix);
expected = builtins.fromJSON (builtins.readFile ./example-schema.json);
};
testParseNestedOptions =
let
evaled = lib.evalModules {
modules = [
{
options.foo.bar = lib.mkOption {
type = lib.types.bool;
};
options.foo.baz = lib.mkOption {
type = lib.types.bool;
default = false;
};
}
];
};
in
{
expr = (slib.parseOptions evaled.options { });
expected = {
"$schema" = "http://json-schema.org/draft-07/schema#";
additionalProperties = false;
properties = {
foo = {
additionalProperties = false;
properties = {
bar = {
type = "boolean";
};
baz = {
type = "boolean";
default = false;
};
};
required = [ "bar" ];
type = "object";
};
};
type = "object";
required = [ "foo" ];
};
};
testFreeFormOfInt =
let
default = {
foo = 1;
bar = 2;
};
in
{
expr = (
slib.parseOptions
(lib.evalModules {
modules = [
{
freeformType = with lib.types; attrsOf int;
options = {
enable = lib.mkEnableOption "enable this";
};
}
default
];
}).options
{ }
);
expected = {
"$schema" = "http://json-schema.org/draft-07/schema#";
additionalProperties = {
type = "integer";
};
properties = {
enable = {
default = false;
description = "Whether to enable enable this.";
examples = [ true ];
type = "boolean";
};
};
required = [ ];
type = "object";
};
};
}