44 行
1.4 KiB
Python
44 行
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from tests.helpers import install_runtime_stubs
|
|
|
|
install_runtime_stubs()
|
|
|
|
from services.download import JMDownloadService # noqa: E402
|
|
from services.errors import DownloadError # noqa: E402
|
|
from services.settings import PluginSettings # noqa: E402
|
|
|
|
|
|
class CleanupTest(unittest.TestCase):
|
|
def test_cleanup_old_work_dirs_removes_children_only(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
settings = PluginSettings.from_config(
|
|
{"cleanup_retention_days": 0},
|
|
Path(temp_dir) / "data",
|
|
)
|
|
settings.ensure_dirs()
|
|
child = settings.work_root / "old-job"
|
|
child.mkdir(parents=True)
|
|
|
|
removed = JMDownloadService(settings).cleanup_old_work_dirs()
|
|
|
|
self.assertEqual(removed, 1)
|
|
self.assertFalse(child.exists())
|
|
|
|
def test_cleanup_refuses_path_outside_work_root(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
root = Path(temp_dir)
|
|
settings = PluginSettings.from_config({}, root / "data")
|
|
settings.ensure_dirs()
|
|
service = JMDownloadService(settings)
|
|
|
|
with self.assertRaises(DownloadError):
|
|
service._remove_work_dir(root)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|