mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
586f091eeb
Signed-off-by: d9pouces <github@19pouces.net>
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
import pytest
|
|
|
|
from id_configurator.config import Configuration
|
|
from id_configurator.context import Context
|
|
|
|
CONFIG = Configuration.model_validate(
|
|
{"uuid": "12345678-1234-5678-1234-567812345678", "organisation": "InterDiode"}
|
|
)
|
|
|
|
|
|
def make_context(tmp_path, with_src: bool = True) -> Context:
|
|
dst = tmp_path / "dst"
|
|
dst.mkdir()
|
|
src = None
|
|
if with_src:
|
|
src = tmp_path / "src"
|
|
src.mkdir()
|
|
return Context(config=CONFIG, dst=dst, src=src)
|
|
|
|
|
|
def test_dst_path_within_destination(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
resolved = context.dst_path("etc/foo.conf")
|
|
assert resolved == context.dst / "etc" / "foo.conf"
|
|
|
|
|
|
def test_dst_path_rejects_escape(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
with pytest.raises(ValueError, match="refusing to write outside"):
|
|
context.dst_path("../escape.conf")
|
|
|
|
|
|
def test_src_path_without_src(tmp_path) -> None:
|
|
context = make_context(tmp_path, with_src=False)
|
|
assert context.src_path("etc/foo.conf") is None
|
|
|
|
|
|
def test_src_path_with_src(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
resolved = context.src_path("etc/foo.conf")
|
|
assert resolved == context.src / "etc" / "foo.conf"
|
|
|
|
|
|
def test_write_file_creates_parents_and_content(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
path = context.write_file("etc/nested/foo.conf", b"hello")
|
|
assert path.read_bytes() == b"hello"
|
|
assert path.stat().st_mode & 0o777 == 0o644
|
|
|
|
|
|
def test_read_src_file_without_src(tmp_path) -> None:
|
|
context = make_context(tmp_path, with_src=False)
|
|
assert context.read_src_file("etc/foo.conf") is None
|
|
|
|
|
|
def test_read_src_file_missing(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
assert context.read_src_file("etc/missing.conf") is None
|
|
|
|
|
|
def test_read_src_file_present(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
(context.src / "foo.conf").write_bytes(b"existing")
|
|
assert context.read_src_file("foo.conf") == b"existing"
|
|
|
|
|
|
def test_iter_src_dir_without_src(tmp_path) -> None:
|
|
context = make_context(tmp_path, with_src=False)
|
|
assert context.iter_src_dir("some/dir") == []
|
|
|
|
|
|
def test_iter_src_dir_missing_dir(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
assert context.iter_src_dir("missing-dir") == []
|
|
|
|
|
|
def test_iter_src_dir_lists_only_files(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
target = context.src / "certs"
|
|
target.mkdir()
|
|
(target / "a.crt").write_bytes(b"a")
|
|
(target / "b.crt").write_bytes(b"b")
|
|
(target / "subdir").mkdir()
|
|
result = context.iter_src_dir("certs")
|
|
assert [entry.name for entry in result] == ["a.crt", "b.crt"]
|
|
|
|
|
|
def test_glob_src_without_src(tmp_path) -> None:
|
|
context = make_context(tmp_path, with_src=False)
|
|
assert context.glob_src("usr/lib/python3.*") == []
|
|
|
|
|
|
def test_glob_src_matches_pattern(tmp_path) -> None:
|
|
context = make_context(tmp_path)
|
|
(context.src / "usr/lib/python3.11").mkdir(parents=True)
|
|
(context.src / "usr/lib/python3.12").mkdir(parents=True)
|
|
(context.src / "usr/lib/python-legacy").mkdir(parents=True)
|
|
result = context.glob_src("usr/lib/python3.*")
|
|
assert [entry.name for entry in result] == ["python3.11", "python3.12"]
|