Skip to content

Commit cb1ca2a

Browse files
committed
removed MockDBGymWorkspace
1 parent dde1f14 commit cb1ca2a

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

orchestrate/clean.py

+3-21
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,7 @@
44
from itertools import chain
55
from pathlib import Path
66

7-
from gymlib.workspace import (
8-
DBGymWorkspace,
9-
get_runs_path_from_workspace_path,
10-
get_symlinks_path_from_workspace_path,
11-
is_child_path,
12-
parent_path_of_path,
13-
)
14-
15-
16-
# This is used in test_clean.py. However, we also need it in count_files_in_workspace, so it's defined here to avoid a circular import.
17-
class MockDBGymWorkspace:
18-
def __init__(self, scratchspace_path: Path):
19-
self.dbgym_workspace_path = scratchspace_path
20-
self.dbgym_symlinks_path = get_symlinks_path_from_workspace_path(
21-
scratchspace_path
22-
)
23-
self.dbgym_runs_path = get_runs_path_from_workspace_path(scratchspace_path)
7+
from gymlib.workspace import DBGymWorkspace, is_child_path, parent_path_of_path
248

259

2610
def add_symlinks_in_path(
@@ -39,9 +23,7 @@ def add_symlinks_in_path(
3923
processed_symlinks.add(file_path)
4024

4125

42-
def count_files_in_workspace(
43-
dbgym_workspace: DBGymWorkspace | MockDBGymWorkspace,
44-
) -> int:
26+
def count_files_in_workspace(dbgym_workspace: DBGymWorkspace) -> int:
4527
"""
4628
Counts the number of files (regular file or dir or symlink) in the workspace.
4729
"""
@@ -61,7 +43,7 @@ def count_files_in_workspace(
6143

6244

6345
def clean_workspace(
64-
dbgym_workspace: DBGymWorkspace | MockDBGymWorkspace,
46+
dbgym_workspace: DBGymWorkspace,
6547
mode: str = "safe",
6648
verbose: bool = False,
6749
) -> None:

orchestrate/tests/unittest_clean.py

+35-26
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
make_workspace_structure,
1010
verify_structure,
1111
)
12+
from gymlib.workspace import DBGymWorkspace
1213

13-
from orchestrate.clean import MockDBGymWorkspace, clean_workspace
14+
from orchestrate.clean import clean_workspace
1415

1516
# This is here instead of on `if __name__ == "__main__"` because we often run individual tests, which
1617
# does not go through the `if __name__ == "__main__"` codepath.
@@ -26,26 +27,34 @@ class CleanTests(unittest.TestCase):
2627

2728
@classmethod
2829
def setUpClass(cls) -> None:
29-
cls.scratchspace_path = Path.cwd() / "manage/tests/test_clean_scratchspace/"
30+
cls.scratchspace_path = (
31+
Path.cwd() / "orchestrate/tests/test_clean_scratchspace/"
32+
)
3033
cls.workspace_path = cls.scratchspace_path / "dbgym_workspace"
3134

3235
def setUp(self) -> None:
3336
if self.scratchspace_path.exists():
3437
shutil.rmtree(self.scratchspace_path)
3538

39+
# Reset _num_times_created_this_run since previous tests may have created a workspace.
40+
DBGymWorkspace._num_times_created_this_run = 0
41+
self.workspace = DBGymWorkspace(self.workspace_path)
42+
# Since creating DBGymWorkspace creates the workspace, we want to remove it.
43+
shutil.rmtree(self.workspace_path)
44+
3645
def tearDown(self) -> None:
3746
if self.scratchspace_path.exists():
3847
shutil.rmtree(self.scratchspace_path)
3948

4049
def test_nonexistent_workspace(self) -> None:
4150
# This just ensures that it doesn't raise an exception.
42-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
51+
clean_workspace(self.workspace)
4352

4453
def test_empty_workspace(self) -> None:
4554
starting_structure = FilesystemStructure({"dbgym_workspace": {}})
4655
ending_structure = FilesystemStructure({"dbgym_workspace": {}})
4756
create_structure(self.scratchspace_path, starting_structure)
48-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
57+
clean_workspace(self.workspace)
4958
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
5059

5160
def test_no_symlinks_dir_and_yes_task_runs_dir(self) -> None:
@@ -54,15 +63,15 @@ def test_no_symlinks_dir_and_yes_task_runs_dir(self) -> None:
5463
)
5564
ending_structure = FilesystemStructure({"dbgym_workspace": {"task_runs": {}}})
5665
create_structure(self.scratchspace_path, starting_structure)
57-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
66+
clean_workspace(self.workspace)
5867
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
5968

6069
def test_yes_symlinks_dir_and_no_task_runs_dir(self) -> None:
6170
# If there are no task runs there can't be any symlinks.
6271
starting_structure = FilesystemStructure({"dbgym_workspace": {"symlinks": {}}})
6372
ending_structure = FilesystemStructure({"dbgym_workspace": {"symlinks": {}}})
6473
create_structure(self.scratchspace_path, starting_structure)
65-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
74+
clean_workspace(self.workspace)
6675
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
6776

6877
def test_no_symlinks_in_dir_and_no_task_runs_in_dir(self) -> None:
@@ -78,7 +87,7 @@ def test_no_symlinks_in_dir_and_no_task_runs_in_dir(self) -> None:
7887
)
7988

8089
create_structure(self.scratchspace_path, starting_structure)
81-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
90+
clean_workspace(self.workspace)
8291
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
8392

8493
def test_no_links_in_symlinks(self) -> None:
@@ -94,7 +103,7 @@ def test_no_links_in_symlinks(self) -> None:
94103
)
95104

96105
create_structure(self.scratchspace_path, starting_structure)
97-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
106+
clean_workspace(self.workspace)
98107
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
99108

100109
def test_link_to_file_directly_in_task_runs(self) -> None:
@@ -116,7 +125,7 @@ def test_link_to_file_directly_in_task_runs(self) -> None:
116125
)
117126

118127
create_structure(self.scratchspace_path, starting_structure)
119-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
128+
clean_workspace(self.workspace)
120129
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
121130

122131
def test_link_to_dir_directly_in_task_runs(self) -> None:
@@ -143,7 +152,7 @@ def test_link_to_dir_directly_in_task_runs(self) -> None:
143152
)
144153

145154
create_structure(self.scratchspace_path, starting_structure)
146-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
155+
clean_workspace(self.workspace)
147156
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
148157

149158
def test_link_to_file_in_dir_in_task_runs(self) -> None:
@@ -170,7 +179,7 @@ def test_link_to_file_in_dir_in_task_runs(self) -> None:
170179
)
171180

172181
create_structure(self.scratchspace_path, starting_structure)
173-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
182+
clean_workspace(self.workspace)
174183
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
175184

176185
def test_link_to_dir_in_dir_in_task_runs(self) -> None:
@@ -199,7 +208,7 @@ def test_link_to_dir_in_dir_in_task_runs(self) -> None:
199208
)
200209

201210
create_structure(self.scratchspace_path, starting_structure)
202-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
211+
clean_workspace(self.workspace)
203212
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
204213

205214
def test_link_to_link_crashes(self) -> None:
@@ -218,7 +227,7 @@ def test_link_to_link_crashes(self) -> None:
218227

219228
create_structure(self.scratchspace_path, starting_structure)
220229
with self.assertRaises(AssertionError):
221-
clean_workspace(MockDBGymWorkspace(self.workspace_path))
230+
clean_workspace(self.workspace)
222231

223232
def test_safe_mode_link_to_dir_with_link(self) -> None:
224233
starting_symlinks_structure = FilesystemStructure(
@@ -252,7 +261,7 @@ def test_safe_mode_link_to_dir_with_link(self) -> None:
252261
)
253262

254263
create_structure(self.scratchspace_path, starting_structure)
255-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
264+
clean_workspace(self.workspace, mode="safe")
256265
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
257266

258267
def test_safe_mode_link_to_file_in_dir_with_link(self) -> None:
@@ -289,7 +298,7 @@ def test_safe_mode_link_to_file_in_dir_with_link(self) -> None:
289298
)
290299

291300
create_structure(self.scratchspace_path, starting_structure)
292-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
301+
clean_workspace(self.workspace, mode="safe")
293302
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
294303

295304
def test_safe_mode_link_to_dir_with_link_to_file_in_dir_in_task_runs(self) -> None:
@@ -328,7 +337,7 @@ def test_safe_mode_link_to_dir_with_link_to_file_in_dir_in_task_runs(self) -> No
328337
)
329338

330339
create_structure(self.scratchspace_path, starting_structure)
331-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
340+
clean_workspace(self.workspace, mode="safe")
332341
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
333342

334343
def test_aggressive_mode_link_to_dir_with_link(self) -> None:
@@ -360,7 +369,7 @@ def test_aggressive_mode_link_to_dir_with_link(self) -> None:
360369
)
361370

362371
create_structure(self.scratchspace_path, starting_structure)
363-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="aggressive")
372+
clean_workspace(self.workspace, mode="aggressive")
364373
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
365374

366375
def test_link_to_link_to_file_gives_error(self) -> None:
@@ -383,7 +392,7 @@ def test_link_to_link_to_file_gives_error(self) -> None:
383392

384393
# We disallow links to links so it's an AssertionError
385394
with self.assertRaises(AssertionError):
386-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
395+
clean_workspace(self.workspace, mode="safe")
387396

388397
def test_multi_link_loop_gives_error(self) -> None:
389398
starting_symlinks_structure = FilesystemStructure(
@@ -402,7 +411,7 @@ def test_multi_link_loop_gives_error(self) -> None:
402411

403412
# pathlib disallows multi-link loops so it's a RuntimeError
404413
with self.assertRaises(RuntimeError):
405-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
414+
clean_workspace(self.workspace, mode="safe")
406415

407416
def test_link_self_loop_gives_error(self) -> None:
408417
starting_symlinks_structure = FilesystemStructure(
@@ -417,7 +426,7 @@ def test_link_self_loop_gives_error(self) -> None:
417426

418427
# pathlib disallows link self-loops so it's a RuntimeError
419428
with self.assertRaises(RuntimeError):
420-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
429+
clean_workspace(self.workspace, mode="safe")
421430

422431
def test_dont_loop_infinitely_if_there_are_cycles_between_different_dirs_in_runs(
423432
self,
@@ -460,7 +469,7 @@ def test_dont_loop_infinitely_if_there_are_cycles_between_different_dirs_in_runs
460469
)
461470

462471
create_structure(self.scratchspace_path, starting_structure)
463-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
472+
clean_workspace(self.workspace, mode="safe")
464473
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
465474

466475
def test_dont_loop_infinitely_if_there_is_a_dir_in_runs_that_links_to_a_file_in_itself(
@@ -496,7 +505,7 @@ def test_dont_loop_infinitely_if_there_is_a_dir_in_runs_that_links_to_a_file_in_
496505
)
497506

498507
create_structure(self.scratchspace_path, starting_structure)
499-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
508+
clean_workspace(self.workspace, mode="safe")
500509
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
501510

502511
def test_dont_loop_infinitely_if_there_is_loop_amongst_symlinks(self) -> None:
@@ -530,7 +539,7 @@ def test_dont_loop_infinitely_if_there_is_loop_amongst_symlinks(self) -> None:
530539
)
531540

532541
create_structure(self.scratchspace_path, starting_structure)
533-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
542+
clean_workspace(self.workspace, mode="safe")
534543
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
535544

536545
def test_broken_symlink_has_no_effect(self) -> None:
@@ -563,7 +572,7 @@ def test_broken_symlink_has_no_effect(self) -> None:
563572
)
564573

565574
create_structure(self.scratchspace_path, starting_structure)
566-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
575+
clean_workspace(self.workspace, mode="safe")
567576
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
568577

569578
# The idea behind this test is that we shouldn't be following links outside of task_runs, even on safe mode
@@ -612,7 +621,7 @@ def test_link_to_folder_outside_runs_that_contains_link_to_other_run_doesnt_save
612621
}
613622

614623
create_structure(self.scratchspace_path, starting_structure)
615-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
624+
clean_workspace(self.workspace, mode="safe")
616625
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
617626

618627
def test_outside_task_runs_doesnt_get_deleted(self) -> None:
@@ -630,7 +639,7 @@ def test_outside_task_runs_doesnt_get_deleted(self) -> None:
630639
ending_structure["external"] = FilesystemStructure({"file1.txt": ("file",)})
631640

632641
create_structure(self.scratchspace_path, starting_structure)
633-
clean_workspace(MockDBGymWorkspace(self.workspace_path), mode="safe")
642+
clean_workspace(self.workspace, mode="safe")
634643
self.assertTrue(verify_structure(self.scratchspace_path, ending_structure))
635644

636645

0 commit comments

Comments
 (0)