Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_executor_backend_mpi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import importlib.util
import shutil
import time
Expand Down Expand Up @@ -110,3 +111,20 @@ def test_meta_executor_parallel_cache(self):
self.assertTrue(fs_2.done())
time_4 = time.time()
self.assertTrue(time_3 - time_4 < 1)


class TestWorkingDirectory(unittest.TestCase):
def test_output_files_cwd(self):
dirname = os.path.abspath(os.path.dirname(__file__))
os.makedirs(dirname, exist_ok=True)
Comment on lines +118 to +119
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove unnecessary directory creation of the test file's directory

Creating the directory of the test file itself (__file__) is problematic because:

  1. The directory already exists (the test file is in it)
  2. It could cause permission issues
  3. It doesn't actually test working directory functionality

Instead, create a temporary test directory:

-        dirname = os.path.abspath(os.path.dirname(__file__))
-        os.makedirs(dirname, exist_ok=True)
+        dirname = os.path.join(os.path.dirname(__file__), "test_workdir")
+        os.makedirs(dirname)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dirname = os.path.abspath(os.path.dirname(__file__))
os.makedirs(dirname, exist_ok=True)
dirname = os.path.join(os.path.dirname(__file__), "test_workdir")
os.makedirs(dirname)

with Executor(
max_cores=1,
resource_dict={"cores": 1, "cwd": dirname},
backend="local",
block_allocation=True,
) as p:
output = p.map(calc, [1, 2, 3])
self.assertEqual(
list(output),
[1, 2, 3],
)
Comment on lines +116 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage for working directory functionality

The current test only verifies calculation results but doesn't actually test if the working directory is being used correctly. Consider:

  1. Adding cleanup in tearDown
  2. Testing file operations in the working directory
  3. Testing non-existent directory cases

Here's a suggested implementation:

 class TestWorkingDirectory(unittest.TestCase):
+    def setUp(self):
+        self.test_dir = os.path.join(os.path.dirname(__file__), "test_workdir")
+        os.makedirs(self.test_dir)
+
+    def tearDown(self):
+        if os.path.exists(self.test_dir):
+            shutil.rmtree(self.test_dir)
+
     def test_output_files_cwd(self):
-        dirname = os.path.abspath(os.path.dirname(__file__))
-        os.makedirs(dirname, exist_ok=True)
         with Executor(
             max_cores=1,
-            resource_dict={"cores": 1, "cwd": dirname},
+            resource_dict={"cores": 1, "cwd": self.test_dir},
             backend="local",
             block_allocation=True,
         ) as p:
             output = p.map(calc, [1, 2, 3])
+            # Verify working directory was used
+            self.assertEqual(os.getcwd(), self.test_dir)
         self.assertEqual(
             list(output),
             [1, 2, 3],
         )
+
+    def test_nonexistent_workdir(self):
+        nonexistent_dir = os.path.join(self.test_dir, "nonexistent")
+        with self.assertRaises(FileNotFoundError):
+            with Executor(
+                max_cores=1,
+                resource_dict={"cores": 1, "cwd": nonexistent_dir},
+                backend="local",
+                block_allocation=True,
+            ):
+                pass
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class TestWorkingDirectory(unittest.TestCase):
def test_output_files_cwd(self):
dirname = os.path.abspath(os.path.dirname(__file__))
os.makedirs(dirname, exist_ok=True)
with Executor(
max_cores=1,
resource_dict={"cores": 1, "cwd": dirname},
backend="local",
block_allocation=True,
) as p:
output = p.map(calc, [1, 2, 3])
self.assertEqual(
list(output),
[1, 2, 3],
)
class TestWorkingDirectory(unittest.TestCase):
def setUp(self):
self.test_dir = os.path.join(os.path.dirname(__file__), "test_workdir")
os.makedirs(self.test_dir)
def tearDown(self):
if os.path.exists(self.test_dir):
shutil.rmtree(self.test_dir)
def test_output_files_cwd(self):
with Executor(
max_cores=1,
resource_dict={"cores": 1, "cwd": self.test_dir},
backend="local",
block_allocation=True,
) as p:
output = p.map(calc, [1, 2, 3])
# Verify working directory was used
self.assertEqual(os.getcwd(), self.test_dir)
self.assertEqual(
list(output),
[1, 2, 3],
)
def test_nonexistent_workdir(self):
nonexistent_dir = os.path.join(self.test_dir, "nonexistent")
with self.assertRaises(FileNotFoundError):
with Executor(
max_cores=1,
resource_dict={"cores": 1, "cwd": nonexistent_dir},
backend="local",
block_allocation=True,
):
pass

Loading