9
9
make_workspace_structure ,
10
10
verify_structure ,
11
11
)
12
+ from gymlib .workspace import DBGymWorkspace
12
13
13
- from orchestrate .clean import MockDBGymWorkspace , clean_workspace
14
+ from orchestrate .clean import clean_workspace
14
15
15
16
# This is here instead of on `if __name__ == "__main__"` because we often run individual tests, which
16
17
# does not go through the `if __name__ == "__main__"` codepath.
@@ -26,26 +27,34 @@ class CleanTests(unittest.TestCase):
26
27
27
28
@classmethod
28
29
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
+ )
30
33
cls .workspace_path = cls .scratchspace_path / "dbgym_workspace"
31
34
32
35
def setUp (self ) -> None :
33
36
if self .scratchspace_path .exists ():
34
37
shutil .rmtree (self .scratchspace_path )
35
38
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
+
36
45
def tearDown (self ) -> None :
37
46
if self .scratchspace_path .exists ():
38
47
shutil .rmtree (self .scratchspace_path )
39
48
40
49
def test_nonexistent_workspace (self ) -> None :
41
50
# This just ensures that it doesn't raise an exception.
42
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
51
+ clean_workspace (self .workspace )
43
52
44
53
def test_empty_workspace (self ) -> None :
45
54
starting_structure = FilesystemStructure ({"dbgym_workspace" : {}})
46
55
ending_structure = FilesystemStructure ({"dbgym_workspace" : {}})
47
56
create_structure (self .scratchspace_path , starting_structure )
48
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
57
+ clean_workspace (self .workspace )
49
58
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
50
59
51
60
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:
54
63
)
55
64
ending_structure = FilesystemStructure ({"dbgym_workspace" : {"task_runs" : {}}})
56
65
create_structure (self .scratchspace_path , starting_structure )
57
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
66
+ clean_workspace (self .workspace )
58
67
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
59
68
60
69
def test_yes_symlinks_dir_and_no_task_runs_dir (self ) -> None :
61
70
# If there are no task runs there can't be any symlinks.
62
71
starting_structure = FilesystemStructure ({"dbgym_workspace" : {"symlinks" : {}}})
63
72
ending_structure = FilesystemStructure ({"dbgym_workspace" : {"symlinks" : {}}})
64
73
create_structure (self .scratchspace_path , starting_structure )
65
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
74
+ clean_workspace (self .workspace )
66
75
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
67
76
68
77
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:
78
87
)
79
88
80
89
create_structure (self .scratchspace_path , starting_structure )
81
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
90
+ clean_workspace (self .workspace )
82
91
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
83
92
84
93
def test_no_links_in_symlinks (self ) -> None :
@@ -94,7 +103,7 @@ def test_no_links_in_symlinks(self) -> None:
94
103
)
95
104
96
105
create_structure (self .scratchspace_path , starting_structure )
97
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
106
+ clean_workspace (self .workspace )
98
107
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
99
108
100
109
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:
116
125
)
117
126
118
127
create_structure (self .scratchspace_path , starting_structure )
119
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
128
+ clean_workspace (self .workspace )
120
129
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
121
130
122
131
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:
143
152
)
144
153
145
154
create_structure (self .scratchspace_path , starting_structure )
146
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
155
+ clean_workspace (self .workspace )
147
156
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
148
157
149
158
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:
170
179
)
171
180
172
181
create_structure (self .scratchspace_path , starting_structure )
173
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
182
+ clean_workspace (self .workspace )
174
183
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
175
184
176
185
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:
199
208
)
200
209
201
210
create_structure (self .scratchspace_path , starting_structure )
202
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
211
+ clean_workspace (self .workspace )
203
212
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
204
213
205
214
def test_link_to_link_crashes (self ) -> None :
@@ -218,7 +227,7 @@ def test_link_to_link_crashes(self) -> None:
218
227
219
228
create_structure (self .scratchspace_path , starting_structure )
220
229
with self .assertRaises (AssertionError ):
221
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) )
230
+ clean_workspace (self .workspace )
222
231
223
232
def test_safe_mode_link_to_dir_with_link (self ) -> None :
224
233
starting_symlinks_structure = FilesystemStructure (
@@ -252,7 +261,7 @@ def test_safe_mode_link_to_dir_with_link(self) -> None:
252
261
)
253
262
254
263
create_structure (self .scratchspace_path , starting_structure )
255
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
264
+ clean_workspace (self .workspace , mode = "safe" )
256
265
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
257
266
258
267
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:
289
298
)
290
299
291
300
create_structure (self .scratchspace_path , starting_structure )
292
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
301
+ clean_workspace (self .workspace , mode = "safe" )
293
302
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
294
303
295
304
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
328
337
)
329
338
330
339
create_structure (self .scratchspace_path , starting_structure )
331
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
340
+ clean_workspace (self .workspace , mode = "safe" )
332
341
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
333
342
334
343
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:
360
369
)
361
370
362
371
create_structure (self .scratchspace_path , starting_structure )
363
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "aggressive" )
372
+ clean_workspace (self .workspace , mode = "aggressive" )
364
373
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
365
374
366
375
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:
383
392
384
393
# We disallow links to links so it's an AssertionError
385
394
with self .assertRaises (AssertionError ):
386
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
395
+ clean_workspace (self .workspace , mode = "safe" )
387
396
388
397
def test_multi_link_loop_gives_error (self ) -> None :
389
398
starting_symlinks_structure = FilesystemStructure (
@@ -402,7 +411,7 @@ def test_multi_link_loop_gives_error(self) -> None:
402
411
403
412
# pathlib disallows multi-link loops so it's a RuntimeError
404
413
with self .assertRaises (RuntimeError ):
405
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
414
+ clean_workspace (self .workspace , mode = "safe" )
406
415
407
416
def test_link_self_loop_gives_error (self ) -> None :
408
417
starting_symlinks_structure = FilesystemStructure (
@@ -417,7 +426,7 @@ def test_link_self_loop_gives_error(self) -> None:
417
426
418
427
# pathlib disallows link self-loops so it's a RuntimeError
419
428
with self .assertRaises (RuntimeError ):
420
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
429
+ clean_workspace (self .workspace , mode = "safe" )
421
430
422
431
def test_dont_loop_infinitely_if_there_are_cycles_between_different_dirs_in_runs (
423
432
self ,
@@ -460,7 +469,7 @@ def test_dont_loop_infinitely_if_there_are_cycles_between_different_dirs_in_runs
460
469
)
461
470
462
471
create_structure (self .scratchspace_path , starting_structure )
463
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
472
+ clean_workspace (self .workspace , mode = "safe" )
464
473
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
465
474
466
475
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_
496
505
)
497
506
498
507
create_structure (self .scratchspace_path , starting_structure )
499
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
508
+ clean_workspace (self .workspace , mode = "safe" )
500
509
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
501
510
502
511
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:
530
539
)
531
540
532
541
create_structure (self .scratchspace_path , starting_structure )
533
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
542
+ clean_workspace (self .workspace , mode = "safe" )
534
543
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
535
544
536
545
def test_broken_symlink_has_no_effect (self ) -> None :
@@ -563,7 +572,7 @@ def test_broken_symlink_has_no_effect(self) -> None:
563
572
)
564
573
565
574
create_structure (self .scratchspace_path , starting_structure )
566
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
575
+ clean_workspace (self .workspace , mode = "safe" )
567
576
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
568
577
569
578
# 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
612
621
}
613
622
614
623
create_structure (self .scratchspace_path , starting_structure )
615
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
624
+ clean_workspace (self .workspace , mode = "safe" )
616
625
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
617
626
618
627
def test_outside_task_runs_doesnt_get_deleted (self ) -> None :
@@ -630,7 +639,7 @@ def test_outside_task_runs_doesnt_get_deleted(self) -> None:
630
639
ending_structure ["external" ] = FilesystemStructure ({"file1.txt" : ("file" ,)})
631
640
632
641
create_structure (self .scratchspace_path , starting_structure )
633
- clean_workspace (MockDBGymWorkspace ( self .workspace_path ) , mode = "safe" )
642
+ clean_workspace (self .workspace , mode = "safe" )
634
643
self .assertTrue (verify_structure (self .scratchspace_path , ending_structure ))
635
644
636
645
0 commit comments