Merge pull request 'pkgs/clan/lib(directory): Add API function to query the configured directory' (#4655) from kenji/ke-add-clan-dir-api into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4655
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
from clan_lib.cmd import RunOpts, run
|
from clan_lib.cmd import RunOpts, run
|
||||||
@@ -129,3 +130,15 @@ def get_clan_directory_relative(flake: Flake) -> str:
|
|||||||
|
|
||||||
_, relative_dir = get_clan_directories(flake)
|
_, relative_dir = get_clan_directories(flake)
|
||||||
return relative_dir
|
return relative_dir
|
||||||
|
|
||||||
|
|
||||||
|
def get_clan_dir(flake: Flake) -> Path:
|
||||||
|
"""
|
||||||
|
Get the effective clan directory, respecting the clan.directory configuration.
|
||||||
|
Args:
|
||||||
|
flake: The clan flake
|
||||||
|
Returns:
|
||||||
|
Path to the effective clan directory
|
||||||
|
"""
|
||||||
|
relative_clan_dir = get_clan_directory_relative(flake)
|
||||||
|
return flake.path / relative_clan_dir if relative_clan_dir else flake.path
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from pathlib import Path
|
|||||||
import pytest
|
import pytest
|
||||||
from clan_cli.tests.fixtures_flakes import FlakeForTest
|
from clan_cli.tests.fixtures_flakes import FlakeForTest
|
||||||
|
|
||||||
from clan_lib.api.directory import get_clan_directory_relative
|
from clan_lib.api.directory import get_clan_dir, get_clan_directory_relative
|
||||||
from clan_lib.errors import ClanError
|
from clan_lib.errors import ClanError
|
||||||
from clan_lib.flake import Flake
|
from clan_lib.flake import Flake
|
||||||
|
|
||||||
@@ -46,3 +46,34 @@ def test_get_relative_clan_directory_invalid_flake() -> None:
|
|||||||
|
|
||||||
with pytest.raises(ClanError):
|
with pytest.raises(ClanError):
|
||||||
get_clan_directory_relative(invalid_flake)
|
get_clan_directory_relative(invalid_flake)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.with_core
|
||||||
|
def test_get_clan_dir_default(
|
||||||
|
test_flake_with_core: FlakeForTest,
|
||||||
|
) -> None:
|
||||||
|
flake = Flake(str(test_flake_with_core.path))
|
||||||
|
clan_dir = get_clan_dir(flake)
|
||||||
|
|
||||||
|
# Default configuration should return the full path
|
||||||
|
assert clan_dir == flake.path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.with_core
|
||||||
|
def test_get_clan_dir_custom(
|
||||||
|
clan_flake: Callable[..., Flake],
|
||||||
|
) -> None:
|
||||||
|
flake = clan_flake(
|
||||||
|
raw="""
|
||||||
|
{
|
||||||
|
directory = ./direct-config;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
test_subdir = Path(flake.path) / "direct-config"
|
||||||
|
test_subdir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
relative_dir = get_clan_dir(flake)
|
||||||
|
|
||||||
|
assert relative_dir == test_subdir
|
||||||
|
|||||||
Reference in New Issue
Block a user