File size: 3,167 Bytes
f9bc0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Skip guards for tests that read machine-local artifacts that are not in git.

Some tests audit real packs, fonts, runs or sibling checkouts that live only on a workstation
(ignored `data/`, `outputs/`, `runs/`, `/tmp`, `../nepal-ttf2utf`). On a clean clone those
inputs do not exist, so the tests skip with the exact missing paths instead of failing.

The guards refuse to cover committed files: a path inside the repository must be local-only
(ignored `data/`, `outputs/`, `runs/` or `spaces/limbu-ocr/models/`, and not tracked by git). A missing tracked file
still fails the test.
"""

from __future__ import annotations

import re
import subprocess
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]
LOCAL_ONLY_PREFIXES = ("data/", "outputs/", "runs/", "spaces/limbu-ocr/models/")
TRACKED_DATA_PREFIXES = ("data/dictionaries/", "data/mappings/")
_MISSING_PATH_PATTERNS = (
    re.compile(r"path does not exist for [^:]+: (?P<path>.+)$"),
    re.compile(r"does not exist: (?P<path>.+)$"),
    re.compile(r"is not a directory: (?P<path>.+)$"),
    re.compile(r"missing local path: .*?\.path=(?P<path>.+)$"),
)


def _is_tracked(relative: str) -> bool:
    if not (ROOT / ".git").exists():
        return False
    result = subprocess.run(
        ["git", "-C", str(ROOT), "ls-files", "--error-unmatch", relative],
        capture_output=True,
        text=True,
        check=False,
    )
    return result.returncode == 0


def is_local_only(path: str | Path) -> bool:
    """True when ``path`` is outside the repo, or an untracked path under data/, outputs/ or runs/."""
    candidate = Path(path)
    if not candidate.is_absolute():
        candidate = ROOT / candidate
    try:
        relative = candidate.resolve().relative_to(ROOT).as_posix()
    except ValueError:
        return True
    if relative.startswith(TRACKED_DATA_PREFIXES) or not relative.startswith(LOCAL_ONLY_PREFIXES):
        return False
    return not _is_tracked(relative)


def require_local_artifacts(*paths: str | Path, reason: str) -> None:
    """Skip (module or test) when any of ``paths`` is missing; each path must be local-only."""
    committed = [str(path) for path in paths if not is_local_only(path)]
    if committed:
        raise ValueError(f"require_local_artifacts is only for local-only inputs; committed paths: {committed}")
    missing = [str(path) for path in paths if not Path(path).exists()]
    if missing:
        pytest.skip(f"{reason}; local-only inputs missing: {missing}", allow_module_level=True)


def skip_if_only_local_artifacts_missing(errors: list[str], *, reason: str) -> None:
    """Skip when every validator error is a missing local-only path; otherwise return and let the test fail."""
    if not errors:
        return
    missing: list[str] = []
    for error in errors:
        match = next((m for m in (p.search(str(error)) for p in _MISSING_PATH_PATTERNS) if m), None)
        if match is None or not is_local_only(match.group("path").strip()):
            return
        missing.append(match.group("path").strip())
    pytest.skip(f"{reason}; {len(missing)} local-only inputs missing, e.g. {missing[:3]}")