Fix unit tests

This commit is contained in:
Johannes Kirschbauer
2024-07-24 12:56:41 +02:00
parent 5b4105b4d8
commit 1d51ae5c85
8 changed files with 50 additions and 31 deletions

View File

@@ -1,12 +1,11 @@
# !/usr/bin/env python3
import argparse
import os
from dataclasses import dataclass, fields
from dataclasses import dataclass
from pathlib import Path
from clan_cli.api import API
from clan_cli.arg_actions import AppendOptionAction
from clan_cli.inventory import Meta, load_inventory_json, save_inventory
from clan_cli.inventory import Inventory, init_inventory
from ..cmd import CmdOut, run
from ..errors import ClanError
@@ -29,11 +28,9 @@ class CreateClanResponse:
@dataclass
class CreateOptions:
directory: Path | str
# Metadata for the clan
# Metadata can be shown with `clan show`
meta: Meta | None = None
# URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url
initial: Inventory | None = None
def git_command(directory: Path, *args: str) -> list[str]:
@@ -88,17 +85,13 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
git_command(directory, "config", "user.email", "clan@example.com")
)
# Write inventory.json file
inventory = load_inventory_json(directory)
if options.meta is not None:
inventory.meta = options.meta
# Persist creates a commit message for each change
save_inventory(inventory, directory, "Init inventory")
flake_update = run(
nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory
)
if options.initial:
init_inventory(options.directory, init=options.initial)
response = CreateClanResponse(
flake_init=flake_init,
git_init=git_init,
@@ -118,15 +111,6 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
default=default_template_url,
)
parser.add_argument(
"--meta",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(Meta)]) }""",
nargs=2,
metavar=("name", "value"),
action=AppendOptionAction,
default=[],
)
parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".")
)

View File

@@ -19,7 +19,8 @@ from pathlib import Path
from types import UnionType
from typing import Any, get_args, get_origin
from clan_cli.errors import ClanError
from clan_cli.api import API
from clan_cli.errors import ClanCmdError, ClanError
from clan_cli.git import commit_file
from ..cmd import run_no_stdout
@@ -216,7 +217,7 @@ def load_inventory_json(
Load the inventory file from the flake directory
If not file is found, returns the default inventory
"""
inventory = default_inventory
inventory = default
inventory_file = get_path(flake_dir)
if inventory_file.exists():
@@ -228,6 +229,10 @@ def load_inventory_json(
# Error decoding the inventory file
raise ClanError(f"Error decoding inventory file: {e}")
if not inventory_file.exists():
# Copy over the meta from the flake if the inventory is not initialized
inventory.meta = load_inventory_eval(flake_dir).meta
return inventory
@@ -242,3 +247,22 @@ def save_inventory(inventory: Inventory, flake_dir: str | Path, message: str) ->
json.dump(dataclass_to_dict(inventory), f, indent=2)
commit_file(inventory_file, Path(flake_dir), commit_message=message)
@API.register
def init_inventory(directory: str, init: Inventory | None = None) -> None:
inventory = None
# Try reading the current flake
if init is None:
try:
inventory = load_inventory_eval(directory)
except ClanCmdError:
pass
if init is not None:
inventory = init
# Write inventory.json file
if inventory is not None:
# Persist creates a commit message for each change
save_inventory(inventory, directory, "Init inventory")

View File

@@ -25,6 +25,7 @@ def create_machine(flake: FlakeId, machine: Machine) -> None:
)
inventory = load_inventory_json(flake.path)
full_inventory = load_inventory_eval(flake.path)
if machine.name in full_inventory.machines.keys():