diff --git a/dlccore/project/schemas/base/finding_codes.py b/dlccore/project/schemas/base/finding_codes.py index bbba2b6..38f2cdf 100644 --- a/dlccore/project/schemas/base/finding_codes.py +++ b/dlccore/project/schemas/base/finding_codes.py @@ -144,6 +144,19 @@ class ConfigFindingCode(FindingCode): Severity.NOTE, ("default",), ) + CONFIG_PROJECT_PATH_MISMATCH = ( + "CONFIG_PROJECT_PATH_MISMATCH", + "The project path declared in config.yaml does not match the project root observed. A declared path " + "probed as missing may also be an unreachable network location or a path from another platform.", + Severity.NOTE, + ("declared_project_path", "observed_project_root", "declared_path_probe", "error_type", "error_message"), + ) + CONFIG_PROJECT_PATH_NOT_ABSOLUTE = ( + "CONFIG_PROJECT_PATH_NOT_ABSOLUTE", + "The project path in config.yaml is missing, empty or relative, and cannot be compared with the project root.", + Severity.NOTE, + ("declared_project_path",), + ) def iter_finding_codes() -> tuple[FindingCode, ...]: diff --git a/dlccore/project/validation/deeplabcut/project_path.py b/dlccore/project/validation/deeplabcut/project_path.py new file mode 100644 index 0000000..14b6595 --- /dev/null +++ b/dlccore/project/validation/deeplabcut/project_path.py @@ -0,0 +1,93 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from __future__ import annotations + +from pathlib import Path + +from dlccore.project.discovery.probe import file_identity, probe_path +from dlccore.project.schemas.base.core import is_absolute_path +from dlccore.project.schemas.base.finding_codes import ConfigFindingCode +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.base.probes import PathProbeStatus +from dlccore.project.schemas.locations import one_per_project +from dlccore.project.schemas.project_layout.project import PROJECT_CONFIG +from dlccore.project.validation.context import ValidationContext + + +def validate_project_path( + context: ValidationContext, +) -> tuple[Finding, ...]: + """Compare the config's declared project_path with the inspected project root. + + A missing, empty or relative project_path is reported as ``CONFIG_PROJECT_PATH_NOT_ABSOLUTE``. + A path counts as absolute if it is absolute on Windows or on POSIX; a relative one is never + resolved against the working directory. + + An absolute path matches when it names the same directory as the root, following symlinks. + Otherwise ``CONFIG_PROJECT_PATH_MISMATCH`` is reported with the declared path as written, the + result of probing it (missing, observed or inaccessible) and any error. A path absolute only on + the other platform is not probed, and those three are ``None``. + + This check is read-only and never writes the configuration. + """ + + observation = context.snapshot.config_observation + declared_path = observation.declared_project_path + + if declared_path is None or not is_absolute_path(declared_path): + return ( + Finding( + code=ConfigFindingCode.CONFIG_PROJECT_PATH_NOT_ABSOLUTE, + **one_per_project(PROJECT_CONFIG), + path=observation.config_path, + field_location=("project_path",), + evidence={ + "declared_project_path": declared_path, + }, + ), + ) + + observed_path = context.snapshot.project_root.absolute() + + if Path(declared_path) == observed_path: + return () + + probe_status = error_type = error_message = None + + # Absolute only on the other platform: here it would resolve against the working directory. + if Path(declared_path).is_absolute(): + probe = probe_path(Path(declared_path)) + probe_status, error_type, error_message = probe.status.value, probe.error_type, probe.error_message + + if probe.status == PathProbeStatus.OBSERVED: + try: + if file_identity(Path(declared_path)) == file_identity(observed_path): + return () + except (OSError, ValueError) as error: + # The entry exists but cannot be compared, such as a link whose target is gone. + error_type, error_message = type(error).__name__, str(error) + + return ( + Finding( + code=ConfigFindingCode.CONFIG_PROJECT_PATH_MISMATCH, + **one_per_project(PROJECT_CONFIG), + path=observation.config_path, + field_location=("project_path",), + evidence={ + "declared_project_path": declared_path, + "observed_project_root": str(observed_path), + "declared_path_probe": probe_status, + "error_type": error_type, + "error_message": error_message, + }, + ), + ) diff --git a/tests/project/inspection/test_policy.py b/tests/project/inspection/test_policy.py index 98ed30a..1668a1d 100644 --- a/tests/project/inspection/test_policy.py +++ b/tests/project/inspection/test_policy.py @@ -11,7 +11,72 @@ from __future__ import annotations +from collections.abc import Callable + +import pytest + from dlccore.project import api, policy +from dlccore.project.policy import get_blocking_findings, unhandled +from dlccore.project.schemas.base.finding_codes import ConfigFindingCode, FindingCode +from dlccore.project.schemas.base.findings import Finding, Reach + +ERROR = ConfigFindingCode.CONFIG_PARSE_FAILED +WARNING = ConfigFindingCode.CONFIG_NO_KEYPOINTS +NOTE = ConfigFindingCode.CONFIG_PROJECT_PATH_MISMATCH + + +def _finding(code: FindingCode) -> Finding: + return Finding( + code=code, + schema_id="dlc.project.config", + reach=Reach.ONE_PER_PROJECT, + evidence=dict.fromkeys(code.evidence_keys, "placeholder"), + ) + + +FINDINGS = (_finding(NOTE), _finding(ERROR), _finding(WARNING)) + + +def _codes(findings: tuple[Finding, ...]) -> list[FindingCode]: + return [finding.code for finding in findings] + + +def test_by_default_only_blocking_severities_block() -> None: + assert _codes(get_blocking_findings(FINDINGS)) == [ERROR] + + +def test_a_consumer_blocks_more_codes_and_keeps_snapshot_order() -> None: + assert _codes(get_blocking_findings(FINDINGS, block={WARNING, NOTE})) == [NOTE, ERROR, WARNING] + + +def test_a_consumer_allows_a_code_the_default_blocks() -> None: + assert _codes(get_blocking_findings(FINDINGS, allow={ERROR})) == [] + + +def test_a_code_cannot_be_both_blocked_and_allowed() -> None: + with pytest.raises(ValueError, match="both blocked and allowed"): + get_blocking_findings(FINDINGS, block={WARNING}, allow={WARNING}) + + +def test_unhandled_reads_the_keys_of_a_handling_table() -> None: + handled = {NOTE: "project was moved: use the inspected folder"} + + assert _codes(unhandled(FINDINGS, handled)) == [ERROR, WARNING] + + +@pytest.mark.parametrize( + "call", + [ + lambda: get_blocking_findings(FINDINGS, block={"CONFIG_NO_KEYPOINTS"}), + lambda: get_blocking_findings(FINDINGS, allow={"CONFIG_PARSE_FAILED"}), + lambda: unhandled(FINDINGS, {"CONFIG_PROJECT_PATH_MISMATCH": "moved"}), + ], +) +def test_a_code_named_by_string_raises(call: Callable[[], object]) -> None: + """A string would still match today, and silently stop matching once the code is renamed.""" + + with pytest.raises(TypeError, match="FindingCode members"): + call() def test_the_api_helpers_reexport_the_policy_helpers() -> None: