Skip to content
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

Refactor: remove unnecessary use of tempfile.mkdtemp #5639

Merged
merged 2 commits into from
Sep 22, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Sep 13, 2022

Fixes #4336

When it is used for unit tests, it should be replaced by the tmp_path fixture provided by pytest since it will take care of cleanup automatically.

Then there are a few cases in the code where it was used where it was replaced with tempfile.TemporaryDirectory. The latter has the advantage that it will be automatically cleanup when the object goes out of scope.

A few places in the code still use mkdtemp since it needs to control the cleanup manually.

The temp_dir fixture is also deprecated. It works exactly as the tmp_path fixture which ships with pytest and should be used instead.

@sphuber sphuber force-pushed the fix/4336/remove-tempfile-mkdtemp branch from e4eb8a4 to 7f7449e Compare September 13, 2022 12:19
@sphuber
Copy link
Contributor Author

sphuber commented Sep 13, 2022

Tests seem to randomly fail. See #5640

Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

@sphuber thanks, looks all good just one minor request.

@@ -325,7 +325,7 @@ def create_profile(self):
self.create_aiida_db()

if not self.root_dir:
self.root_dir = tempfile.mkdtemp()
self.root_dir = tempfile.TemporaryDirectory().name # pylint: disable=consider-using-with
Copy link
Member

Choose a reason for hiding this comment

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

So this is not cleanup after use? Why not use it inwith?

@@ -325,7 +325,7 @@ def create_profile(self):
self.create_aiida_db()

if not self.root_dir:
self.root_dir = tempfile.mkdtemp()
self.root_dir = tempfile.TemporaryDirectory().name # pylint: disable=consider-using-with
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.root_dir = tempfile.TemporaryDirectory().name # pylint: disable=consider-using-with
temp_dir = tempfile.TemporaryDirectory()
self.root_dir = temp_dir.name # pylint: disable=consider-using-with
temp_dir.cleanup()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot use it here, because it needs to be kept alive for the duration of the tests. The cleanup is called manually in destroy_all which is called at the end of the test suite.

Copy link
Member

Choose a reason for hiding this comment

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

I see, found that. Thanks!

When it is used for unit tests, it should be replaced by the `tmp_path`
fixture provided by `pytest` since it will take care of cleanup
automatically.

Then there are a few cases in the code where it was used where it was
replaced with `tempfile.TemporaryDirectory`. The latter has the advantage
that it will be automatically cleanup when the object goes out of scope.

A few places in the code still use `mkdtemp` since it needs to control
the cleanup manually.

The `temp_dir` fixture is also deprecated. It works exactly as the
`tmp_path` fixture which ships with `pytest` and should be used instead.
@sphuber sphuber force-pushed the fix/4336/remove-tempfile-mkdtemp branch from c7ff0e4 to d3fdd20 Compare September 22, 2022 13:31
@sphuber sphuber requested a review from unkcpz September 22, 2022 13:32
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

Hi @sphuber, I just search for tmp_dir and find there are some in tests/cmdline/commands/test_node.py that can use this fixture. I think probably better to change all in this PR. What do you think?

@@ -325,7 +325,7 @@ def create_profile(self):
self.create_aiida_db()

if not self.root_dir:
self.root_dir = tempfile.mkdtemp()
self.root_dir = tempfile.TemporaryDirectory().name # pylint: disable=consider-using-with
Copy link
Member

Choose a reason for hiding this comment

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

I see, found that. Thanks!

@sphuber sphuber requested a review from unkcpz September 22, 2022 15:44
@sphuber
Copy link
Contributor Author

sphuber commented Sep 22, 2022

Thanks @unkcpz I have searched for TemporaryDirectory and replaced that could be replaced. There are still some uses in tests/transports/test_all_plugins.py but those tests still use unittest so cannot use the pytest fixture.

Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

LGTM! cheers.

@sphuber sphuber merged commit 1c4df02 into aiidateam:main Sep 22, 2022
@sphuber sphuber deleted the fix/4336/remove-tempfile-mkdtemp branch September 22, 2022 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace 'tempfile.mkdtemp' with 'tempfile.TemporaryDirectory'.
2 participants