212 lines
7.0 KiB
Python
212 lines
7.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.trial_task_center.errors import TaskCenterError
|
|
from dx_productivity.trial_task_center.path_access import strip_extended_prefix
|
|
from dx_productivity.trial_task_center.scanner import scan_projects
|
|
|
|
|
|
SCHEMATIC_PROJECT = '''SECTION DesignInfo
|
|
KEY FrontEndSnapshot "DxD"
|
|
ENDSECTION
|
|
SECTION iCDB
|
|
KEY iCDBDir ".\\database"
|
|
ENDSECTION
|
|
'''
|
|
|
|
|
|
def _write_schematic_project(path: Path) -> None:
|
|
path.write_text(SCHEMATIC_PROJECT, encoding="utf-8")
|
|
|
|
|
|
def test_scan_projects_discovers_case_insensitive_prj_files_in_canonical_order(tmp_path: Path) -> None:
|
|
root = tmp_path / "projects"
|
|
first = root / "z" / "B.PRJ"
|
|
second = root / "a" / "a.PrJ"
|
|
first.parent.mkdir(parents=True)
|
|
second.parent.mkdir()
|
|
_write_schematic_project(first)
|
|
_write_schematic_project(second)
|
|
before = sorted(path.relative_to(root) for path in root.rglob("*"))
|
|
|
|
result = scan_projects(root)
|
|
|
|
assert result == tuple(sorted((first.resolve(), second.resolve()), key=lambda path: str(path).casefold()))
|
|
assert sorted(path.relative_to(root) for path in root.rglob("*")) == before
|
|
|
|
|
|
def test_scan_projects_stops_at_selected_project_directory_when_it_contains_prj_files(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Descending past a directly selected project can expose backups as separate projects."""
|
|
root = tmp_path / "selected-project"
|
|
first = root / "A.PRJ"
|
|
second = root / "Board.prj"
|
|
nested_backup = root / "backup" / "Board-old.prj"
|
|
nested_generated = root / "generated" / "Board-copy.PRJ"
|
|
nested_backup.parent.mkdir(parents=True)
|
|
nested_generated.parent.mkdir()
|
|
_write_schematic_project(first)
|
|
_write_schematic_project(second)
|
|
_write_schematic_project(nested_backup)
|
|
_write_schematic_project(nested_generated)
|
|
real_scandir = os.scandir
|
|
|
|
def reject_nested(path: str | bytes | int) -> object:
|
|
if Path(strip_extended_prefix(path)) != root:
|
|
raise AssertionError(f"unexpected nested scan: {path}")
|
|
return real_scandir(path)
|
|
|
|
monkeypatch.setattr("dx_productivity.trial_task_center.scanner.os.scandir", reject_nested)
|
|
|
|
result = scan_projects(root)
|
|
|
|
assert result == (first.resolve(), second.resolve())
|
|
|
|
|
|
def test_scan_projects_reads_batch_root_once_before_recursive_fallback(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Enumerating a live root twice can mix two different filesystem snapshots."""
|
|
root = tmp_path / "projects"
|
|
child = root / "child"
|
|
project = child / "Board.prj"
|
|
child.mkdir(parents=True)
|
|
_write_schematic_project(project)
|
|
real_scandir = os.scandir
|
|
root_reads = 0
|
|
|
|
def count_root(path: str | bytes | int) -> object:
|
|
nonlocal root_reads
|
|
if Path(strip_extended_prefix(path)) == root:
|
|
root_reads += 1
|
|
return real_scandir(path)
|
|
|
|
monkeypatch.setattr("dx_productivity.trial_task_center.scanner.os.scandir", count_root)
|
|
|
|
result = scan_projects(root)
|
|
|
|
assert result == (project.resolve(),)
|
|
assert root_reads == 1
|
|
|
|
|
|
def test_scan_projects_skips_portable_task_data_and_its_backups(tmp_path: Path) -> None:
|
|
root = tmp_path / "DxPartReplace"
|
|
project = root / "projects" / "Board" / "Board.prj"
|
|
backup = root / "data" / "quick-run-history" / "batch-test" / "project-1" / "Board.prj"
|
|
project.parent.mkdir(parents=True)
|
|
backup.parent.mkdir(parents=True)
|
|
_write_schematic_project(project)
|
|
_write_schematic_project(backup)
|
|
|
|
assert scan_projects(root, excluded_roots=(root / "data",)) == (project.resolve(),)
|
|
|
|
|
|
def test_scan_projects_rejects_task_data_as_the_selected_root(tmp_path: Path) -> None:
|
|
data = tmp_path / "data"
|
|
data.mkdir()
|
|
|
|
with pytest.raises(TaskCenterError) as raised:
|
|
scan_projects(data, excluded_roots=(data,))
|
|
|
|
assert raised.value.code == "discovery_root"
|
|
|
|
|
|
def test_scan_projects_wraps_invalid_roots_without_losing_input_error_code(tmp_path: Path) -> None:
|
|
missing = tmp_path / "missing"
|
|
file_root = tmp_path / "not-a-directory.prj"
|
|
_write_schematic_project(file_root)
|
|
|
|
for root in (missing, file_root):
|
|
with pytest.raises(TaskCenterError) as raised:
|
|
scan_projects(root)
|
|
assert raised.value.code == "discovery_root"
|
|
|
|
|
|
def test_scan_projects_turns_inaccessible_subtree_errors_into_explicit_task_errors(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
root = tmp_path / "projects"
|
|
denied = root / "denied"
|
|
denied.mkdir(parents=True)
|
|
real_scandir = __import__("os").scandir
|
|
|
|
def deny(path: str | bytes | int) -> object:
|
|
if Path(strip_extended_prefix(path)) == denied:
|
|
raise PermissionError("denied subtree")
|
|
return real_scandir(path)
|
|
|
|
monkeypatch.setattr("dx_productivity.trial_task_center.scanner.os.scandir", deny)
|
|
|
|
with pytest.raises(TaskCenterError) as raised:
|
|
scan_projects(root)
|
|
|
|
assert raised.value.code == "discovery_access"
|
|
|
|
|
|
@pytest.mark.skipif(os.name != "nt", reason="Windows junction coverage")
|
|
def test_scan_projects_rejects_junction_subtrees_without_scanning_them(tmp_path: Path) -> None:
|
|
root = tmp_path / "projects"
|
|
outside = tmp_path / "outside"
|
|
junction = root / "junction"
|
|
normal = root / "normal.prj"
|
|
outside_project = outside / "outside.prj"
|
|
root.mkdir()
|
|
outside.mkdir()
|
|
_write_schematic_project(normal)
|
|
_write_schematic_project(outside_project)
|
|
created = subprocess.run(
|
|
["cmd", "/c", "mklink", "/J", str(junction), str(outside)],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
if created.returncode != 0:
|
|
pytest.skip(f"junctions unavailable: {created.stderr or created.stdout}")
|
|
try:
|
|
with pytest.raises(TaskCenterError) as raised:
|
|
scan_projects(root)
|
|
finally:
|
|
junction.rmdir()
|
|
|
|
assert raised.value.code == "discovery_reparse"
|
|
assert normal.exists()
|
|
assert outside_project.exists()
|
|
|
|
|
|
def test_scan_projects_ignores_pcb_and_other_prj_files(tmp_path: Path) -> None:
|
|
root = tmp_path / "projects"
|
|
schematic = root / "Schematic.prj"
|
|
pcb = root / "PCB.prj"
|
|
vivado = root / "vlog.prj"
|
|
schematic.parent.mkdir(parents=True)
|
|
_write_schematic_project(schematic)
|
|
pcb.write_text(
|
|
'SECTION DesignInfo\nKEY FrontEndSnapshot "PCB"\nENDSECTION\n'
|
|
'SECTION Board\nKEY PCBDesignPath "PCB\\Board.pcb"\nENDSECTION\n',
|
|
encoding="utf-8",
|
|
)
|
|
vivado.write_text("verilog work source.v\n", encoding="utf-8")
|
|
|
|
assert scan_projects(root) == (schematic.resolve(),)
|
|
|
|
|
|
def test_scan_projects_keeps_schematic_projects_that_also_contain_pcb_configuration(tmp_path: Path) -> None:
|
|
root = tmp_path / "projects"
|
|
integrated = root / "Integrated.prj"
|
|
integrated.parent.mkdir(parents=True)
|
|
integrated.write_text(
|
|
SCHEMATIC_PROJECT
|
|
+ 'SECTION Board1\nKEY ConfigType "PCB"\nKEY PCBDesignPath "PCB\\Board1.pcb"\nENDSECTION\n',
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert scan_projects(root) == (integrated.resolve(),)
|