-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Adding Temporary Directory Routine #9029
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
Open
ericspod
wants to merge
12
commits into
Project-MONAI:dev
Choose a base branch
from
ericspod:tmpdir_routine
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−3
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a95e44e
Adding create_temp_dir
ericspod d9cd2d1
Faster test
ericspod 5e7e430
Cleanup
ericspod 101064f
DCO Remediation Commit for Eric Kerfoot <17726042+ericspod@users.nore…
ericspod 8592172
Merge branch 'dev' into tmpdir_routine
ericspod a96b14e
Update monai/apps/utils.py
ericspod a0c4948
Updates from comments
ericspod c575c62
Doc update
ericspod f2947d9
Merge branch 'dev' into tmpdir_routine
ericspod 7042c77
Test fix
ericspod 58ec5f5
Merge branch 'tmpdir_routine' of github.com:ericspod/MONAI into tmpdi…
ericspod 61fddee
Fix
ericspod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from monai.apps import create_temp_dir | ||
| from monai.utils import MONAIEnvVars | ||
|
|
||
| MONAI_DATA_DIRECTORY = "MONAI_DATA_DIRECTORY" | ||
|
|
||
|
|
||
| class TestCreateTempDir(unittest.TestCase): | ||
| def test_basic_use(self): | ||
| """Test basic usage which should create a new random temporary directory.""" | ||
|
|
||
| data_dir = os.environ.pop(MONAI_DATA_DIRECTORY, None) # ignore the environment variable if present | ||
| test_dir = None | ||
| try: | ||
| with patch("atexit.register") as mock_reg: | ||
| test_dir = create_temp_dir() | ||
|
|
||
| self.assertTrue(os.path.isdir(test_dir)) | ||
|
|
||
| mock_reg.assert_called_once_with(shutil.rmtree, test_dir, ignore_errors=True) | ||
|
ericspod marked this conversation as resolved.
|
||
| finally: | ||
| if data_dir is not None: | ||
| os.environ[MONAI_DATA_DIRECTORY] = data_dir | ||
| if test_dir is not None: | ||
| shutil.rmtree(test_dir, ignore_errors=True) | ||
|
|
||
| def test_data_dir(self): | ||
| """Test using a mocked MONAI_DATA_DIRECTORY, which should be returned by the function.""" | ||
| with patch("monai.utils.MONAIEnvVars.data_dir") as data_dir, tempfile.TemporaryDirectory() as fake_data_dir: | ||
| data_dir.return_value = fake_data_dir | ||
|
|
||
| self.assertEqual(fake_data_dir, MONAIEnvVars.data_dir()) | ||
|
|
||
| test_dir = create_temp_dir() | ||
|
|
||
| self.assertTrue(os.path.isdir(test_dir)) | ||
| self.assertEqual(test_dir, fake_data_dir) | ||
|
|
||
| def test_given_dir(self): | ||
| """Test giving a directory to the function, ensuring it creates the directory.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| selected_dir = f"{temp_dir}{os.path.sep}test_inner_dir" | ||
|
|
||
| test_dir = create_temp_dir(selected_dir) | ||
|
|
||
| self.assertTrue(os.path.isdir(selected_dir)) | ||
| self.assertEqual(test_dir, selected_dir) | ||
|
|
||
| def test_given_dir_path(self): | ||
| """Test giving a directory as a Path object to the function, ensuring it creates the directory.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| selected_dir = f"{temp_dir}{os.path.sep}test_inner_dir" | ||
|
|
||
| test_dir = create_temp_dir(Path(selected_dir)) | ||
|
|
||
| self.assertTrue(os.path.isdir(selected_dir)) | ||
| self.assertEqual(test_dir, selected_dir) | ||
|
|
||
| def test_finalisation(self): | ||
| """Test the temporary directory is deleted by finalisation.""" | ||
| self.finaliser = None | ||
|
|
||
| def _register(func, /, *args, **kwargs): | ||
| self.finaliser = (func, args, kwargs) | ||
|
|
||
| with patch("atexit.register", new=_register), tempfile.TemporaryDirectory() as temp_dir: | ||
| selected_dir = f"{temp_dir}{os.path.sep}test_inner_dir" | ||
| test_dir = create_temp_dir(selected_dir, True) | ||
|
|
||
| self.assertTrue(os.path.isdir(selected_dir)) | ||
| self.assertIsNotNone(self.finaliser) | ||
|
|
||
| with open(test_dir + "/test_file", "w") as o: | ||
| o.write("Test file data") | ||
|
|
||
| func, args, kwargs = self.finaliser | ||
| func(*args, **kwargs) | ||
|
|
||
| self.assertFalse(os.path.exists(selected_dir)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
test_basic_use exercises the implicit auto-cleanup path (no directory, no MONAI_DATA_DIRECTORY), where delete_on_finalise gets forced to True in monai/apps/utils.py. That's the exact path every notebook migrating to this helper will hit, but this test never asserts atexit.register was called, so a regression here would go unnoticed. Could you patch atexit.register in this test too and assert it gets called when no directory or env var is provided?
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.
This is updated now.