from pathlib import Path import subprocess from edkb_system.skills import soffice_util def test_convert_with_soffice_uses_isolated_profile_and_cleans(monkeypatch, tmp_path): profile_dir = tmp_path / "lo-profile-fixed" def fake_mkdtemp(prefix=""): profile_dir.mkdir(parents=True, exist_ok=True) return str(profile_dir) monkeypatch.setenv("EDKB_SOFFICE_ISOLATED_PROFILE", "1") monkeypatch.setattr(soffice_util.tempfile, "mkdtemp", fake_mkdtemp) captured = {} def fake_run(cmd): captured["cmd"] = cmd assert profile_dir.exists() return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="") monkeypatch.setattr(soffice_util, "run_soffice", fake_run) source_path = tmp_path / "sample.doc" source_path.write_text("dummy", encoding="utf-8") out_dir = tmp_path / "out" proc = soffice_util.convert_with_soffice( "soffice", convert_to="docx", source_path=source_path, out_dir=out_dir, ) assert proc.returncode == 0 assert any( str(item).startswith("-env:UserInstallation=") for item in captured["cmd"] ) assert not profile_dir.exists() def test_convert_with_soffice_can_use_shared_profile(monkeypatch, tmp_path): called = {"mkdtemp": 0} def fake_mkdtemp(prefix=""): called["mkdtemp"] += 1 return str(tmp_path / "unused-profile") monkeypatch.setenv("EDKB_SOFFICE_ISOLATED_PROFILE", "0") monkeypatch.setattr(soffice_util.tempfile, "mkdtemp", fake_mkdtemp) captured = {} def fake_run(cmd): captured["cmd"] = cmd return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="") monkeypatch.setattr(soffice_util, "run_soffice", fake_run) source_path = tmp_path / "sample.doc" source_path.write_text("dummy", encoding="utf-8") out_dir = tmp_path / "out" proc = soffice_util.convert_with_soffice( "soffice", convert_to="docx", source_path=source_path, out_dir=out_dir, ) assert proc.returncode == 0 assert called["mkdtemp"] == 0 assert not any( str(item).startswith("-env:UserInstallation=") for item in captured["cmd"] )