77
88import os
99import shutil
10+ import tempfile
1011import unittest
1112import warnings
1213
1617from arc .main import ARC
1718
1819
19- def _project_name (base : str ) -> str :
20- """Return a per-xdist-worker project name to avoid parallel cleanup collisions."""
21- worker_id = os .environ .get ('PYTEST_XDIST_WORKER' )
22- if worker_id :
23- return f'{ base } _{ worker_id } '
24- return base
25-
26-
2720class TestRestart (unittest .TestCase ):
2821 """
2922 Contains unit tests for restarting ARC.
@@ -37,15 +30,21 @@ def setUpClass(cls):
3730 cls .maxDiff = None
3831 warnings .filterwarnings (action = 'ignore' , module = '.*matplotlib.*' )
3932
33+ def make_project_directory (self , project : str ) -> str :
34+ """Get a path to a temporary project directory that is deleted when the test ends."""
35+ project_directory = os .path .join (tempfile .mkdtemp (prefix = 'arc_test_restart_' ), project )
36+ self .addCleanup (shutil .rmtree , os .path .dirname (project_directory ), ignore_errors = True )
37+ return project_directory
38+
4039 def test_restart_thermo (self ):
4140 """
4241 Test restarting ARC through the ARC class in main.py via the input_dict argument of the API
4342 Rather than through ARC.py. Check that all files are in place and the log file content.
4443 """
4544 restart_dir = os .path .join (ARC_PATH , 'arc' , 'testing' , 'restart' , '1_restart_thermo' )
4645 restart_path = os .path .join (restart_dir , 'restart.yml' )
47- project = _project_name ( 'arc_project_for_testing_delete_after_usage_restart_thermo' )
48- project_directory = os . path . join ( ARC_PATH , 'Projects' , project )
46+ project = 'arc_project_for_testing_delete_after_usage_restart_thermo'
47+ project_directory = self . make_project_directory ( project )
4948 os .makedirs (os .path .dirname (project_directory ), exist_ok = True )
5049 shutil .copytree (os .path .join (restart_dir , 'calcs' ), os .path .join (project_directory , 'calcs' , 'Species' ), dirs_exist_ok = True )
5150 input_dict = read_yaml_file (path = restart_path , project_directory = project_directory )
@@ -141,8 +140,8 @@ def test_restart_rate_1(self):
141140 """Test restarting ARC and attaining a reaction rate coefficient"""
142141 restart_dir = os .path .join (ARC_PATH , 'arc' , 'testing' , 'restart' , '2_restart_rate' )
143142 restart_path = os .path .join (restart_dir , 'restart.yml' )
144- project = _project_name ( 'arc_project_for_testing_delete_after_usage_restart_rate_1' )
145- project_directory = os . path . join ( ARC_PATH , 'Projects' , project )
143+ project = 'arc_project_for_testing_delete_after_usage_restart_rate_1'
144+ project_directory = self . make_project_directory ( project )
146145 os .makedirs (os .path .dirname (project_directory ), exist_ok = True )
147146 shutil .copytree (os .path .join (restart_dir , 'calcs' ), os .path .join (project_directory , 'calcs' ), dirs_exist_ok = True )
148147 input_dict = read_yaml_file (path = restart_path , project_directory = project_directory )
@@ -162,8 +161,8 @@ def test_restart_rate_1(self):
162161
163162 def test_restart_rate_2 (self ):
164163 """Test restarting ARC and attaining a reaction rate coefficient"""
165- project = _project_name ( 'arc_project_for_testing_delete_after_usage_restart_rate_2' )
166- project_directory = os . path . join ( ARC_PATH , 'Projects' , project )
164+ project = 'arc_project_for_testing_delete_after_usage_restart_rate_2'
165+ project_directory = self . make_project_directory ( project )
167166 base_path = os .path .join (ARC_PATH , 'arc' , 'testing' , 'restart' , '5_TS1' )
168167 restart_path = os .path .join (base_path , 'restart.yml' )
169168 input_dict = read_yaml_file (path = restart_path , project_directory = project_directory )
@@ -191,16 +190,16 @@ def test_restart_bde (self):
191190 """Test restarting ARC and attaining a BDE for anilino_radical."""
192191 restart_dir = os .path .join (ARC_PATH , 'arc' , 'testing' , 'restart' , '3_restart_bde' )
193192 restart_path = os .path .join (restart_dir , 'restart.yml' )
194- project = _project_name ( 'test_restart_bde' )
195- project_directory = os . path . join ( ARC_PATH , 'Projects' , project )
193+ project = 'test_restart_bde'
194+ project_directory = self . make_project_directory ( project )
196195 os .makedirs (os .path .dirname (project_directory ), exist_ok = True )
197196 shutil .copytree (os .path .join (restart_dir , 'calcs' ), os .path .join (project_directory , 'calcs' ), dirs_exist_ok = True )
198197 input_dict = read_yaml_file (path = restart_path , project_directory = project_directory )
199198 input_dict ['project' ], input_dict ['project_directory' ] = project , project_directory
200199 arc1 = ARC (** input_dict )
201200 arc1 .execute ()
202201
203- report_path = os .path .join (ARC_PATH , 'Projects' , project , 'output' , 'BDE_report.txt' )
202+ report_path = os .path .join (project_directory , 'output' , 'BDE_report.txt' )
204203 with open (report_path , 'r' ) as f :
205204 lines = f .readlines ()
206205 self .assertIn (' BDE report for anilino_radical:\n ' , lines )
@@ -224,17 +223,8 @@ def test_globalize_paths(self):
224223 def tearDownClass (cls ):
225224 """
226225 A function that is run ONCE after all unit tests in this class.
227- Delete all project directories created during these unit tests
226+ Delete all files and directories created during these unit tests
228227 """
229- projects = [_project_name ('arc_project_for_testing_delete_after_usage_restart_thermo' ),
230- _project_name ('arc_project_for_testing_delete_after_usage_restart_rate_1' ),
231- _project_name ('arc_project_for_testing_delete_after_usage_restart_rate_2' ),
232- _project_name ('test_restart_bde' ),
233- ]
234- for project in projects :
235- project_directory = os .path .join (ARC_PATH , 'Projects' , project )
236- shutil .rmtree (project_directory , ignore_errors = True )
237-
238228 shutil .rmtree (os .path .join (ARC_PATH , 'arc' , 'testing' , 'restart' , '4_globalized_paths' ,
239229 'log_and_restart_archive' ), ignore_errors = True )
240230 for file_name in ['arc.log' , 'restart_paths_globalized.yml' ]:
0 commit comments