From 7a6ec5616d071e3b21f0ce1c07c66388b3cce81a Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 8 Mar 2021 19:12:08 -0800 Subject: [PATCH] clean up mkdtemp usage Committed via https://github.com/asottile/all-repos --- doc/en/fixture.rst | 12 +++++------- testing/test_assertrewrite.py | 6 ++---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 028786f65..50bc9ee8e 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -2228,7 +2228,6 @@ file: # content of conftest.py import os - import shutil import tempfile import pytest @@ -2236,12 +2235,11 @@ file: @pytest.fixture def cleandir(): - old_cwd = os.getcwd() - newpath = tempfile.mkdtemp() - os.chdir(newpath) - yield - os.chdir(old_cwd) - shutil.rmtree(newpath) + with tempfile.TemporaryDirectory() as newpath: + old_cwd = os.getcwd() + os.chdir(newpath) + yield + os.chdir(old_cwd) and declare its use in a test module via a ``usefixtures`` marker: diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index cba03406e..f7d9d62ef 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1395,12 +1395,10 @@ class TestEarlyRewriteBailout: **{ "test_setup_nonexisting_cwd.py": """\ import os - import shutil import tempfile - d = tempfile.mkdtemp() - os.chdir(d) - shutil.rmtree(d) + with tempfile.TemporaryDirectory() as d: + os.chdir(d) """, "test_test.py": """\ def test():