|
1 | | -import logging |
2 | 1 | import os |
3 | 2 | import shutil |
4 | 3 | import tempfile |
5 | 4 | import unittest |
6 | | -import uuid |
7 | 5 |
|
8 | 6 |
|
9 | 7 | class BaseTestCase(unittest.TestCase): |
10 | 8 | """ |
11 | | - Base class for tests with cross-platform setup and teardown. |
12 | | - Creates unique temporary directories and pre-creates expected log files. |
| 9 | + Base test case class for cross-platform unit tests. |
| 10 | +
|
| 11 | + Provides: |
| 12 | + 1. A unique temporary directory for each test to avoid filesystem conflicts. |
| 13 | + 2. Automatic restoration of the working directory after each test. |
| 14 | + 3. Prepares a logs folder path for tests that need logging configuration. |
13 | 15 | """ |
14 | 16 |
|
15 | 17 | def setUp(self): |
16 | | - # Unique temporary test directory |
| 18 | + """ |
| 19 | + Prepare the test environment before each test method runs. |
| 20 | +
|
| 21 | + Actions performed: |
| 22 | + 1. Creates a unique temporary directory for the test. |
| 23 | + 2. Creates a 'logs' subdirectory within the temp directory. |
| 24 | + 3. Changes the current working directory to the temporary directory. |
| 25 | + """ |
| 26 | + # Create a unique temporary test directory |
17 | 27 | self.test_dir = tempfile.mkdtemp(prefix="CodeEntropy_") |
18 | | - self._orig_dir = os.getcwd() |
19 | | - os.chdir(self.test_dir) |
20 | | - |
21 | | - # Unique job folder + logs |
22 | | - self.job_id = f"job_{uuid.uuid4().hex[:6]}" |
23 | | - self.job_path = os.path.join(self.test_dir, self.job_id) |
24 | | - self.logs_path = os.path.join(self.job_path, "logs") |
| 28 | + self.logs_path = os.path.join(self.test_dir, "logs") |
25 | 29 | os.makedirs(self.logs_path, exist_ok=True) |
26 | 30 |
|
27 | | - # Pre-create log files |
28 | | - for fname in ["mdanalysis.log", "program.log", "program.com"]: |
29 | | - with open(os.path.join(self.logs_path, fname), "w") as f: |
30 | | - f.write("") |
| 31 | + self._orig_dir = os.getcwd() |
| 32 | + os.chdir(self.test_dir) |
31 | 33 |
|
32 | 34 | def tearDown(self): |
33 | | - # Shutdown logging and remove handlers (important for Windows) |
34 | | - logging.shutdown() |
35 | | - for handler in logging.root.handlers[:]: |
36 | | - logging.root.removeHandler(handler) |
| 35 | + """ |
| 36 | + Clean up the test environment after each test method runs. |
37 | 37 |
|
38 | | - # Restore working directory |
| 38 | + Actions performed: |
| 39 | + 1. Restores the original working directory. |
| 40 | + 2. Deletes the temporary test directory along with all its contents. |
| 41 | + """ |
39 | 42 | os.chdir(self._orig_dir) |
40 | 43 |
|
41 | | - # Remove temp directory (fail loudly if locked) |
42 | 44 | if os.path.exists(self.test_dir): |
43 | | - shutil.rmtree(self.test_dir, ignore_errors=False) |
| 45 | + shutil.rmtree(self.test_dir, ignore_errors=True) |
0 commit comments