From 5e5fbfcf0608d4f2aadf73d1bb07aaa1b94a2ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 28 Jul 2023 12:12:37 +0200 Subject: [PATCH] write test for get_clan_flake_toplevel --- pkgs/clan-cli/clan_cli/dirs.py | 15 ++++++++------- pkgs/clan-cli/tests/conftest.py | 6 +++++- pkgs/clan-cli/tests/temporary_dir.py | 11 +++++++++++ pkgs/clan-cli/tests/test_dirs.py | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 pkgs/clan-cli/tests/temporary_dir.py create mode 100644 pkgs/clan-cli/tests/test_dirs.py diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py index e895df0..514bbe1 100644 --- a/pkgs/clan-cli/clan_cli/dirs.py +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -2,18 +2,19 @@ import os import sys from pathlib import Path +from .errors import ClanError + def get_clan_flake_toplevel() -> Path: """Returns the path to the toplevel of the clan flake""" - initial_path = Path(os.getcwd()) - path = Path(initial_path) - while path.parent == path: - project_files = [".clan-flake"] - for project_file in project_files: + for project_file in [".clan-flake", ".git", ".hg", ".svn", "flake.nix"]: + initial_path = Path(os.getcwd()) + path = Path(initial_path) + while path.parent == path: if (path / project_file).exists(): return path - path = path.parent - return initial_path + path = path.parent + raise ClanError("Could not find clan flake toplevel directory") def user_data_dir() -> Path: diff --git a/pkgs/clan-cli/tests/conftest.py b/pkgs/clan-cli/tests/conftest.py index b389b36..f9b5b00 100644 --- a/pkgs/clan-cli/tests/conftest.py +++ b/pkgs/clan-cli/tests/conftest.py @@ -1,2 +1,6 @@ import os -sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers')) +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) + +pytest_plugins = ["temporary_dir"] diff --git a/pkgs/clan-cli/tests/temporary_dir.py b/pkgs/clan-cli/tests/temporary_dir.py new file mode 100644 index 0000000..91310f9 --- /dev/null +++ b/pkgs/clan-cli/tests/temporary_dir.py @@ -0,0 +1,11 @@ +import tempfile +from pathlib import Path +from typing import Iterator + +import pytest + + +@pytest.fixture +def temporary_dir() -> Iterator[Path]: + with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath: + yield Path(dirpath) diff --git a/pkgs/clan-cli/tests/test_dirs.py b/pkgs/clan-cli/tests/test_dirs.py new file mode 100644 index 0000000..e379490 --- /dev/null +++ b/pkgs/clan-cli/tests/test_dirs.py @@ -0,0 +1,14 @@ +from pathlib import Path + +import pytest + +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_dir: Path +) -> None: + monkeypatch.chdir(temporary_dir) + with pytest.raises(ClanError): + get_clan_flake_toplevel()