pkgs/clan/lib: Move get_clan_directories to dirs

This commit is contained in:
a-kenji
2025-07-30 15:07:05 +02:00
parent 3d51cee4bb
commit 62c4f735ed
4 changed files with 105 additions and 96 deletions

View File

@@ -52,56 +52,6 @@ def get_clan_folder() -> Flake:
raise NotImplementedError(msg)
@API.register
def get_clan_directories(flake: Flake) -> tuple[str, str]:
"""
Get the clan source directory and computed clan directory paths.
Args:
flake: The clan flake to get directories from
Returns:
A tuple of (source_directory, computed_clan_directory) where:
- source_directory: Path to the clan source in the nixpkgs store
- computed_clan_directory: Computed clan directory path (source + relative directory)
Raises:
ClanError: If the flake evaluation fails or directories cannot be found
"""
import json
from pathlib import Path
from clan_lib.cmd import run
from clan_lib.errors import ClanError
from clan_lib.nix import nix_eval
# Get the source directory from nix store
root_directory = flake.select("sourceInfo")
# Get the configured directory using nix eval instead of flake.select
# to avoid the select bug with clanInternals.inventoryClass.directory
directory_result = run(
nix_eval(
flags=[
f"{flake.identifier}#clanInternals.inventoryClass.directory",
]
)
)
directory = json.loads(directory_result.stdout.strip())
# Both directories are in the nix store, but we need to calculate the relative path
# to get the actual configured value (e.g., "./direct-config")
root_path = Path(root_directory)
directory_path = Path(directory)
try:
relative_path = directory_path.relative_to(root_path)
return (root_directory, str(relative_path))
except ValueError as e:
msg = f"Directory path '{directory}' is not relative to root directory '{root_directory}'. This indicates a configuration issue with the clan directory setting."
raise ClanError(msg) from e
@dataclass
class BlkInfo:
name: str
@@ -175,5 +125,7 @@ def get_clan_directory_relative(flake: Flake) -> str:
Raises:
ClanError: If the flake evaluation fails or directories cannot be found
"""
from clan_lib.dirs import get_clan_directories
_, relative_dir = get_clan_directories(flake)
return relative_dir

View File

@@ -4,56 +4,11 @@ from pathlib import Path
import pytest
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_lib.api.directory import get_clan_directories, get_clan_directory_relative
from clan_lib.api.directory import get_clan_directory_relative
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
@pytest.mark.with_core
def test_get_clan_directories_default(test_flake_with_core: FlakeForTest) -> None:
flake = Flake(str(test_flake_with_core.path))
source_dir, computed_dir = get_clan_directories(flake)
assert source_dir.startswith("/nix/store/")
assert "source" in source_dir
computed_path = Path(computed_dir)
assert computed_path == Path()
@pytest.mark.with_core
def test_get_clan_directories_invalid_flake() -> None:
invalid_flake = Flake("/non/existent/path")
with pytest.raises(ClanError):
get_clan_directories(invalid_flake)
@pytest.mark.with_core
def test_get_clan_directories_with_direct_directory_config(
clan_flake: Callable[..., Flake],
) -> None:
"""Test get_clan_directories with clan.directory set directly in Nix configuration"""
flake = clan_flake(
raw="""
{
directory = ./direct-config;
}
"""
)
test_subdir = Path(flake.path) / "direct-config"
test_subdir.mkdir(exist_ok=True)
source_dir, computed_dir = get_clan_directories(flake)
assert source_dir.startswith("/nix/store/")
assert "source" in source_dir
assert computed_dir == "direct-config"
@pytest.mark.with_core
def test_get_relative_clan_directory_default(
test_flake_with_core: FlakeForTest,

View File

@@ -170,3 +170,51 @@ def nixpkgs_source() -> Path:
def select_source() -> Path:
return (module_root() / "select").resolve()
def get_clan_directories(flake: "Flake") -> tuple[str, str]:
"""
Get the clan source directory and computed clan directory paths.
Args:
flake: The clan flake to get directories from
Returns:
A tuple of (source_directory, computed_clan_directory) where:
- source_directory: Path to the clan source in the nixpkgs store
- computed_clan_directory: Computed clan directory path (source + relative directory)
Raises:
ClanError: If the flake evaluation fails or directories cannot be found
"""
import json
from pathlib import Path
from clan_lib.cmd import run
from clan_lib.nix import nix_eval
# Get the source directory from nix store
root_directory = flake.select("sourceInfo")
# Get the configured directory using nix eval instead of flake.select
# to avoid the select bug with clanInternals.inventoryClass.directory
directory_result = run(
nix_eval(
flags=[
f"{flake.identifier}#clanInternals.inventoryClass.directory",
]
)
)
directory = json.loads(directory_result.stdout.strip())
# Both directories are in the nix store, but we need to calculate the relative path
# to get the actual configured value (e.g., "./direct-config")
root_path = Path(root_directory)
directory_path = Path(directory)
try:
relative_path = directory_path.relative_to(root_path)
return (root_directory, str(relative_path))
except ValueError as e:
msg = f"Directory path '{directory}' is not relative to root directory '{root_directory}'. This indicates a configuration issue with the clan directory setting."
raise ClanError(msg) from e

View File

@@ -0,0 +1,54 @@
from collections.abc import Callable
from pathlib import Path
import pytest
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_lib.dirs import get_clan_directories
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
@pytest.mark.with_core
def test_get_clan_directories_default(test_flake_with_core: FlakeForTest) -> None:
flake = Flake(str(test_flake_with_core.path))
source_dir, computed_dir = get_clan_directories(flake)
assert source_dir.startswith("/nix/store/")
assert "source" in source_dir
computed_path = Path(computed_dir)
assert computed_path == Path()
@pytest.mark.with_core
def test_get_clan_directories_invalid_flake() -> None:
invalid_flake = Flake("/non/existent/path")
with pytest.raises(ClanError):
get_clan_directories(invalid_flake)
@pytest.mark.with_core
def test_get_clan_directories_with_direct_directory_config(
clan_flake: Callable[..., Flake],
) -> None:
"""Test get_clan_directories with clan.directory set directly in Nix configuration"""
flake = clan_flake(
raw="""
{
directory = ./direct-config;
}
"""
)
test_subdir = Path(flake.path) / "direct-config"
test_subdir.mkdir(exist_ok=True)
source_dir, computed_dir = get_clan_directories(flake)
assert source_dir.startswith("/nix/store/")
assert "source" in source_dir
assert computed_dir == "direct-config"