From f9b1a8fa89931f4f97ff83855d1b4d6ac2ec5f72 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 25 Oct 2023 19:23:28 +0200 Subject: [PATCH] Removing find_git_repo --- pkgs/clan-cli/clan_cli/config/__init__.py | 10 +++++- pkgs/clan-cli/clan_cli/config/machine.py | 4 +-- pkgs/clan-cli/clan_cli/dirs.py | 35 ++++++++++---------- pkgs/clan-cli/clan_cli/git.py | 8 ++--- pkgs/clan-cli/shell.nix | 2 +- pkgs/clan-cli/tests/api.py | 1 + pkgs/clan-cli/tests/fixtures_flakes.py | 37 +++++++++------------- pkgs/clan-cli/tests/temporary_dir.py | 2 ++ pkgs/clan-cli/tests/test_config.py | 2 ++ pkgs/clan-cli/tests/test_dirs.py | 35 +++++++++----------- pkgs/clan-cli/tests/test_vms_api_create.py | 7 ++-- 11 files changed, 68 insertions(+), 75 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index 5882af4ca..0cd5ab4fc 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -1,6 +1,7 @@ # !/usr/bin/env python3 import argparse import json +import logging import os import re import shlex @@ -17,6 +18,8 @@ from clan_cli.types import FlakeName script_dir = Path(__file__).parent +log = logging.getLogger(__name__) + # nixos option type description to python type def map_type(type: str) -> Any: @@ -287,6 +290,7 @@ def set_option( current_config = json.load(f) else: current_config = {} + # merge and save the new config file new_config = merge(current_config, result) settings_file.parent.mkdir(parents=True, exist_ok=True) @@ -295,7 +299,11 @@ def set_option( print(file=f) # add newline at the end of the file to make git happy if settings_file.resolve().is_relative_to(specific_flake_dir(flake_name)): - commit_file(settings_file, commit_message=f"Set option {option_description}") + commit_file( + settings_file, + repo_dir=specific_flake_dir(flake_name), + commit_message=f"Set option {option_description}", + ) # takes a (sub)parser and configures it diff --git a/pkgs/clan-cli/clan_cli/config/machine.py b/pkgs/clan-cli/clan_cli/config/machine.py index cd9957ce4..b04c87b8b 100644 --- a/pkgs/clan-cli/clan_cli/config/machine.py +++ b/pkgs/clan-cli/clan_cli/config/machine.py @@ -12,7 +12,7 @@ from clan_cli.dirs import ( specific_flake_dir, specific_machine_dir, ) -from clan_cli.git import commit_file, find_git_repo_root +from clan_cli.git import commit_file from clan_cli.nix import nix_eval from ..types import FlakeName @@ -82,7 +82,7 @@ def set_config_for_machine( settings_path.parent.mkdir(parents=True, exist_ok=True) with open(settings_path, "w") as f: json.dump(config, f) - repo_dir = find_git_repo_root() + repo_dir = specific_flake_dir(flake_name) if repo_dir is not None: commit_file(settings_path, repo_dir) diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py index 5f1157757..1bd45ebb1 100644 --- a/pkgs/clan-cli/clan_cli/dirs.py +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -2,7 +2,6 @@ import logging import os import sys from pathlib import Path -from typing import Optional from .errors import ClanError from .types import FlakeName @@ -10,27 +9,27 @@ from .types import FlakeName log = logging.getLogger(__name__) -def _get_clan_flake_toplevel() -> Path: - return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) +# def _get_clan_flake_toplevel() -> Path: +# return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) -def find_git_repo_root() -> Optional[Path]: - try: - return find_toplevel([".git"]) - except ClanError: - return None +# def find_git_repo_root() -> Optional[Path]: +# try: +# return find_toplevel([".git"]) +# except ClanError: +# return None -def find_toplevel(top_level_files: list[str]) -> Path: - """Returns the path to the toplevel of the clan flake""" - for project_file in top_level_files: - initial_path = Path(os.getcwd()) - path = Path(initial_path) - while path.parent != path: - if (path / project_file).exists(): - return path - path = path.parent - raise ClanError("Could not find clan flake toplevel directory") +# def find_toplevel(top_level_files: list[str]) -> Path: +# """Returns the path to the toplevel of the clan flake""" +# for project_file in top_level_files: +# initial_path = Path(os.getcwd()) +# path = Path(initial_path) +# while path.parent != path: +# if (path / project_file).exists(): +# return path +# path = path.parent +# raise ClanError("Could not find clan flake toplevel directory") def user_config_dir() -> Path: diff --git a/pkgs/clan-cli/clan_cli/git.py b/pkgs/clan-cli/clan_cli/git.py index 12ba4df51..81d61230a 100644 --- a/pkgs/clan-cli/clan_cli/git.py +++ b/pkgs/clan-cli/clan_cli/git.py @@ -3,7 +3,7 @@ import subprocess from pathlib import Path from typing import Optional -from clan_cli.dirs import find_git_repo_root +# from clan_cli.dirs import find_git_repo_root from clan_cli.errors import ClanError from clan_cli.nix import nix_shell @@ -11,13 +11,9 @@ from clan_cli.nix import nix_shell # generic vcs agnostic commit function def commit_file( file_path: Path, - repo_dir: Optional[Path] = None, + repo_dir: Path, commit_message: Optional[str] = None, ) -> None: - if repo_dir is None: - repo_dir = find_git_repo_root() - if repo_dir is None: - return # check that the file is in the git repository and exists if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()): raise ClanError(f"File {file_path} is not in the git repository {repo_dir}") diff --git a/pkgs/clan-cli/shell.nix b/pkgs/clan-cli/shell.nix index 4c3d9cfdc..c796a4d85 100644 --- a/pkgs/clan-cli/shell.nix +++ b/pkgs/clan-cli/shell.nix @@ -44,7 +44,7 @@ mkShell { export PATH="$tmp_path/python/bin:${checkScript}/bin:$PATH" export PYTHONPATH="$repo_root:$tmp_path/python/${pythonWithDeps.sitePackages}:" - export PYTHONBREAKPOINT=ipdb.set_trace + export XDG_DATA_DIRS="$tmp_path/share''${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}" export fish_complete_path="$tmp_path/share/fish/vendor_completions.d''${fish_complete_path:+:$fish_complete_path}" diff --git a/pkgs/clan-cli/tests/api.py b/pkgs/clan-cli/tests/api.py index 7dc1ed86a..78d35e98a 100644 --- a/pkgs/clan-cli/tests/api.py +++ b/pkgs/clan-cli/tests/api.py @@ -4,6 +4,7 @@ from fastapi.testclient import TestClient from clan_cli.webui.app import app +# TODO: Why stateful @pytest.fixture(scope="session") def api() -> TestClient: return TestClient(app) diff --git a/pkgs/clan-cli/tests/fixtures_flakes.py b/pkgs/clan-cli/tests/fixtures_flakes.py index 9497cba25..a151f488b 100644 --- a/pkgs/clan-cli/tests/fixtures_flakes.py +++ b/pkgs/clan-cli/tests/fixtures_flakes.py @@ -2,12 +2,12 @@ import fileinput import logging import os import shutil +import subprocess as sp import tempfile from pathlib import Path from typing import Iterator, NamedTuple import pytest -from command import Command from root import CLAN_CORE from clan_cli.dirs import nixpkgs_source @@ -42,7 +42,6 @@ def create_flake( monkeypatch: pytest.MonkeyPatch, temporary_home: Path, flake_name: FlakeName, - command: Command, clan_core_flake: Path | None = None, machines: list[str] = [], remote: bool = False, @@ -71,24 +70,22 @@ def create_flake( substitute(flake_nix, clan_core_flake, flake) if "/tmp" not in str(os.environ.get("HOME")): - log.warning(f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}") + log.warning( + f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}" + ) # TODO: Find out why test_vms_api.py fails in nix build # but works in pytest when this bottom line is commented out - command.run( + sp.run( ["git", "config", "--global", "init.defaultBranch", "main"], - workdir=flake, + cwd=flake, check=True, ) - command.run(["git", "init"], workdir=flake, check=True) - command.run(["git", "add", "."], workdir=flake, check=True) - command.run(["git", "config", "user.name", "clan-tool"], workdir=flake, check=True) - command.run( - ["git", "config", "user.email", "clan@example.com"], workdir=flake, check=True - ) - command.run( - ["git", "commit", "-a", "-m", "Initial commit"], workdir=flake, check=True - ) + sp.run(["git", "init"], cwd=flake, check=True) + sp.run(["git", "add", "."], cwd=flake, check=True) + sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True) + sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True) + sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True) if remote: with tempfile.TemporaryDirectory(): @@ -99,16 +96,14 @@ def create_flake( @pytest.fixture def test_flake( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command + monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: - yield from create_flake( - monkeypatch, temporary_home, FlakeName("test_flake"), command - ) + yield from create_flake(monkeypatch, temporary_home, FlakeName("test_flake")) @pytest.fixture def test_flake_with_core( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command + monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: if not (CLAN_CORE / "flake.nix").exists(): raise Exception( @@ -118,14 +113,13 @@ def test_flake_with_core( monkeypatch, temporary_home, FlakeName("test_flake_with_core"), - command, CLAN_CORE, ) @pytest.fixture def test_flake_with_core_and_pass( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command + monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: if not (CLAN_CORE / "flake.nix").exists(): raise Exception( @@ -135,6 +129,5 @@ def test_flake_with_core_and_pass( monkeypatch, temporary_home, FlakeName("test_flake_with_core_and_pass"), - command, CLAN_CORE, ) diff --git a/pkgs/clan-cli/tests/temporary_dir.py b/pkgs/clan-cli/tests/temporary_dir.py index 4d6ca1747..e7cbfa0d5 100644 --- a/pkgs/clan-cli/tests/temporary_dir.py +++ b/pkgs/clan-cli/tests/temporary_dir.py @@ -16,10 +16,12 @@ def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]: path = Path(env_dir).resolve() log.debug("Temp HOME directory: %s", str(path)) monkeypatch.setenv("HOME", str(path)) + monkeypatch.chdir(str(path)) yield path else: log.debug("TEST_TEMPORARY_DIR not set, using TemporaryDirectory") with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath: monkeypatch.setenv("HOME", str(dirpath)) + monkeypatch.chdir(str(dirpath)) log.debug("Temp HOME directory: %s", str(dirpath)) yield Path(dirpath) diff --git a/pkgs/clan-cli/tests/test_config.py b/pkgs/clan-cli/tests/test_config.py index 4266c7121..9dabf425f 100644 --- a/pkgs/clan-cli/tests/test_config.py +++ b/pkgs/clan-cli/tests/test_config.py @@ -60,11 +60,13 @@ def test_configure_machine( monkeypatch: pytest.MonkeyPatch, ) -> None: cli = Cli() + cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true", test_flake.name]) # clear the output buffer capsys.readouterr() # read a option value cli.run(["config", "-m", "machine1", "clan.jitsi.enable", test_flake.name]) + # read the output assert capsys.readouterr().out == "true\n" diff --git a/pkgs/clan-cli/tests/test_dirs.py b/pkgs/clan-cli/tests/test_dirs.py index 2f8c9d588..25191b419 100644 --- a/pkgs/clan-cli/tests/test_dirs.py +++ b/pkgs/clan-cli/tests/test_dirs.py @@ -1,22 +1,17 @@ -from pathlib import Path +# from clan_cli.dirs import _get_clan_flake_toplevel -import pytest +# TODO: Reimplement test? +# def test_get_clan_flake_toplevel( +# monkeypatch: pytest.MonkeyPatch, temporary_home: Path +# ) -> None: +# monkeypatch.chdir(temporary_home) +# with pytest.raises(ClanError): +# print(_get_clan_flake_toplevel()) +# (temporary_home / ".git").touch() +# assert _get_clan_flake_toplevel() == temporary_home -from clan_cli.dirs import _get_clan_flake_toplevel -from clan_cli.errors import ClanError - - -def test_get_clan_flake_toplevel( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path -) -> None: - monkeypatch.chdir(temporary_home) - with pytest.raises(ClanError): - print(_get_clan_flake_toplevel()) - (temporary_home / ".git").touch() - assert _get_clan_flake_toplevel() == temporary_home - - subdir = temporary_home / "subdir" - subdir.mkdir() - monkeypatch.chdir(subdir) - (subdir / ".clan-flake").touch() - assert _get_clan_flake_toplevel() == subdir +# subdir = temporary_home / "subdir" +# subdir.mkdir() +# monkeypatch.chdir(subdir) +# (subdir / ".clan-flake").touch() +# assert _get_clan_flake_toplevel() == subdir diff --git a/pkgs/clan-cli/tests/test_vms_api_create.py b/pkgs/clan-cli/tests/test_vms_api_create.py index 820eefd33..71b7b81be 100644 --- a/pkgs/clan-cli/tests/test_vms_api_create.py +++ b/pkgs/clan-cli/tests/test_vms_api_create.py @@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Iterator import pytest from api import TestClient from cli import Cli -from command import Command from fixtures_flakes import FlakeForTest, create_flake from httpx import SyncByteStream from root import CLAN_CORE @@ -18,13 +17,12 @@ if TYPE_CHECKING: @pytest.fixture def flake_with_vm_with_secrets( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command + monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: yield from create_flake( monkeypatch, temporary_home, FlakeName("test_flake_with_core_dynamic_machines"), - command, CLAN_CORE, machines=["vm_with_secrets"], ) @@ -32,13 +30,12 @@ def flake_with_vm_with_secrets( @pytest.fixture def remote_flake_with_vm_without_secrets( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command + monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: yield from create_flake( monkeypatch, temporary_home, FlakeName("test_flake_with_core_dynamic_machines"), - command, CLAN_CORE, machines=["vm_without_secrets"], remote=True,