from pathlib import Path import pytest from app.services import project_inp def test_temporary_project_inp_exports_unique_file_and_removes_it( monkeypatch, tmp_path ) -> None: exported_paths: list[Path] = [] def fake_dump_inp(project_code: str, path: str, version: str) -> None: assert project_code == "demo" assert version == "2" exported_path = Path(path) exported_path.write_text("[TITLE]\ndemo", encoding="utf-8") exported_paths.append(exported_path) monkeypatch.setattr(project_inp, "PROJECT_INP_DIRECTORY", tmp_path) monkeypatch.setattr(project_inp, "dump_inp", fake_dump_inp) with project_inp.temporary_project_inp( "demo", purpose="sensor-placement" ) as first_path: assert first_path.read_text(encoding="utf-8") == "[TITLE]\ndemo" with project_inp.temporary_project_inp( "demo", purpose="sensor-placement" ) as second_path: assert second_path.exists() assert first_path != second_path assert exported_paths == [first_path, second_path] assert all(not path.exists() for path in exported_paths) def test_temporary_project_inp_removes_empty_export(monkeypatch, tmp_path) -> None: monkeypatch.setattr(project_inp, "PROJECT_INP_DIRECTORY", tmp_path) monkeypatch.setattr(project_inp, "dump_inp", lambda *_args: None) with pytest.raises(ValueError, match="INP 导出失败"): with project_inp.temporary_project_inp("missing", purpose="burst"): pass assert list(tmp_path.iterdir()) == []