Removing find_git_repo

This commit is contained in:
Qubasa
2023-10-25 19:23:28 +02:00
parent 8e8b6530b0
commit a628934351
11 changed files with 68 additions and 75 deletions

View File

@@ -1,6 +1,7 @@
# !/usr/bin/env python3 # !/usr/bin/env python3
import argparse import argparse
import json import json
import logging
import os import os
import re import re
import shlex import shlex
@@ -17,6 +18,8 @@ from clan_cli.types import FlakeName
script_dir = Path(__file__).parent script_dir = Path(__file__).parent
log = logging.getLogger(__name__)
# nixos option type description to python type # nixos option type description to python type
def map_type(type: str) -> Any: def map_type(type: str) -> Any:
@@ -287,6 +290,7 @@ def set_option(
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)
settings_file.parent.mkdir(parents=True, exist_ok=True) 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 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)): 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 # takes a (sub)parser and configures it

View File

@@ -12,7 +12,7 @@ from clan_cli.dirs import (
specific_flake_dir, specific_flake_dir,
specific_machine_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 clan_cli.nix import nix_eval
from ..types import FlakeName from ..types import FlakeName
@@ -82,7 +82,7 @@ def set_config_for_machine(
settings_path.parent.mkdir(parents=True, exist_ok=True) settings_path.parent.mkdir(parents=True, exist_ok=True)
with open(settings_path, "w") as f: with open(settings_path, "w") as f:
json.dump(config, f) json.dump(config, f)
repo_dir = find_git_repo_root() repo_dir = specific_flake_dir(flake_name)
if repo_dir is not None: if repo_dir is not None:
commit_file(settings_path, repo_dir) commit_file(settings_path, repo_dir)

View File

@@ -2,7 +2,6 @@ import logging
import os import os
import sys import sys
from pathlib import Path from pathlib import Path
from typing import Optional
from .errors import ClanError from .errors import ClanError
from .types import FlakeName from .types import FlakeName
@@ -10,27 +9,27 @@ from .types import FlakeName
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def _get_clan_flake_toplevel() -> Path: # def _get_clan_flake_toplevel() -> Path:
return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) # return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"])
def find_git_repo_root() -> Optional[Path]: # def find_git_repo_root() -> Optional[Path]:
try: # try:
return find_toplevel([".git"]) # return find_toplevel([".git"])
except ClanError: # except ClanError:
return None # return None
def find_toplevel(top_level_files: list[str]) -> Path: # def find_toplevel(top_level_files: list[str]) -> Path:
"""Returns the path to the toplevel of the clan flake""" # """Returns the path to the toplevel of the clan flake"""
for project_file in top_level_files: # for project_file in top_level_files:
initial_path = Path(os.getcwd()) # initial_path = Path(os.getcwd())
path = Path(initial_path) # path = Path(initial_path)
while path.parent != path: # while path.parent != path:
if (path / project_file).exists(): # if (path / project_file).exists():
return path # return path
path = path.parent # path = path.parent
raise ClanError("Could not find clan flake toplevel directory") # raise ClanError("Could not find clan flake toplevel directory")
def user_config_dir() -> Path: def user_config_dir() -> Path:

View File

@@ -3,7 +3,7 @@ import subprocess
from pathlib import Path from pathlib import Path
from typing import Optional 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.errors import ClanError
from clan_cli.nix import nix_shell from clan_cli.nix import nix_shell
@@ -11,13 +11,9 @@ from clan_cli.nix import nix_shell
# generic vcs agnostic commit function # generic vcs agnostic commit function
def commit_file( def commit_file(
file_path: Path, file_path: Path,
repo_dir: Optional[Path] = None, repo_dir: Path,
commit_message: Optional[str] = None, commit_message: Optional[str] = None,
) -> 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 # check that the file is in the git repository and exists
if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()): 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}") raise ClanError(f"File {file_path} is not in the git repository {repo_dir}")

View File

@@ -44,7 +44,7 @@ mkShell {
export PATH="$tmp_path/python/bin:${checkScript}/bin:$PATH" export PATH="$tmp_path/python/bin:${checkScript}/bin:$PATH"
export PYTHONPATH="$repo_root:$tmp_path/python/${pythonWithDeps.sitePackages}:" 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 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}" export fish_complete_path="$tmp_path/share/fish/vendor_completions.d''${fish_complete_path:+:$fish_complete_path}"

View File

@@ -4,6 +4,7 @@ from fastapi.testclient import TestClient
from clan_cli.webui.app import app from clan_cli.webui.app import app
# TODO: Why stateful
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def api() -> TestClient: def api() -> TestClient:
return TestClient(app) return TestClient(app)

View File

@@ -2,12 +2,12 @@ import fileinput
import logging import logging
import os import os
import shutil import shutil
import subprocess as sp
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Iterator, NamedTuple from typing import Iterator, NamedTuple
import pytest import pytest
from command import Command
from root import CLAN_CORE from root import CLAN_CORE
from clan_cli.dirs import nixpkgs_source from clan_cli.dirs import nixpkgs_source
@@ -42,7 +42,6 @@ def create_flake(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
temporary_home: Path, temporary_home: Path,
flake_name: FlakeName, flake_name: FlakeName,
command: Command,
clan_core_flake: Path | None = None, clan_core_flake: Path | None = None,
machines: list[str] = [], machines: list[str] = [],
remote: bool = False, remote: bool = False,
@@ -71,24 +70,22 @@ def create_flake(
substitute(flake_nix, clan_core_flake, flake) substitute(flake_nix, clan_core_flake, flake)
if "/tmp" not in str(os.environ.get("HOME")): 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 # TODO: Find out why test_vms_api.py fails in nix build
# but works in pytest when this bottom line is commented out # but works in pytest when this bottom line is commented out
command.run( sp.run(
["git", "config", "--global", "init.defaultBranch", "main"], ["git", "config", "--global", "init.defaultBranch", "main"],
workdir=flake, cwd=flake,
check=True, check=True,
) )
command.run(["git", "init"], workdir=flake, check=True) sp.run(["git", "init"], cwd=flake, check=True)
command.run(["git", "add", "."], workdir=flake, check=True) sp.run(["git", "add", "."], cwd=flake, check=True)
command.run(["git", "config", "user.name", "clan-tool"], workdir=flake, check=True) sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True)
command.run( sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True)
["git", "config", "user.email", "clan@example.com"], workdir=flake, check=True sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True)
)
command.run(
["git", "commit", "-a", "-m", "Initial commit"], workdir=flake, check=True
)
if remote: if remote:
with tempfile.TemporaryDirectory(): with tempfile.TemporaryDirectory():
@@ -99,16 +96,14 @@ def create_flake(
@pytest.fixture @pytest.fixture
def test_flake( def test_flake(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake( yield from create_flake(monkeypatch, temporary_home, FlakeName("test_flake"))
monkeypatch, temporary_home, FlakeName("test_flake"), command
)
@pytest.fixture @pytest.fixture
def test_flake_with_core( def test_flake_with_core(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists(): if not (CLAN_CORE / "flake.nix").exists():
raise Exception( raise Exception(
@@ -118,14 +113,13 @@ def test_flake_with_core(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core"), FlakeName("test_flake_with_core"),
command,
CLAN_CORE, CLAN_CORE,
) )
@pytest.fixture @pytest.fixture
def test_flake_with_core_and_pass( def test_flake_with_core_and_pass(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists(): if not (CLAN_CORE / "flake.nix").exists():
raise Exception( raise Exception(
@@ -135,6 +129,5 @@ def test_flake_with_core_and_pass(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_and_pass"), FlakeName("test_flake_with_core_and_pass"),
command,
CLAN_CORE, CLAN_CORE,
) )

View File

@@ -16,10 +16,12 @@ def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
path = Path(env_dir).resolve() path = Path(env_dir).resolve()
log.debug("Temp HOME directory: %s", str(path)) log.debug("Temp HOME directory: %s", str(path))
monkeypatch.setenv("HOME", str(path)) monkeypatch.setenv("HOME", str(path))
monkeypatch.chdir(str(path))
yield path yield path
else: else:
log.debug("TEST_TEMPORARY_DIR not set, using TemporaryDirectory") log.debug("TEST_TEMPORARY_DIR not set, using TemporaryDirectory")
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath: with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
monkeypatch.setenv("HOME", str(dirpath)) monkeypatch.setenv("HOME", str(dirpath))
monkeypatch.chdir(str(dirpath))
log.debug("Temp HOME directory: %s", str(dirpath)) log.debug("Temp HOME directory: %s", str(dirpath))
yield Path(dirpath) yield Path(dirpath)

View File

@@ -60,11 +60,13 @@ def test_configure_machine(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
cli = Cli() cli = Cli()
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true", test_flake.name]) cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true", test_flake.name])
# clear the output buffer # clear the output buffer
capsys.readouterr() capsys.readouterr()
# read a option value # read a option value
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", test_flake.name]) cli.run(["config", "-m", "machine1", "clan.jitsi.enable", test_flake.name])
# read the output # read the output
assert capsys.readouterr().out == "true\n" assert capsys.readouterr().out == "true\n"

View File

@@ -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 # subdir = temporary_home / "subdir"
from clan_cli.errors import ClanError # subdir.mkdir()
# monkeypatch.chdir(subdir)
# (subdir / ".clan-flake").touch()
def test_get_clan_flake_toplevel( # assert _get_clan_flake_toplevel() == subdir
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

View File

@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Iterator
import pytest import pytest
from api import TestClient from api import TestClient
from cli import Cli from cli import Cli
from command import Command
from fixtures_flakes import FlakeForTest, create_flake from fixtures_flakes import FlakeForTest, create_flake
from httpx import SyncByteStream from httpx import SyncByteStream
from root import CLAN_CORE from root import CLAN_CORE
@@ -18,13 +17,12 @@ if TYPE_CHECKING:
@pytest.fixture @pytest.fixture
def flake_with_vm_with_secrets( def flake_with_vm_with_secrets(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake( yield from create_flake(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"), FlakeName("test_flake_with_core_dynamic_machines"),
command,
CLAN_CORE, CLAN_CORE,
machines=["vm_with_secrets"], machines=["vm_with_secrets"],
) )
@@ -32,13 +30,12 @@ def flake_with_vm_with_secrets(
@pytest.fixture @pytest.fixture
def remote_flake_with_vm_without_secrets( def remote_flake_with_vm_without_secrets(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake( yield from create_flake(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"), FlakeName("test_flake_with_core_dynamic_machines"),
command,
CLAN_CORE, CLAN_CORE,
machines=["vm_without_secrets"], machines=["vm_without_secrets"],
remote=True, remote=True,