190 lines
6.9 KiB
Python
190 lines
6.9 KiB
Python
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.part_replace.inputs import (
|
|
InputError,
|
|
discover_projects,
|
|
load_mapping,
|
|
load_projects,
|
|
)
|
|
from dx_productivity.part_replace.models import MappingRule, ProjectRef
|
|
|
|
|
|
def write(path: Path, text: str, *, encoding: str = "utf-8") -> Path:
|
|
path.write_text(text, encoding=encoding)
|
|
return path
|
|
|
|
|
|
def test_load_mapping_rejects_case_duplicate_chain_and_cycle(tmp_path: Path) -> None:
|
|
duplicate = write(tmp_path / "duplicate.csv", "old_part_number,new_part_number\nA,B\na,C\n")
|
|
with pytest.raises(InputError, match="duplicate_source"):
|
|
load_mapping(duplicate)
|
|
|
|
chain = write(tmp_path / "chain.csv", "old_part_number,new_part_number\nA,B\nB,C\n")
|
|
with pytest.raises(InputError, match="mapping_chain"):
|
|
load_mapping(chain)
|
|
|
|
cycle = write(tmp_path / "cycle.csv", "old_part_number,new_part_number\nA,B\nB,A\n")
|
|
with pytest.raises(InputError, match="mapping_cycle"):
|
|
load_mapping(cycle)
|
|
|
|
|
|
def test_load_mapping_accepts_utf8_bom_and_preserves_spelling(tmp_path: Path) -> None:
|
|
path = tmp_path / "替换.csv"
|
|
path.write_text(
|
|
"old_part_number,new_part_number\n old-01 , New-01 \n",
|
|
encoding="utf-8-sig",
|
|
)
|
|
assert load_mapping(path) == (MappingRule("old-01", "New-01"),)
|
|
|
|
|
|
def test_load_mapping_requires_exact_two_column_header_and_nonblank_values(tmp_path: Path) -> None:
|
|
for name, contents, code in (
|
|
(
|
|
"missing.csv",
|
|
"old_part_number\nA\n",
|
|
"mapping_header",
|
|
),
|
|
(
|
|
"extra.csv",
|
|
"old_part_number,new_part_number,comment\nA,B,note\n",
|
|
"mapping_header",
|
|
),
|
|
(
|
|
"blank.csv",
|
|
"old_part_number,new_part_number\n ,B\n",
|
|
"blank_part_number",
|
|
),
|
|
):
|
|
with pytest.raises(InputError, match=code):
|
|
load_mapping(write(tmp_path / name, contents))
|
|
|
|
|
|
def test_load_mapping_accepts_named_columns_in_either_order_but_rejects_extra_row_values(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
reversed_header = write(
|
|
tmp_path / "reversed.csv",
|
|
"new_part_number,old_part_number\nB,A\n",
|
|
)
|
|
assert load_mapping(reversed_header) == (MappingRule("A", "B"),)
|
|
|
|
extra_value = write(
|
|
tmp_path / "extra-value.csv",
|
|
"old_part_number,new_part_number\nA,B,unexpected\n",
|
|
)
|
|
with pytest.raises(InputError, match="mapping_columns"):
|
|
load_mapping(extra_value)
|
|
|
|
|
|
def test_load_mapping_rejects_case_insensitive_equal_rule_before_graph_checks(tmp_path: Path) -> None:
|
|
path = write(tmp_path / "equal.csv", "old_part_number,new_part_number\n Part-A , part-a \n")
|
|
with pytest.raises(InputError, match="equal_rule"):
|
|
load_mapping(path)
|
|
|
|
|
|
def test_load_mapping_reports_chain_for_graph_larger_than_recursion_limit(tmp_path: Path) -> None:
|
|
rows = ["old_part_number,new_part_number"]
|
|
rows.extend(f"P{index},P{index + 1}" for index in range(1_200))
|
|
path = write(tmp_path / "long-chain.csv", "\n".join(rows) + "\n")
|
|
|
|
with pytest.raises(InputError, match="mapping_chain"):
|
|
load_mapping(path)
|
|
|
|
|
|
def test_load_mapping_reports_cycle_for_graph_larger_than_recursion_limit(tmp_path: Path) -> None:
|
|
rows = ["old_part_number,new_part_number"]
|
|
rows.extend(f"P{index},P{index + 1}" for index in range(1_199))
|
|
rows.append("P1199,P1")
|
|
path = write(tmp_path / "long-cycle.csv", "\n".join(rows) + "\n")
|
|
|
|
with pytest.raises(InputError, match="mapping_cycle"):
|
|
load_mapping(path)
|
|
|
|
|
|
def test_load_projects_ignores_comments_and_builds_stable_refs(tmp_path: Path) -> None:
|
|
project = tmp_path / "设计.PRJ"
|
|
project.write_text("project definition", encoding="utf-8")
|
|
list_path = write(
|
|
tmp_path / "projects.txt",
|
|
f"\n# operator comment\n {project} \n",
|
|
)
|
|
|
|
canonical = project.resolve()
|
|
normalized = os.path.normcase(str(canonical))
|
|
expected_id = hashlib.sha256(normalized.encode("utf-8")).hexdigest()[:16]
|
|
assert load_projects(list_path, (".prj",)) == (
|
|
ProjectRef(expected_id, str(canonical), "设计"),
|
|
)
|
|
|
|
|
|
def test_load_projects_rejects_relative_missing_directory_suffix_and_duplicate(tmp_path: Path) -> None:
|
|
project = tmp_path / "one.prj"
|
|
project.write_text("project definition", encoding="utf-8")
|
|
(tmp_path / "one.txt").write_text("not a project", encoding="utf-8")
|
|
cases = (
|
|
("relative.prj\n", "project_path_not_absolute"),
|
|
(f"{tmp_path / 'missing.prj'}\n", "project_not_found"),
|
|
(f"{tmp_path}\n", "project_not_file"),
|
|
(f"{tmp_path / 'one.txt'}\n", "unsupported_project_suffix"),
|
|
)
|
|
for contents, code in cases:
|
|
with pytest.raises(InputError, match=code):
|
|
load_projects(write(tmp_path / f"{code}.txt", contents), (".prj",))
|
|
|
|
duplicate_list = write(tmp_path / "duplicates.txt", f"{project}\n{project.resolve()}\n")
|
|
with pytest.raises(InputError, match="duplicate_project"):
|
|
load_projects(duplicate_list, (".prj",))
|
|
|
|
|
|
def test_load_projects_accepts_suffix_case_insensitively_and_resolves_paths(tmp_path: Path) -> None:
|
|
project = tmp_path / "sub" / "Board.PrJ"
|
|
project.parent.mkdir()
|
|
project.write_text("project definition", encoding="utf-8")
|
|
listed = tmp_path / "projects.txt"
|
|
write(listed, f"{project}\n")
|
|
|
|
result = load_projects(listed, (".PRJ",))
|
|
assert result[0].project_path == str(project.resolve())
|
|
assert result[0].display_name == "Board"
|
|
|
|
|
|
@pytest.mark.parametrize("nested", (False, True), ids=("same-root", "nested-root"))
|
|
def test_load_projects_rejects_overlapping_project_ownership_roots(
|
|
tmp_path: Path,
|
|
nested: bool,
|
|
) -> None:
|
|
# Catches one project's backup or restore tree containing another project marker.
|
|
first_root = tmp_path / "projects" / "first"
|
|
second_root = first_root / "nested" if nested else first_root
|
|
second_root.mkdir(parents=True)
|
|
first = first_root / "first.prj"
|
|
second = second_root / "second.prj"
|
|
first.write_text("first\n", encoding="utf-8")
|
|
second.write_text("second\n", encoding="utf-8")
|
|
projects = tmp_path / "projects.txt"
|
|
projects.write_text(f"{first.resolve()}\n{second.resolve()}\n", encoding="utf-8")
|
|
|
|
with pytest.raises(InputError, match="overlapping_project_roots"):
|
|
load_projects(projects, (".prj",))
|
|
|
|
|
|
def test_discover_projects_returns_sorted_unique_canonical_candidates_without_writing(tmp_path: Path) -> None:
|
|
root = tmp_path / "设计"
|
|
(root / "z").mkdir(parents=True)
|
|
(root / "a").mkdir()
|
|
first = root / "z" / "B.PRJ"
|
|
second = root / "a" / "a.prj"
|
|
ignored = root / "a" / "notes.txt"
|
|
first.write_text("project", encoding="utf-8")
|
|
second.write_text("project", encoding="utf-8")
|
|
ignored.write_text("ignored", encoding="utf-8")
|
|
|
|
discovered = discover_projects(root, (".prj",))
|
|
expected = tuple(sorted((first.resolve(), second.resolve()), key=lambda path: str(path).casefold()))
|
|
assert discovered == expected
|
|
assert not (root / "projects.txt").exists()
|