mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
feat: initial commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
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" # nosec B101
|
||||
|
||||
|
||||
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 # nosec B101
|
||||
|
||||
|
||||
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" # nosec B101
|
||||
|
||||
|
||||
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" # nosec B101
|
||||
assert path.stat().st_mode & 0o777 == 0o644 # nosec B101
|
||||
|
||||
|
||||
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 # nosec B101
|
||||
|
||||
|
||||
def test_read_src_file_missing(tmp_path) -> None:
|
||||
context = make_context(tmp_path)
|
||||
assert context.read_src_file("etc/missing.conf") is None # nosec B101
|
||||
|
||||
|
||||
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" # nosec B101
|
||||
|
||||
|
||||
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") == [] # nosec B101
|
||||
|
||||
|
||||
def test_iter_src_dir_missing_dir(tmp_path) -> None:
|
||||
context = make_context(tmp_path)
|
||||
assert context.iter_src_dir("missing-dir") == [] # nosec B101
|
||||
|
||||
|
||||
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"] # nosec B101
|
||||
Reference in New Issue
Block a user