mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-11 13:17:07 +00:00
Development worktrees carry empty .claude/.cc-writes directories in role source trees; the one-call unchanged check gave up on any directory without files, so the docs tooling copy fell back to per-file stat and copy round trips (about 56 s on the echoserver production deploy). file_checksums now also reports directories (isdir, mode), and the check asks for every leaf directory in the same remote call: it settles only when each leaf exists as a real directory and, when directory_mode is set, carries exactly that mode. With directory_mode unset the stock copy would not touch an existing directory either (file module with mode None). A missing directory, a mode mismatch, a file or symlink in its place, or a symbolic directory_mode with leaves still runs the regular copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import importlib.util
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ansible.module_utils.testing import patch_module_args
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
if TYPE_CHECKING:
|
|
from types import ModuleType
|
|
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
MODULE_PATH = PROJECT_ROOT / "library" / "file_checksums.py"
|
|
|
|
|
|
def _load_file_checksums() -> ModuleType:
|
|
spec = importlib.util.spec_from_file_location(
|
|
"library_file_checksums", str(MODULE_PATH)
|
|
)
|
|
if spec is None or spec.loader is None:
|
|
raise ImportError(f"Could not load: {MODULE_PATH}")
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod) # type: ignore[attr-defined]
|
|
return mod
|
|
|
|
|
|
_MOD = _load_file_checksums()
|
|
|
|
|
|
class PathStateTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.root = Path(self._tmp.name)
|
|
with patch_module_args({"paths": []}):
|
|
self.module = AnsibleModule(
|
|
argument_spec=_MOD.ARGUMENT_SPEC, supports_check_mode=True
|
|
)
|
|
|
|
def tearDown(self):
|
|
self._tmp.cleanup()
|
|
|
|
def _state(self, path):
|
|
return _MOD.path_state(self.module, str(path))
|
|
|
|
def test_regular_file_reports_sha1_and_mode(self):
|
|
target = self.root / "icon.png"
|
|
target.write_bytes(b"png")
|
|
target.chmod(0o640)
|
|
|
|
state = self._state(target)
|
|
|
|
self.assertEqual(
|
|
state,
|
|
{
|
|
"isreg": True,
|
|
"isdir": False,
|
|
"mode": "0640",
|
|
"checksum": hashlib.sha1(b"png").hexdigest(), # noqa: S324 - the checksum Ansible copy compares is SHA-1
|
|
},
|
|
)
|
|
|
|
def test_missing_path_is_neither_regular_nor_a_directory(self):
|
|
self.assertEqual(
|
|
self._state(self.root / "absent"),
|
|
{"isreg": False, "isdir": False, "mode": None, "checksum": None},
|
|
)
|
|
|
|
def test_directory_is_not_regular(self):
|
|
(self.root / "nested").mkdir()
|
|
|
|
self.assertFalse(self._state(self.root / "nested")["isreg"])
|
|
|
|
def test_directory_reports_its_mode_without_checksum(self):
|
|
target = self.root / ".cc-writes"
|
|
target.mkdir()
|
|
target.chmod(0o1750)
|
|
|
|
self.assertEqual(
|
|
self._state(target),
|
|
{"isreg": False, "isdir": True, "mode": "1750", "checksum": None},
|
|
)
|
|
|
|
def test_symlink_is_reported_not_followed(self):
|
|
target = self.root / "real"
|
|
target.write_bytes(b"data")
|
|
(self.root / "link").symlink_to(target)
|
|
|
|
self.assertFalse(self._state(self.root / "link")["isreg"])
|
|
|
|
def test_symlink_to_a_directory_is_not_a_directory(self):
|
|
(self.root / "real").mkdir()
|
|
(self.root / "link").symlink_to(self.root / "real")
|
|
|
|
self.assertEqual(
|
|
self._state(self.root / "link"),
|
|
{"isreg": False, "isdir": False, "mode": None, "checksum": None},
|
|
)
|
|
|
|
def test_path_below_a_file_is_not_regular(self):
|
|
(self.root / "file").write_bytes(b"x")
|
|
|
|
self.assertFalse(self._state(self.root / "file" / "child")["isreg"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|