"""The path rewrite, exercised without a database. rewrite() is pure on purpose: the risky part of this script is deciding WHICH paths to touch, and that judgement should be testable without standing up an app context. """ import importlib.util from pathlib import Path import pytest SCRIPT = Path(__file__).resolve().parent.parent / 'scripts' / 'fix_drive_letter_sharepaths.py' def _rewrite(*args, **kwargs): # Imported lazily and by path: scripts/ is not a package, and importing the # module runs load_dotenv + create_app imports at module scope. spec = importlib.util.spec_from_file_location('fixsharepaths', SCRIPT) module = importlib.util.module_from_spec(spec) try: spec.loader.exec_module(module) except Exception as exc: # pragma: no cover - env-dependent pytest.skip('script imports need an app environment: {}'.format(exc)) return module.rewrite(*args, **kwargs) # Generic on purpose: a real site hostname in a test is still a site # hostname in the published repo (ADR-015), and the rewrite does not care # which server it is. UNC = r'\\fileserver\share\path' def test_a_mapped_drive_path_becomes_its_unc_equivalent(): got = _rewrite(r'W:\backup\partmarker\WJPRT\20260825-144500', 'W:', UNC) assert got == UNC + r'\backup\partmarker\WJPRT\20260825-144500' def test_a_path_that_is_already_unc_is_left_alone(): """The script repairs a known mistake; it does not normalise what it finds.""" assert _rewrite(UNC + r'\backup\partmarker\WJPRT\x', 'W:', UNC) is None def test_another_drive_is_left_alone(): assert _rewrite(r'S:\DT\Shopfloor\backup\x', 'W:', UNC) is None def test_an_empty_or_missing_path_is_left_alone(): assert _rewrite('', 'W:', UNC) is None assert _rewrite(None, 'W:', UNC) is None def test_the_drive_letter_match_is_case_insensitive(): assert _rewrite(r'w:\backup\x', 'W:', UNC) == UNC + r'\backup\x' def test_a_trailing_backslash_on_the_unc_does_not_double_up(): assert _rewrite(r'W:\backup\x', 'W:', UNC + '\\') == UNC + r'\backup\x'