test_isolated_workspace.py
3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""U-036: real unittest coverage for isolated_workspace.py -- adapted from
graphloop's test_isolated_coding_workspace.py (2026-08-02). Module logic is
unchanged from graphloop (see isolated_workspace.py's own docstring); only
the import path changed to match this package's location.
Run: python3 -m unittest discover -s supervisor/isolation -p "test_*.py" -v
"""
import os
import shutil
import tempfile
import unittest
from supervisor.isolation import isolated_workspace as iso
class IsolateWorkspaceTests(unittest.TestCase):
def setUp(self):
self.source_dir = tempfile.mkdtemp(prefix="iso-src-")
self.addCleanup(shutil.rmtree, self.source_dir, ignore_errors=True)
self.source_file = os.path.join(self.source_dir, "hello.txt")
with open(self.source_file, "w", encoding="utf-8") as handle:
handle.write("hello world")
self._copies_to_clean = []
self.addCleanup(self._cleanup_copies)
def _cleanup_copies(self):
for path in self._copies_to_clean:
shutil.rmtree(path, ignore_errors=True)
def test_returns_separate_path_leaves_source_unchanged_and_copies_file(self):
copied_dir = iso.isolate_workspace(self.source_dir)
self._copies_to_clean.append(copied_dir)
self.assertNotEqual(os.path.abspath(copied_dir), os.path.abspath(self.source_dir))
self.assertTrue(os.path.isdir(self.source_dir))
with open(self.source_file, "r", encoding="utf-8") as handle:
self.assertEqual(handle.read(), "hello world")
copied_file = os.path.join(copied_dir, "hello.txt")
self.assertTrue(os.path.isfile(copied_file))
with open(copied_file, "r", encoding="utf-8") as handle:
self.assertEqual(handle.read(), "hello world")
def test_parent_dir_overrides_os_default_temp_location(self):
custom_parent = tempfile.mkdtemp(prefix="iso-parent-")
self.addCleanup(shutil.rmtree, custom_parent, ignore_errors=True)
copied_dir = iso.isolate_workspace(self.source_dir, parent_dir=custom_parent)
self._copies_to_clean.append(copied_dir)
self.assertEqual(os.path.dirname(copied_dir), custom_parent)
def test_parent_dir_created_if_missing(self):
base = tempfile.mkdtemp(prefix="iso-parent-")
self.addCleanup(shutil.rmtree, base, ignore_errors=True)
custom_parent = os.path.join(base, "nested", "dir")
copied_dir = iso.isolate_workspace(self.source_dir, parent_dir=custom_parent)
self._copies_to_clean.append(copied_dir)
self.assertTrue(os.path.isdir(custom_parent))
self.assertEqual(os.path.dirname(copied_dir), custom_parent)
def test_rejects_nonexistent_source_dir(self):
with self.assertRaises(iso.IsolatedWorkspaceError):
iso.isolate_workspace(os.path.join(self.source_dir, "does-not-exist"))
def test_rejects_empty_source_dir_argument(self):
with self.assertRaises(iso.IsolatedWorkspaceError):
iso.isolate_workspace("")
if __name__ == "__main__":
unittest.main()