import pytest from mentor_pdf_export.models import ExportOptions from mentor_pdf_export.workspace import prepare_workspace, fingerprint def test_copy_changes_profile_not_original(project_factory, tmp_path): source = project_factory() (source.parent / "dxpdf.ini").write_text("Color 1\nUrlProperties Value\n") (source.parent / "PCB").mkdir() (source.parent / "PCB" / "private.bin").write_bytes(b"pcb") before = fingerprint(source.parent) copied = prepare_workspace(source, tmp_path / "runs" / "one", ExportOptions()) assert copied.is_file() assert not (copied.parent / "PCB").exists() assert "Color 4" in (copied.parent / "dxpdf.ini").read_text() assert before == fingerprint(source.parent) def test_rejects_database_outside_project(project_factory, tmp_path): project = project_factory() project.write_text(project.read_text().replace('.\\database', '..\\other')) with pytest.raises(ValueError, match="数据库|database"): prepare_workspace(project, tmp_path / "run", ExportOptions()) def test_rejects_workspace_inside_source(project_factory): source = project_factory() with pytest.raises(ValueError, match="副本|workspace"): prepare_workspace(source, source.parent / "run", ExportOptions()) def test_library_override_only_changes_copy(project_factory, tmp_path): source = project_factory() source.write_text(source.read_text() + 'KEY CentralLibrary "\\\\oldserver\\lib\\mentor.lmc"\n') library = tmp_path / "library.lmc";library.write_bytes(b"sample library") before = fingerprint(source.parent) copied = prepare_workspace(source, tmp_path / "run", ExportOptions(library_override=str(library))) assert str(library).replace("\\", "/") in copied.read_text().replace("\\", "/") assert fingerprint(source.parent) == before assert library.read_bytes() == b"sample library" def test_missing_library_override_rejected(project_factory, tmp_path): source = project_factory() with pytest.raises(ValueError, match="中心库"): prepare_workspace(source, tmp_path / "run", ExportOptions(library_override=str(tmp_path / "missing.lmc"))) def test_library_override_preserves_next_project_line(project_factory, tmp_path): source = project_factory() source.write_text(source.read_text() + 'KEY CentralLibrary "\\\\oldserver\\lib\\mentor.lmc"\nKEY DxD_Version "7.9.4"\n') library=tmp_path/"local.lmc";library.write_bytes(b"lib") copied=prepare_workspace(source,tmp_path/"run",ExportOptions(library_override=str(library))) lines=copied.read_text().splitlines() assert 'KEY DxD_Version "7.9.4"' in lines def test_empty_library_override_preserves_project_bytes(project_factory,tmp_path): source=project_factory() source.write_text(source.read_text()+'KEY CentralLibrary "\\\\server\\library\\original.lmc"\n') before=source.read_bytes() copied=prepare_workspace(source,tmp_path/"run",ExportOptions(library_override="")) assert copied.read_bytes()==before assert source.read_bytes()==before def test_compare_fingerprints_reports_sorted_added_removed_and_modified(): from mentor_pdf_export.workspace import compare_fingerprints before = {"stable": "same", "db/b": "old", "db/a": "old", "deleted": "gone"} after = {"stable": "same", "db/b": "new", "db/a": "new", "new/z": "z", "new/a": "a"} assert compare_fingerprints(before, after) == { "modified": ["db/a", "db/b"], "added": ["new/a", "new/z"], "removed": ["deleted"] } assert compare_fingerprints(before, before) == {"modified": [], "added": [], "removed": []}