re-enable config subcommand if CLAN_OPTIONS_FILE is set

This commit is contained in:
Jörg Thalheim
2023-08-24 17:53:33 +02:00
parent dcbf1613fb
commit 6adfadd8b9
2 changed files with 12 additions and 20 deletions

View File

@@ -1,11 +1,10 @@
import argparse import argparse
import os
import sys import sys
from types import ModuleType from types import ModuleType
from typing import Optional from typing import Optional
from . import admin, machines, secrets, webui from . import admin, config, machines, secrets, webui
# from . import admin, config, secrets, update, webui
from .errors import ClanError from .errors import ClanError
from .ssh import cli as ssh_cli from .ssh import cli as ssh_cli
@@ -24,8 +23,9 @@ def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser:
admin.register_parser(parser_admin) admin.register_parser(parser_admin)
# DISABLED: this currently crashes if a flake does not define .#clanOptions # DISABLED: this currently crashes if a flake does not define .#clanOptions
# parser_config = subparsers.add_parser("config", help="set nixos configuration") if os.environ.get("CLAN_OPTIONS_FILE") is not None:
# config.register_parser(parser_config) parser_config = subparsers.add_parser("config", help="set nixos configuration")
config.register_parser(parser_config)
parser_ssh = subparsers.add_parser("ssh", help="ssh to a remote machine") parser_ssh = subparsers.add_parser("ssh", help="ssh to a remote machine")
ssh_cli.register_parser(parser_ssh) ssh_cli.register_parser(parser_ssh)

View File

@@ -1,11 +1,10 @@
import argparse
import json import json
import sys
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import pytest import pytest
from cli import Cli
from clan_cli import config from clan_cli import config
from clan_cli.config import parsing from clan_cli.config import parsing
@@ -15,7 +14,7 @@ example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
# use pytest.parametrize # use pytest.parametrize
@pytest.mark.parametrize( @pytest.mark.parametrize(
"argv,expected", "args,expected",
[ [
(["name", "DavHau"], {"name": "DavHau"}), (["name", "DavHau"], {"name": "DavHau"}),
( (
@@ -27,25 +26,18 @@ example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
], ],
) )
def test_set_some_option( def test_set_some_option(
argv: list[str], args: list[str],
expected: dict[str, Any], expected: dict[str, Any],
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
# monkeypatch sys.argv monkeypatch.setenv("CLAN_OPTIONS_FILE", example_options)
# create temporary file for out_file # create temporary file for out_file
with tempfile.NamedTemporaryFile() as out_file: with tempfile.NamedTemporaryFile() as out_file:
with open(out_file.name, "w") as f: with open(out_file.name, "w") as f:
json.dump({}, f) json.dump({}, f)
monkeypatch.setattr( cli = Cli()
sys, "argv", ["", "--quiet", "--settings-file", out_file.name] + argv cli.run(["config", "--quiet", "--settings-file", out_file.name] + args)
)
parser = argparse.ArgumentParser()
config._register_parser(
parser=parser,
options=json.loads(Path(example_options).read_text()),
)
args = parser.parse_args()
args.func(args)
json_out = json.loads(open(out_file.name).read()) json_out = json.loads(open(out_file.name).read())
assert json_out == expected assert json_out == expected