pkgs/cli: Move the test folder inside the python module

Move the `tests` folder to `clan_cli/tests`.

As we now want part of our tests to live next to the functions that are
tested - tests that are not in the `/tests` module also need access to
the configured test fixtures that are exposed by the `pytest_plugins`
declaration.

The following folder structure doesn't support this model:

```
├── clan_cli
│   ├── api
│   │    └── api_init_test.py
├── tests/
│   ├── conftest.py
│   └── ...
```

Here `api_init_test.py` even when importing the test functions will not
have the fixtures configured.

There is a way to configure python to import the fixtures from another
[`project/module`](https://docs.pytest.org/en/stable/how-to/fixtures.html#using-fixtures-from-other-projects), but this seems to *generally* be discouraged.

So moving the `conftest.py` to the toplevel and the `/tests` folder into
the toplevel seems to be a sensible choice choice.
This commit is contained in:
a-kenji
2025-02-28 11:56:38 +07:00
committed by Johannes Kirschbauer
parent 37cc831695
commit fae630842d
72 changed files with 117 additions and 100 deletions

View File

@@ -0,0 +1,19 @@
import argparse
import logging
import shlex
from clan_cli import create_parser
from clan_cli.custom_logger import print_trace
log = logging.getLogger(__name__)
def run(args: list[str]) -> argparse.Namespace:
parser = create_parser(prog="clan")
parsed = parser.parse_args(args)
cmd = shlex.join(["clan", *args])
print_trace(f"$ {cmd}", log, "localhost")
if hasattr(parsed, "func"):
parsed.func(parsed)
return parsed

View File

@@ -0,0 +1,11 @@
from collections import defaultdict
from collections.abc import Callable
from typing import Any
def def_value() -> defaultdict:
return defaultdict(def_value)
# allows defining nested dictionary in a single line
nested_dict: Callable[[], dict[str, Any]] = lambda: defaultdict(def_value)

View File

@@ -0,0 +1,21 @@
import subprocess
class Error(Exception):
pass
def is_valid_age_key(secret_key: str) -> bool:
# Run the age-keygen command with the -y flag to check the key format
result = subprocess.run(
["age-keygen", "-y"],
input=secret_key,
capture_output=True,
text=True,
check=False,
)
if result.returncode == 0:
return True
msg = f"Invalid age key: {secret_key}"
raise Error(msg)