generated from Luis/nextjs-python-web-template
clan-config: fix tests
This commit is contained in:
@@ -92,7 +92,7 @@ def cast(value: Any, type: Type, opt_description: str) -> Any:
|
|||||||
|
|
||||||
|
|
||||||
def process_args(
|
def process_args(
|
||||||
option: str, value: Any, options: dict, option_description: str = ""
|
option: str, value: Any, options: dict, out_file: Path, option_description: str = ""
|
||||||
) -> None:
|
) -> None:
|
||||||
option_path = option.split(".")
|
option_path = option.split(".")
|
||||||
|
|
||||||
@@ -107,6 +107,7 @@ def process_args(
|
|||||||
option=".".join(option_parent),
|
option=".".join(option_parent),
|
||||||
value={attr: value},
|
value={attr: value},
|
||||||
options=options,
|
options=options,
|
||||||
|
out_file=out_file,
|
||||||
option_description=option,
|
option_description=option,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -125,14 +126,14 @@ def process_args(
|
|||||||
current[option_path[-1]] = casted
|
current[option_path[-1]] = casted
|
||||||
|
|
||||||
# check if there is an existing config file
|
# check if there is an existing config file
|
||||||
if os.path.exists("clan-settings.json"):
|
if os.path.exists(out_file):
|
||||||
with open("clan-settings.json") as f:
|
with open(out_file) as f:
|
||||||
current_config = json.load(f)
|
current_config = json.load(f)
|
||||||
else:
|
else:
|
||||||
current_config = {}
|
current_config = {}
|
||||||
# merge and save the new config file
|
# merge and save the new config file
|
||||||
new_config = merge(current_config, result)
|
new_config = merge(current_config, result)
|
||||||
with open("clan-settings.json", "w") as f:
|
with open(out_file, "w") as f:
|
||||||
json.dump(new_config, f, indent=2)
|
json.dump(new_config, f, indent=2)
|
||||||
print("New config:")
|
print("New config:")
|
||||||
print(json.dumps(new_config, indent=2))
|
print(json.dumps(new_config, indent=2))
|
||||||
@@ -178,10 +179,22 @@ def _register_parser(
|
|||||||
# inject callback function to process the input later
|
# inject callback function to process the input later
|
||||||
parser.set_defaults(
|
parser.set_defaults(
|
||||||
func=lambda args: process_args(
|
func=lambda args: process_args(
|
||||||
option=args.option, value=args.value, options=options
|
option=args.option,
|
||||||
|
value=args.value,
|
||||||
|
options=options,
|
||||||
|
out_file=args.out_file,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# add argument to pass output file
|
||||||
|
parser.add_argument(
|
||||||
|
"--out-file",
|
||||||
|
"-o",
|
||||||
|
help="Output file",
|
||||||
|
type=Path,
|
||||||
|
default=Path("clan-settings.json"),
|
||||||
|
)
|
||||||
|
|
||||||
# add single positional argument for the option (e.g. "foo.bar")
|
# add single positional argument for the option (e.g. "foo.bar")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"option",
|
"option",
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
, rsync
|
, rsync
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
# This provides dummy options for testing clan config and prevents it from
|
||||||
|
# evaluating the flake .#
|
||||||
|
CLAN_OPTIONS_FILE = ./clan_cli/config/jsonschema/options.json;
|
||||||
|
|
||||||
dependencies = [ argcomplete ];
|
dependencies = [ argcomplete ];
|
||||||
|
|
||||||
testDependencies = [
|
testDependencies = [
|
||||||
@@ -48,7 +52,7 @@ python3.pkgs.buildPythonPackage {
|
|||||||
src = source;
|
src = source;
|
||||||
format = "pyproject";
|
format = "pyproject";
|
||||||
|
|
||||||
CLAN_OPTIONS_FILE = ./clan_cli/config/jsonschema/options.json;
|
inherit CLAN_OPTIONS_FILE;
|
||||||
|
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
setuptools
|
setuptools
|
||||||
@@ -58,6 +62,7 @@ python3.pkgs.buildPythonPackage {
|
|||||||
|
|
||||||
passthru.tests = {
|
passthru.tests = {
|
||||||
clan-mypy = runCommand "clan-mypy" { } ''
|
clan-mypy = runCommand "clan-mypy" { } ''
|
||||||
|
export CLAN_OPTIONS_FILE="${CLAN_OPTIONS_FILE}"
|
||||||
cp -r ${source} ./src
|
cp -r ${source} ./src
|
||||||
chmod +w -R ./src
|
chmod +w -R ./src
|
||||||
cd ./src
|
cd ./src
|
||||||
@@ -68,6 +73,7 @@ python3.pkgs.buildPythonPackage {
|
|||||||
{
|
{
|
||||||
nativeBuildInputs = [ age zerotierone bubblewrap sops nix openssh rsync stdenv.cc ];
|
nativeBuildInputs = [ age zerotierone bubblewrap sops nix openssh rsync stdenv.cc ];
|
||||||
} ''
|
} ''
|
||||||
|
export CLAN_OPTIONS_FILE="${CLAN_OPTIONS_FILE}"
|
||||||
cp -r ${source} ./src
|
cp -r ${source} ./src
|
||||||
chmod +w -R ./src
|
chmod +w -R ./src
|
||||||
cd ./src
|
cd ./src
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ pkgs.mkShell {
|
|||||||
];
|
];
|
||||||
# sets up an editable install and add enty points to $PATH
|
# sets up an editable install and add enty points to $PATH
|
||||||
CLAN_FLAKE = self;
|
CLAN_FLAKE = self;
|
||||||
|
# This provides dummy options for testing clan config and prevents it from
|
||||||
|
# evaluating the flake .#
|
||||||
|
CLAN_OPTIONS_FILE = ./clan_cli/config/jsonschema/options.json;
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
tmp_path=$(realpath ./.pythonenv)
|
tmp_path=$(realpath ./.pythonenv)
|
||||||
repo_root=$(realpath .)
|
repo_root=$(realpath .)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -32,14 +33,19 @@ def test_set_some_option(
|
|||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
) -> None:
|
||||||
# monkeypatch sys.argv
|
# monkeypatch sys.argv
|
||||||
monkeypatch.setattr(sys, "argv", [""] + argv)
|
# create temporary file for out_file
|
||||||
|
with tempfile.NamedTemporaryFile() as out_file:
|
||||||
|
with open(out_file.name, "w") as f:
|
||||||
|
json.dump({}, f)
|
||||||
|
monkeypatch.setattr(sys, "argv", ["", "--out-file", out_file.name] + argv)
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
config.register_parser(parser=parser, optionsFile=Path(example_options))
|
config._register_parser(
|
||||||
|
parser=parser,
|
||||||
|
options=json.loads(Path(example_options).read_text()),
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
args.func(args)
|
args.func(args)
|
||||||
captured = capsys.readouterr()
|
json_out = json.loads(open(out_file.name).read())
|
||||||
print(captured.out)
|
|
||||||
json_out = json.loads(captured.out)
|
|
||||||
assert json_out == expected
|
assert json_out == expected
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user