Files
xieyi-weixin-/test_packaging_exporter.py
T
2026-09-21 10:56:48 +08:00

56 lines
2.7 KiB
Python

"""Build succeeds without the old machine's sibling exporter directory."""
import os
import tempfile
import unittest
from pathlib import Path
from unittest import mock
import archive_auto_backup as backup
from packaging_exporter import EXPORTER_FILES, exporter_data_files
class ExporterBuildTests(unittest.TestCase):
def test_clean_checkout_uses_only_four_code_files(self):
with tempfile.TemporaryDirectory() as temporary:
project = Path(temporary) / "checkout"
bundled = project / "archive_exporter"
bundled.mkdir(parents=True)
for name in EXPORTER_FILES:
(bundled / name).write_text('# code\n', encoding='utf-8')
for name in ('wxwork_keys.json', 'message.db', 'wxwork_gui_config.json'):
(bundled / name).write_text('private', encoding='utf-8')
result = exporter_data_files(project, override='')
self.assertEqual([Path(source).name for source, _ in result], list(EXPORTER_FILES))
self.assertTrue(all(destination == 'archive_exporter' for _, destination in result))
def test_missing_file_fails_before_pyinstaller(self):
with tempfile.TemporaryDirectory() as temporary:
with self.assertRaisesRegex(FileNotFoundError, 'wxwork_export_final.py'):
exporter_data_files(Path(temporary), override='')
def test_explicit_bad_override_does_not_silently_use_other_source(self):
with tempfile.TemporaryDirectory() as temporary:
with self.assertRaisesRegex(FileNotFoundError, 'WECOM_BUILD_EXPORTER_DIR'):
exporter_data_files(Path(__file__).parent, override=temporary)
def test_source_runtime_uses_project_exporter_without_sibling_search(self):
project = Path(__file__).resolve().parent
with mock.patch.dict(os.environ, {'WECOM_ARCHIVE_EXPORTER_DIR': ''}), \
mock.patch.object(backup, 'is_frozen', return_value=False), \
mock.patch.object(Path, 'iterdir', side_effect=AssertionError('Unexpected sibling scan')):
self.assertEqual(backup._discover_exporter_root(), project / 'archive_exporter')
def test_override_and_invalid_python_source(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
for name in EXPORTER_FILES:
(root / name).write_text('# code\n', encoding='utf-8')
self.assertEqual(len(exporter_data_files(Path('.'), override=temporary)), 4)
(root / EXPORTER_FILES[0]).write_text('def bad(', encoding='utf-8')
with self.assertRaises(SyntaxError):
exporter_data_files(Path('.'), override=temporary)
if __name__ == '__main__':
unittest.main()