Skip to content

Commit 36144db

Browse files
committed
Refactor test files to use utility functions for directory paths and improve path handling
minor fix minor fix
1 parent e652603 commit 36144db

4 files changed

Lines changed: 38 additions & 58 deletions

File tree

autotst/calculator/orca_test.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@
3333

3434
from .orca import Orca
3535
from ..species import Conformer
36+
from ..utils.paths import project_root, test_data_dir
3637

3738
class TestOrca(unittest.TestCase):
3839

3940
def setUp(self):
4041
conf = Conformer(smiles='C')
41-
self.orca = Orca(conformer=conf, directory=os.path.expandvars(
42-
"$AUTOTST/autotst/calculator/fod"))
42+
self.orca = Orca(
43+
conformer=conf,
44+
directory=str(project_root() / "autotst" / "calculator" / "fod")
45+
)
4346

4447
def test_load_conformer_attributes(self):
4548
charge = 0
@@ -59,21 +62,19 @@ def test_write_fod_input(self):
5962
self.assertTrue(os.path.exists(os.path.join(self.orca.directory,'C_fod.inp')))
6063

6164
def test_check_normal_termination(self):
62-
path = os.path.expandvars(
63-
"$AUTOTST/test/bin/log-files/C_fod.log")
64-
self.assertTrue(self.orca.check_normal_termination(path))
65+
path = test_data_dir() / "C_fod.log"
66+
self.assertTrue(self.orca.check_normal_termination(str(path)))
6567

6668
def test_read_fod_log(self):
67-
path = os.path.expandvars(
68-
"$AUTOTST/test/bin/log-files/C_fod.log")
69-
fod = self.orca.read_fod_log(path)
69+
path = test_data_dir() / "C_fod.log"
70+
fod = self.orca.read_fod_log(str(path))
7071
self.assertEquals(float(0.000025),fod)
7172

7273
def tearDown(self):
7374

74-
if os.path.exists(os.path.expandvars("$AUTOTST/autotst/calculator/fod")):
75-
shutil.rmtree(os.path.expandvars(
76-
"$AUTOTST/autotst/calculator/fod"))
75+
fod_dir = project_root() / "autotst" / "calculator" / "fod"
76+
if os.path.exists(fod_dir):
77+
shutil.rmtree(fod_dir)
7778

7879
if __name__ == "__main__":
7980
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))

autotst/calculator/statmech_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import unittest, os, sys, shutil
3232
from ..reaction import Reaction
3333
from .statmech import StatMech
34+
from ..utils.paths import project_root, test_data_dir, test_dir
3435
import rmgpy.reaction
3536
import rmgpy.kinetics
3637

@@ -40,12 +41,12 @@ def setUp(self):
4041
self.reaction.get_labeled_reaction()
4142
self.reaction.generate_reactants_and_products()
4243

43-
directory = os.path.expandvars("$AUTOTST/test")
44+
directory = test_dir()
4445
if not os.path.exists(os.path.join(directory, "ts", self.reaction.label)):
4546
os.makedirs(os.path.join(directory, "ts", self.reaction.label))
4647
if not os.path.exists(os.path.join(directory, "ts", self.reaction.label, self.reaction.label + ".log")):
4748
shutil.copy(
48-
os.path.join(directory, "bin", "log-files", self.reaction.label + "_forward_0.log"),
49+
test_data_dir() / f"{self.reaction.label}_forward_0.log",
4950
os.path.join(directory, "ts", self.reaction.label, self.reaction.label + ".log")
5051
)
5152

@@ -55,24 +56,24 @@ def setUp(self):
5556
os.makedirs(os.path.join(directory, "species", smiles))
5657
if not os.path.exists(os.path.join(directory, "species", smiles, smiles+".log")):
5758
shutil.copy(
58-
os.path.join(directory, "bin", "log-files", smiles + "_0.log"),
59+
test_data_dir() / f"{smiles}_0.log",
5960
os.path.join(directory, "species", smiles, smiles+".log")
6061
)
6162

6263
self.statmech = StatMech(
6364
reaction = self.reaction,
64-
directory = directory
65+
directory = str(directory)
6566
)
6667

6768
def tearDown(self):
6869
try:
69-
directory = os.path.expandvars("$AUTOTST/test")
70+
directory = test_dir()
7071
if os.path.exists(os.path.join(directory, "ts")):
7172
shutil.rmtree(os.path.join(directory, "ts"))
7273
if os.path.exists(os.path.join(directory, "species")):
7374
shutil.rmtree(os.path.join(directory, "species"))
7475

75-
for head, _, files in os.walk(os.path.expandvars("$AUTOTST")):
76+
for head, _, files in os.walk(project_root()):
7677
for fi in files:
7778
if fi.endswith(".symm"):
7879
os.remove(os.path.join(head, fi))
@@ -176,4 +177,4 @@ def test_set_results(self):
176177
if __name__ == "__main__":
177178
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
178179

179-
180+

autotst/calculator/vibrational_analysis_test.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from ..reaction import Reaction, TS
4040
from ..species import Species, Conformer
4141
from .vibrational_analysis import percent_change, VibrationalAnalysis
42+
from ..utils.paths import project_root, test_data_dir, test_dir
4243

4344
class VibrationalAnalysisTest(unittest.TestCase):
4445

@@ -48,29 +49,28 @@ def setUp(self):
4849
self.ts = self.reaction.ts["forward"][0]
4950
self.ts.get_molecules()
5051

51-
directory = os.path.expandvars("$AUTOTST/test")
52+
directory = test_dir()
5253
if not os.path.exists(os.path.join(directory, "ts", self.reaction.label, "conformers")):
5354
os.makedirs(os.path.join(directory, "ts", self.reaction.label, "conformers"))
5455
if not os.path.exists(os.path.join(directory, "ts", self.reaction.label, self.reaction.label + ".log")):
5556
shutil.copy(
56-
os.path.join(directory, "bin", "log-files", self.reaction.label + "_forward_0.log"),
57+
test_data_dir() / f"{self.reaction.label}_forward_0.log",
5758
os.path.join(directory, "ts", self.reaction.label, "conformers", self.reaction.label + "_forward_0.log")
5859
)
5960

6061
self.directory = directory
6162
self.vibrational_analysis = VibrationalAnalysis(
6263
transitionstate = self.ts,
63-
directory = self.directory
64+
directory = str(self.directory)
6465
)
6566

6667
self.reaction2 = Reaction("[CH3]+CC(F)(F)F_C+[CH2]C(F)(F)F")
6768
self.reaction2.get_labeled_reaction()
6869
self.ts2 = self.reaction2.ts["forward"][0]
69-
log_file = os.path.join(
70-
directory, "bin", "log-files", "[CH3]+CC(F)(F)F_C+[CH2]C(F)(F)F.log")
70+
log_file = test_data_dir() / "[CH3]+CC(F)(F)F_C+[CH2]C(F)(F)F.log"
7171
self.vibrational_analysis2 = VibrationalAnalysis(
7272
transitionstate = self.ts2,
73-
log_file = log_file
73+
log_file = str(log_file)
7474
)
7575

7676
def test_get_log_file(self):
@@ -140,14 +140,13 @@ def test_validate(self):
140140
if __name__ == "__main__":
141141
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
142142
try:
143-
directory = os.path.expandvars("$AUTOTST/test")
143+
directory = test_dir()
144144
if os.path.exists(os.path.join(directory, "ts")):
145145
shutil.rmtree(os.path.join(directory, "ts"))
146146

147-
for head, _, files in os.walk(os.path.expandvars("$AUTOTST")):
147+
for head, _, files in os.walk(project_root()):
148148
for fi in files:
149149
if fi.endswith(".symm"):
150150
os.remove(os.path.join(head, fi))
151-
except:
151+
except Exception:
152152
None
153-

autotst/data/inputoutput_test.py

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from ..reaction import Reaction
3939
from .base import QMData, DistanceData
4040
from .inputoutput import InputOutput
41+
from ..utils.paths import test_data_dir, test_dir
4142
import rmgpy.kinetics
4243

4344
class TestInputOutput(unittest.TestCase):
@@ -46,54 +47,32 @@ def setUp(self):
4647
self.reaction = Reaction("CC+[O]O_[CH2]C+OO")
4748
self.io = InputOutput(
4849
reaction=self.reaction,
49-
directory=os.path.expandvars("$AUTOTST/test/")
50+
directory=str(test_dir())
5051
)
5152
try:
52-
os.makedirs(os.path.join(
53-
os.path.expandvars("$AUTOTST/test/"),
54-
"ts",
55-
self.reaction.label
56-
))
53+
os.makedirs(test_dir() / "ts" / self.reaction.label)
5754
except OSError:
5855
try:
5956
shutil.copy(
60-
os.path.join(
61-
os.path.expandvars("$AUTOTST/test/bin/log-files"),
62-
self.reaction.label + "_forward_0.log"
63-
),
64-
os.path.join(
65-
os.path.expandvars("$AUTOTST/test/"),
66-
"ts",
67-
self.reaction.label,
68-
self.reaction.label + ".log"
69-
)
57+
test_data_dir() / f"{self.reaction.label}_forward_0.log",
58+
test_dir() / "ts" / self.reaction.label / f"{self.reaction.label}.log"
7059
)
71-
except:
60+
except Exception:
7261
pass
7362

7463

7564
def test_ts_file_path(self):
7665
path = self.io.get_ts_file_path()
7766
self.assertEqual(
7867
path,
79-
os.path.join(
80-
os.path.expandvars("$AUTOTST/test/"),
81-
"ts",
82-
self.reaction.label,
83-
self.reaction.label + ".ts"
84-
)
68+
str(test_dir() / "ts" / self.reaction.label / f"{self.reaction.label}.ts")
8569
)
8670

8771
def test_kinetics_file_path(self):
8872
path = self.io.get_kinetics_file_path()
8973
self.assertEqual(
9074
path,
91-
os.path.join(
92-
os.path.expandvars("$AUTOTST/test/"),
93-
"ts",
94-
self.reaction.label,
95-
self.reaction.label + ".kinetics"
96-
)
75+
str(test_dir() / "ts" / self.reaction.label / f"{self.reaction.label}.kinetics")
9776
)
9877

9978
def test_get_qmdata(self):
@@ -123,4 +102,4 @@ def test_kinetics_io(self):
123102

124103

125104
if __name__ == "__main__":
126-
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
105+
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))

0 commit comments

Comments
 (0)