Skip to content

Commit 87f3c51

Browse files
committed
Fixes restart tests for parallel execution
Modifies restart tests to generate unique project names when running in parallel using pytest-xdist. This avoids collisions during cleanup of project directories. Updates restart tests to use ARC_TESTING_PATH Modifies restart tests to utilize the ARC_TESTING_PATH constant for specifying test directories. This change ensures consistency and simplifies path management within the testing framework.
1 parent abd9201 commit 87f3c51

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

functional/restart_test.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212

1313
from arc.molecule.molecule import Molecule
1414

15-
from arc.common import ARC_PATH, read_yaml_file
15+
from arc.common import ARC_PATH, ARC_TESTING_PATH, read_yaml_file
1616
from arc.main import ARC
1717

1818

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+
1927
class TestRestart(unittest.TestCase):
2028
"""
2129
Contains unit tests for restarting ARC.
@@ -34,9 +42,9 @@ def test_restart_thermo(self):
3442
Test restarting ARC through the ARC class in main.py via the input_dict argument of the API
3543
Rather than through ARC.py. Check that all files are in place and the log file content.
3644
"""
37-
restart_dir = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '1_restart_thermo')
45+
restart_dir = os.path.join(ARC_TESTING_PATH, 'restart', '1_restart_thermo')
3846
restart_path = os.path.join(restart_dir, 'restart.yml')
39-
project = 'arc_project_for_testing_delete_after_usage_restart_thermo'
47+
project = _project_name('arc_project_for_testing_delete_after_usage_restart_thermo')
4048
project_directory = os.path.join(ARC_PATH, 'Projects', project)
4149
os.makedirs(os.path.dirname(project_directory), exist_ok=True)
4250
shutil.copytree(os.path.join(restart_dir, 'calcs'), os.path.join(project_directory, 'calcs', 'Species'), dirs_exist_ok=True)
@@ -55,7 +63,7 @@ def test_restart_thermo(self):
5563
break
5664
self.assertTrue(thermo_dft_ccsdtf12_bac)
5765

58-
with open(os.path.join(project_directory, 'arc_project_for_testing_delete_after_usage_restart_thermo.info'), 'r') as f:
66+
with open(os.path.join(project_directory, f'{project}.info'), 'r') as f:
5967
sts, n2h3, oet, lot, ap = False, False, False, False, False
6068
for line in f.readlines():
6169
if 'Considered the following species and TSs:' in line:
@@ -66,7 +74,7 @@ def test_restart_thermo(self):
6674
oet = True
6775
elif 'Levels of theory used:' in line:
6876
lot = True
69-
elif 'ARC project arc_project_for_testing_delete_after_usage_restart_thermo' in line:
77+
elif f'ARC project {project}' in line:
7078
ap = True
7179
self.assertTrue(sts)
7280
self.assertTrue(n2h3)
@@ -131,9 +139,9 @@ def test_restart_thermo(self):
131139

132140
def test_restart_rate_1(self):
133141
"""Test restarting ARC and attaining a reaction rate coefficient"""
134-
restart_dir = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '2_restart_rate')
142+
restart_dir = os.path.join(ARC_TESTING_PATH, 'restart', '2_restart_rate')
135143
restart_path = os.path.join(restart_dir, 'restart.yml')
136-
project = 'arc_project_for_testing_delete_after_usage_restart_rate_1'
144+
project = _project_name('arc_project_for_testing_delete_after_usage_restart_rate_1')
137145
project_directory = os.path.join(ARC_PATH, 'Projects', project)
138146
os.makedirs(os.path.dirname(project_directory), exist_ok=True)
139147
shutil.copytree(os.path.join(restart_dir, 'calcs'), os.path.join(project_directory, 'calcs'), dirs_exist_ok=True)
@@ -154,9 +162,9 @@ def test_restart_rate_1(self):
154162

155163
def test_restart_rate_2(self):
156164
"""Test restarting ARC and attaining a reaction rate coefficient"""
157-
project = 'arc_project_for_testing_delete_after_usage_restart_rate_2'
165+
project = _project_name('arc_project_for_testing_delete_after_usage_restart_rate_2')
158166
project_directory = os.path.join(ARC_PATH, 'Projects', project)
159-
base_path = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '5_TS1')
167+
base_path = os.path.join(ARC_TESTING_PATH, 'restart', '5_TS1')
160168
restart_path = os.path.join(base_path, 'restart.yml')
161169
input_dict = read_yaml_file(path=restart_path, project_directory=project_directory)
162170
input_dict['output']['TS0']['paths']['freq'] = os.path.join(ARC_PATH, input_dict['output']['TS0']['paths']['freq'])
@@ -181,9 +189,9 @@ def test_restart_rate_2(self):
181189

182190
def test_restart_bde (self):
183191
"""Test restarting ARC and attaining a BDE for anilino_radical."""
184-
restart_dir = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '3_restart_bde')
192+
restart_dir = os.path.join(ARC_TESTING_PATH, 'restart', '3_restart_bde')
185193
restart_path = os.path.join(restart_dir, 'restart.yml')
186-
project = 'test_restart_bde'
194+
project = _project_name('test_restart_bde')
187195
project_directory = os.path.join(ARC_PATH, 'Projects', project)
188196
os.makedirs(os.path.dirname(project_directory), exist_ok=True)
189197
shutil.copytree(os.path.join(restart_dir, 'calcs'), os.path.join(project_directory, 'calcs'), dirs_exist_ok=True)
@@ -192,15 +200,15 @@ def test_restart_bde (self):
192200
arc1 = ARC(**input_dict)
193201
arc1.execute()
194202

195-
report_path = os.path.join(ARC_PATH, 'Projects', 'test_restart_bde', 'output', 'BDE_report.txt')
203+
report_path = os.path.join(ARC_PATH, 'Projects', project, 'output', 'BDE_report.txt')
196204
with open(report_path, 'r') as f:
197205
lines = f.readlines()
198206
self.assertIn(' BDE report for anilino_radical:\n', lines)
199207
self.assertIn(' (1, 9) N - H 353.92\n', lines)
200208

201209
def test_globalize_paths(self):
202210
"""Test modifying a YAML file's contents to correct absolute file paths"""
203-
project_directory = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '4_globalized_paths')
211+
project_directory = os.path.join(ARC_TESTING_PATH, 'restart', '4_globalized_paths')
204212
restart_path = os.path.join(project_directory, 'restart_paths.yml')
205213
input_dict = read_yaml_file(path=restart_path, project_directory=project_directory)
206214
input_dict['project_directory'] = project_directory
@@ -218,25 +226,25 @@ def tearDownClass(cls):
218226
A function that is run ONCE after all unit tests in this class.
219227
Delete all project directories created during these unit tests
220228
"""
221-
projects = ['arc_project_for_testing_delete_after_usage_restart_thermo',
222-
'arc_project_for_testing_delete_after_usage_restart_rate_1',
223-
'arc_project_for_testing_delete_after_usage_restart_rate_2',
224-
'test_restart_bde',
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'),
225233
]
226234
for project in projects:
227235
project_directory = os.path.join(ARC_PATH, 'Projects', project)
228236
shutil.rmtree(project_directory, ignore_errors=True)
229237

230-
shutil.rmtree(os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '4_globalized_paths',
238+
shutil.rmtree(os.path.join(ARC_TESTING_PATH, 'restart', '4_globalized_paths',
231239
'log_and_restart_archive'), ignore_errors=True)
232240
for file_name in ['arc.log', 'restart_paths_globalized.yml']:
233-
file_path = os.path.join(ARC_PATH, 'arc', 'testing', 'restart', '4_globalized_paths', file_name)
241+
file_path = os.path.join(ARC_TESTING_PATH, 'restart', '4_globalized_paths', file_name)
234242
if os.path.isfile(file_path):
235243
os.remove(file_path)
236244
file_paths = [os.path.join(ARC_PATH, 'functional', 'nul'), os.path.join(ARC_PATH, 'functional', 'run.out')]
237245
project_names = ['1_restart_thermo', '2_restart_rate', '3_restart_bde', '5_TS1']
238246
for project_name in project_names:
239-
file_paths.append(os.path.join(ARC_PATH, 'arc', 'testing', 'restart', project_name, 'restart_globalized.yml'))
247+
file_paths.append(os.path.join(ARC_TESTING_PATH, 'restart', project_name, 'restart_globalized.yml'))
240248
for file_path in file_paths:
241249
if os.path.isfile(file_path):
242250
os.remove(file_path)

0 commit comments

Comments
 (0)