Skip to content

Commit eeb0758

Browse files
authored
Merge pull request #111 from CCPBioSim/110-improve-test-coverage-for-main
Improve Test Coverage for `main.py`
2 parents 572e5ed + d922e04 commit eeb0758

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/test_CodeEntropy/test_main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import sys
5+
import tempfile
16
import unittest
27
from unittest.mock import MagicMock, patch
38

@@ -9,6 +14,22 @@ class TestMain(unittest.TestCase):
914
Unit tests for the main functionality of CodeEntropy.
1015
"""
1116

17+
def setUp(self):
18+
"""
19+
Set up a temporary directory as the working directory before each test.
20+
"""
21+
self.test_dir = tempfile.mkdtemp(prefix="CodeEntropy_")
22+
self._orig_dir = os.getcwd()
23+
os.chdir(self.test_dir)
24+
25+
def tearDown(self):
26+
"""
27+
Clean up by removing the temporary directory and restoring the original working
28+
directory.
29+
"""
30+
os.chdir(self._orig_dir)
31+
shutil.rmtree(self.test_dir)
32+
1233
@patch("CodeEntropy.main.sys.exit")
1334
@patch("CodeEntropy.main.RunManager")
1435
def test_main_successful_run(self, mock_RunManager, mock_exit):
@@ -68,6 +89,51 @@ def test_main_exception_triggers_exit(
6889
"Fatal error during entropy calculation: Test exception", exc_info=True
6990
)
7091

92+
def test_main_entry_point_runs(self):
93+
"""
94+
Test that the CLI entry point (main.py) runs successfully with minimal required
95+
arguments.
96+
"""
97+
# Prepare input files
98+
data_dir = os.path.abspath(
99+
os.path.join(os.path.dirname(__file__), "..", "data")
100+
)
101+
tpr_path = shutil.copy(os.path.join(data_dir, "md_A4_dna.tpr"), self.test_dir)
102+
trr_path = shutil.copy(
103+
os.path.join(data_dir, "md_A4_dna_xf.trr"), self.test_dir
104+
)
105+
106+
config_path = os.path.join(self.test_dir, "config.yaml")
107+
with open(config_path, "w") as f:
108+
f.write("run1:\n" " selection_string: resid 1\n")
109+
110+
result = subprocess.run(
111+
[
112+
sys.executable,
113+
"-m",
114+
"CodeEntropy.main",
115+
"--top_traj_file",
116+
tpr_path,
117+
trr_path,
118+
],
119+
cwd=self.test_dir,
120+
capture_output=True,
121+
text=True,
122+
)
123+
124+
self.assertEqual(result.returncode, 0)
125+
126+
# Check for job folder and output file
127+
job_dir = os.path.join(self.test_dir, "job001")
128+
output_file = os.path.join(job_dir, "output_file.json")
129+
130+
self.assertTrue(os.path.exists(job_dir))
131+
self.assertTrue(os.path.exists(output_file))
132+
133+
with open(output_file) as f:
134+
content = f.read()
135+
self.assertIn("DA", content)
136+
71137

72138
if __name__ == "__main__":
73139
unittest.main()

0 commit comments

Comments
 (0)