-
Notifications
You must be signed in to change notification settings - Fork 4
test working directory #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe pull request introduces a new test class Changes
Possibly related PRs
Poem
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
tests/test_executor_backend_mpi.py (2)
126-130
: Add more comprehensive test casesThe current test only covers basic calculation with a single core. Consider adding:
- Test cases for multi-core scenarios with working directory
- Tests that verify file operations and side effects
- Tests for concurrent access to the working directory
Example test to add:
def test_output_files_cwd_multicore(self): def calc_with_file(i): # Write to a file to verify working directory with open(f"output_{i}.txt", "w") as f: f.write(str(i)) return i with Executor( max_cores=2, resource_dict={"cores": 2, "cwd": self.test_dir}, backend="local", block_allocation=True, ) as p: output = p.map(calc_with_file, [1, 2, 3]) # Verify files were created in working directory for i in [1, 2, 3]: self.assertTrue(os.path.exists( os.path.join(self.test_dir, f"output_{i}.txt") )) self.assertEqual(list(output), [1, 2, 3])
116-116
: Consider improving test organizationThe test classes could benefit from:
- A base test class with common setup/teardown logic
- Shared helper methods for directory management
- Constants for common test configurations
Example refactoring:
class ExecutorTestBase(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 create_executor(self, **kwargs): default_config = { "max_cores": 1, "backend": "local", "block_allocation": True, } return Executor(**{**default_config, **kwargs}) class TestWorkingDirectory(ExecutorTestBase): # Your tests here
dirname = os.path.abspath(os.path.dirname(__file__)) | ||
os.makedirs(dirname, exist_ok=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary directory creation of the test file's directory
Creating the directory of the test file itself (__file__
) is problematic because:
- The directory already exists (the test file is in it)
- It could cause permission issues
- 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.
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) |
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], | ||
) |
There was a problem hiding this comment.
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:
- Adding cleanup in tearDown
- Testing file operations in the working directory
- 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.
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 |
Summary by CodeRabbit