forked from clan/clan-core
Fixed pytest
This commit is contained in:
parent
0a9fb41785
commit
d4e57df835
@ -106,6 +106,10 @@ class ClanURI:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, url: str, params: ClanParameters | None = None) -> Self: # noqa
|
def from_str(cls, url: str, params: ClanParameters | None = None) -> Self: # noqa
|
||||||
|
prefix = "clan://"
|
||||||
|
if url.startswith(prefix):
|
||||||
|
url = url[len(prefix) :]
|
||||||
|
|
||||||
if params is None:
|
if params is None:
|
||||||
return cls(f"clan://{url}")
|
return cls(f"clan://{url}")
|
||||||
|
|
||||||
@ -118,4 +122,4 @@ class ClanURI:
|
|||||||
return cls(f"clan://{new_url}")
|
return cls(f"clan://{new_url}")
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"ClanURI({self._components.geturl()})"
|
return self.get_full_uri()
|
||||||
|
@ -5,6 +5,7 @@ from dataclasses import dataclass
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
|
from ..machines.list import list_machines
|
||||||
from ..nix import nix_config, nix_eval, nix_metadata
|
from ..nix import nix_config, nix_eval, nix_metadata
|
||||||
|
|
||||||
|
|
||||||
@ -24,6 +25,12 @@ def inspect_flake(flake_url: str | Path, flake_attr: str) -> FlakeConfig:
|
|||||||
config = nix_config()
|
config = nix_config()
|
||||||
system = config["system"]
|
system = config["system"]
|
||||||
|
|
||||||
|
machines = list_machines(flake_url)
|
||||||
|
if flake_attr not in machines:
|
||||||
|
raise ClanError(
|
||||||
|
f"Machine {flake_attr} not found in {flake_url}. Available machines: {', '.join(machines)}"
|
||||||
|
)
|
||||||
|
|
||||||
cmd = nix_eval(
|
cmd = nix_eval(
|
||||||
[
|
[
|
||||||
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
|
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
|
||||||
|
@ -77,12 +77,12 @@ def add_history(uri: ClanURI) -> list[HistoryEntry]:
|
|||||||
|
|
||||||
|
|
||||||
def add_history_command(args: argparse.Namespace) -> None:
|
def add_history_command(args: argparse.Namespace) -> None:
|
||||||
add_history(args.path)
|
add_history(args.uri)
|
||||||
|
|
||||||
|
|
||||||
# takes a (sub)parser and configures it
|
# takes a (sub)parser and configures it
|
||||||
def register_add_parser(parser: argparse.ArgumentParser) -> None:
|
def register_add_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"uri", type=ClanURI, help="Path to the flake", default=ClanURI(".")
|
"uri", type=ClanURI.from_str, help="Path to the flake", default="."
|
||||||
)
|
)
|
||||||
parser.set_defaults(func=add_history_command)
|
parser.set_defaults(func=add_history_command)
|
||||||
|
@ -11,12 +11,12 @@ from ..nix import nix_config, nix_eval
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def list_machines(flake_dir: Path) -> list[str]:
|
def list_machines(flake_url: Path | str) -> list[str]:
|
||||||
config = nix_config()
|
config = nix_config()
|
||||||
system = config["system"]
|
system = config["system"]
|
||||||
cmd = nix_eval(
|
cmd = nix_eval(
|
||||||
[
|
[
|
||||||
f"{flake_dir}#clanInternals.machines.{system}",
|
f"{flake_url}#clanInternals.machines.{system}",
|
||||||
"--apply",
|
"--apply",
|
||||||
"builtins.attrNames",
|
"builtins.attrNames",
|
||||||
"--json",
|
"--json",
|
||||||
|
@ -125,6 +125,11 @@ def test_from_str() -> None:
|
|||||||
assert uri.params.flake_attr == "defaultVM"
|
assert uri.params.flake_attr == "defaultVM"
|
||||||
assert uri.get_internal() == "~/Downloads/democlan"
|
assert uri.get_internal() == "~/Downloads/democlan"
|
||||||
|
|
||||||
|
uri_str = "clan://~/Downloads/democlan"
|
||||||
|
uri = ClanURI.from_str(url=uri_str)
|
||||||
|
assert uri.params.flake_attr == "defaultVM"
|
||||||
|
assert uri.get_internal() == "~/Downloads/democlan"
|
||||||
|
|
||||||
|
|
||||||
def test_remote_with_all_params() -> None:
|
def test_remote_with_all_params() -> None:
|
||||||
# Create a ClanURI object from a remote URI with parameters
|
# Create a ClanURI object from a remote URI with parameters
|
||||||
|
@ -5,6 +5,7 @@ from cli import Cli
|
|||||||
from fixtures_flakes import FlakeForTest
|
from fixtures_flakes import FlakeForTest
|
||||||
from pytest import CaptureFixture
|
from pytest import CaptureFixture
|
||||||
|
|
||||||
|
from clan_cli.clan_uri import ClanParameters, ClanURI
|
||||||
from clan_cli.dirs import user_history_file
|
from clan_cli.dirs import user_history_file
|
||||||
from clan_cli.history.add import HistoryEntry
|
from clan_cli.history.add import HistoryEntry
|
||||||
|
|
||||||
@ -16,10 +17,12 @@ def test_history_add(
|
|||||||
test_flake_with_core: FlakeForTest,
|
test_flake_with_core: FlakeForTest,
|
||||||
) -> None:
|
) -> None:
|
||||||
cli = Cli()
|
cli = Cli()
|
||||||
|
params = ClanParameters(flake_attr="vm1")
|
||||||
|
uri = ClanURI.from_path(test_flake_with_core.path, params=params)
|
||||||
cmd = [
|
cmd = [
|
||||||
"history",
|
"history",
|
||||||
"add",
|
"add",
|
||||||
str(test_flake_with_core.path),
|
str(uri),
|
||||||
]
|
]
|
||||||
|
|
||||||
cli.run(cmd)
|
cli.run(cmd)
|
||||||
@ -35,6 +38,8 @@ def test_history_list(
|
|||||||
test_flake_with_core: FlakeForTest,
|
test_flake_with_core: FlakeForTest,
|
||||||
) -> None:
|
) -> None:
|
||||||
cli = Cli()
|
cli = Cli()
|
||||||
|
params = ClanParameters(flake_attr="vm1")
|
||||||
|
uri = ClanURI.from_path(test_flake_with_core.path, params=params)
|
||||||
cmd = [
|
cmd = [
|
||||||
"history",
|
"history",
|
||||||
"list",
|
"list",
|
||||||
@ -43,6 +48,6 @@ def test_history_list(
|
|||||||
cli.run(cmd)
|
cli.run(cmd)
|
||||||
assert str(test_flake_with_core.path) not in capsys.readouterr().out
|
assert str(test_flake_with_core.path) not in capsys.readouterr().out
|
||||||
|
|
||||||
cli.run(["history", "add", str(test_flake_with_core.path)])
|
cli.run(["history", "add", str(uri)])
|
||||||
cli.run(cmd)
|
cli.run(cmd)
|
||||||
assert str(test_flake_with_core.path) in capsys.readouterr().out
|
assert str(test_flake_with_core.path) in capsys.readouterr().out
|
||||||
|
@ -4,7 +4,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import gi
|
import gi
|
||||||
from clan_cli import flakes, vms
|
from clan_cli import flakes, vms, history
|
||||||
|
|
||||||
gi.require_version("GdkPixbuf", "2.0")
|
gi.require_version("GdkPixbuf", "2.0")
|
||||||
from gi.repository import GdkPixbuf
|
from gi.repository import GdkPixbuf
|
||||||
@ -77,7 +77,7 @@ def get_initial_vms(start: int = 0, end: int | None = None) -> list[VM]:
|
|||||||
|
|
||||||
# TODO: list_history() should return a list of dicts, not a list of paths
|
# TODO: list_history() should return a list of dicts, not a list of paths
|
||||||
# Execute `clan flakes add <path>` to democlan for this to work
|
# Execute `clan flakes add <path>` to democlan for this to work
|
||||||
for entry in flakes.history.list_history():
|
for entry in history.list.list_history():
|
||||||
flake_config = flakes.inspect.inspect_flake(entry.path, "defaultVM")
|
flake_config = flakes.inspect.inspect_flake(entry.path, "defaultVM")
|
||||||
vm_config = vms.inspect.inspect_vm(entry.path, "defaultVM")
|
vm_config = vms.inspect.inspect_vm(entry.path, "defaultVM")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user