pkgs/clan/lib: Add clan api to get the relative clan directory
This is a Continuation of: #4519
This commit is contained in:
@@ -170,3 +170,22 @@ def list_system_storage_devices() -> Blockdevices:
|
|||||||
return Blockdevices(
|
return Blockdevices(
|
||||||
blockdevices=[blk_from_dict(device) for device in blk_info["blockdevices"]]
|
blockdevices=[blk_from_dict(device) for device in blk_info["blockdevices"]]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@API.register
|
||||||
|
def get_clan_directory_relative(flake: Flake) -> str:
|
||||||
|
"""
|
||||||
|
Get the clan directory path relative to the flake root
|
||||||
|
from the clan.directory configuration setting.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
flake: The clan flake to get the relative directory from
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The relative directory path (e.g., ".", "direct-config", "subdir/config")
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ClanError: If the flake evaluation fails or directories cannot be found
|
||||||
|
"""
|
||||||
|
_, relative_dir = get_clan_directories(flake)
|
||||||
|
return relative_dir
|
||||||
|
|||||||
@@ -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_directories
|
from clan_lib.api.directory import get_clan_directories, 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
|
||||||
|
|
||||||
@@ -52,3 +52,42 @@ def test_get_clan_directories_with_direct_directory_config(
|
|||||||
assert "source" in source_dir
|
assert "source" in source_dir
|
||||||
|
|
||||||
assert computed_dir == "direct-config"
|
assert computed_dir == "direct-config"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.with_core
|
||||||
|
def test_get_relative_clan_directory_default(
|
||||||
|
test_flake_with_core: FlakeForTest,
|
||||||
|
) -> None:
|
||||||
|
flake = Flake(str(test_flake_with_core.path))
|
||||||
|
relative_dir = get_clan_directory_relative(flake)
|
||||||
|
|
||||||
|
# Default configuration should return "." (which represents current directory)
|
||||||
|
assert relative_dir == "."
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.with_core
|
||||||
|
def test_get_relative_clan_directory_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_directory_relative(flake)
|
||||||
|
|
||||||
|
assert relative_dir == "direct-config"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.with_core
|
||||||
|
def test_get_relative_clan_directory_invalid_flake() -> None:
|
||||||
|
invalid_flake = Flake("/non/existent/path")
|
||||||
|
|
||||||
|
with pytest.raises(ClanError):
|
||||||
|
get_clan_directory_relative(invalid_flake)
|
||||||
|
|||||||
Reference in New Issue
Block a user