"""Packaged exporter discovery and user-data isolation.""" import os import sys import tempfile from pathlib import Path from unittest import TestCase, main, mock import archive_auto_backup as backup class PackagedExporterPathsTest(TestCase): def test_fresh_database_initialization(self): from packaging_checks import check_database_initialization check_database_initialization() def test_exporter_import_does_not_replace_desktop_crypto(self): import wxwork_crypto original_path = sys.path[:] exporter_root = Path(__file__).resolve().parent.parent / backup.EXACT_EXPORTER_DIR exporter, media = backup._load_exporter_modules(exporter_root) self.assertTrue(callable(exporter.decrypt_wxwork_database)) self.assertTrue(callable(media.export_media)) self.assertIs(sys.modules["wxwork_crypto"], wxwork_crypto) self.assertTrue(callable(wxwork_crypto.decrypt_db_to_file)) self.assertEqual(sys.path, original_path) def test_failed_exporter_import_restores_host_modules(self): import wxwork_crypto original_path = sys.path[:] with tempfile.TemporaryDirectory() as temporary, \ mock.patch.object(backup.importlib, "import_module", side_effect=ImportError("probe")): with self.assertRaises(ImportError): backup._load_exporter_modules(Path(temporary)) self.assertIs(sys.modules["wxwork_crypto"], wxwork_crypto) self.assertEqual(sys.path, original_path) def test_windows_powershell_scripts_have_utf8_bom(self): project = Path(__file__).resolve().parent for name in ("build_windows_exe.ps1", "build_installer.ps1", "sign_artifact.ps1"): with self.subTest(script=name): self.assertTrue((project / name).read_bytes().startswith(b"\xef\xbb\xbf")) def test_bundle_is_used_without_source_checkout(self): with tempfile.TemporaryDirectory() as temporary: bundle = Path(temporary) / "archive_exporter" bundle.mkdir() (bundle / "wxwork_export_final.py").touch() with mock.patch.dict(os.environ, {"WECOM_ARCHIVE_EXPORTER_DIR": ""}), \ mock.patch.object(backup, "is_frozen", return_value=True), \ mock.patch.object(backup, "resource_path", return_value=bundle): self.assertEqual(backup._discover_exporter_root(), bundle) (bundle / "wxwork_export_final.py").unlink() self.assertIsNone(backup._discover_exporter_root()) def test_packaged_settings_use_persistent_data_directory(self): with tempfile.TemporaryDirectory() as temporary: bundle = Path(temporary) / "archive_exporter" data = Path(temporary) / "user-data" with mock.patch.object(backup, "is_frozen", return_value=True), \ mock.patch.object(backup, "resource_path", return_value=bundle), \ mock.patch.object(backup, "application_data_dir", return_value=data): self.assertEqual( backup._exporter_state_path(bundle, "wxwork_keys.json"), data / "wxwork_keys.json", ) backup._persist_source_root(bundle, Path(temporary) / "WXWork") self.assertTrue((data / "wxwork_gui_config.json").is_file()) self.assertFalse(bundle.exists()) def test_explicit_exporter_override_is_preserved(self): with tempfile.TemporaryDirectory() as temporary: external = Path(temporary) (external / "wxwork_export_final.py").touch() with mock.patch.dict(os.environ, {"WECOM_ARCHIVE_EXPORTER_DIR": temporary}), \ mock.patch.object(backup, "is_frozen", return_value=True): self.assertEqual(backup._discover_exporter_root(), external.resolve()) self.assertEqual( backup._exporter_state_path(external, "wxwork_keys.json"), external / "wxwork_keys.json", ) if __name__ == "__main__": main()