clan-config: improve jsonschema arg parsing

- output json to stdout after success
- expect args in the style: `foo.bar = baz`
- handle different input types
- cast input types on best effort basis
- throw meaningful errors
This commit is contained in:
DavHau
2023-08-11 00:05:52 +02:00
parent d04278e9b1
commit 0a9b914ad5
4 changed files with 218 additions and 93 deletions

View File

@@ -1,4 +1,6 @@
import argparse
import json
import sys
from pathlib import Path
from typing import Any
@@ -6,15 +8,12 @@ import pytest
from clan_cli import config
base_args = [
"",
f"{Path(config.__file__).parent}/jsonschema/example-schema.json",
]
example_schema = f"{Path(config.__file__).parent}/jsonschema/example-schema.json"
# use pytest.parametrize
@pytest.mark.parametrize(
"args,expected",
"argv,expected",
[
(["name", "DavHau"], {"name": "DavHau"}),
(
@@ -26,11 +25,85 @@ base_args = [
],
)
def test_set_some_option(
args: list[str],
argv: list[str],
expected: dict[str, Any],
capsys: pytest.CaptureFixture,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config.main(base_args + args)
captured = capsys.readout()
# monkeypatch sys.argv
monkeypatch.setattr(sys, "argv", [""] + argv)
parser = argparse.ArgumentParser()
config.register_parser(parser=parser, file=Path(example_schema))
args = parser.parse_args()
args.func(args)
captured = capsys.readouterr()
print(captured.out)
json_out = json.loads(captured.out)
assert json_out == expected
def test_walk_jsonschema_all_types() -> None:
schema = dict(
type="object",
properties=dict(
array=dict(
type="array",
items=dict(
type="string",
),
),
boolean=dict(type="boolean"),
integer=dict(type="integer"),
number=dict(type="number"),
string=dict(type="string"),
),
)
expected = {
"array": list[str],
"boolean": bool,
"integer": int,
"number": float,
"string": str,
}
assert config.options_types_from_schema(schema) == expected
def test_walk_jsonschema_nested() -> None:
schema = dict(
type="object",
properties=dict(
name=dict(
type="object",
properties=dict(
first=dict(type="string"),
last=dict(type="string"),
),
),
age=dict(type="integer"),
),
)
expected = {
"age": int,
"name.first": str,
"name.last": str,
}
assert config.options_types_from_schema(schema) == expected
# test walk_jsonschema with dynamic attributes (e.g. "additionalProperties")
def test_walk_jsonschema_dynamic_attrs() -> None:
schema = dict(
type="object",
properties=dict(
age=dict(type="integer"),
users=dict(
type="object",
additionalProperties=dict(type="string"),
),
),
)
expected = {
"age": int,
"users.<name>": str, # <name> is a placeholder for any string
}
assert config.options_types_from_schema(schema) == expected