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
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

View File

@@ -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)

View File

@@ -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:

View File

@@ -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}")

View File

@@ -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}"

View File

@@ -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)

View File

@@ -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,
)

View File

@@ -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)

View File

@@ -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"

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
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

View File

@@ -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,