commit:     e9bdb7342b3048ab3236bff9d94ce733bb877d8e
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Feb 27 04:02:28 2024 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Tue Feb 27 04:03:07 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=e9bdb734

BinarytreeTestCase: Use temporary TMPDIR

Create a temporary TMPDIR which prevents test methods of this
class from leaving behind an empty /tmp/Packages file if TMPDIR
is initially unset.

Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/tests/dbapi/test_bintree.py | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/lib/portage/tests/dbapi/test_bintree.py 
b/lib/portage/tests/dbapi/test_bintree.py
index 018f1cf9bd..91ac338a05 100644
--- a/lib/portage/tests/dbapi/test_bintree.py
+++ b/lib/portage/tests/dbapi/test_bintree.py
@@ -1,4 +1,4 @@
-# Copyright 2022 Gentoo Authors
+# Copyright 2022-2024 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 from unittest.mock import MagicMock, patch, call
@@ -13,6 +13,26 @@ from portage.const import BINREPOS_CONF_FILE
 
 
 class BinarytreeTestCase(TestCase):
+    @classmethod
+    def setUpClass(cls):
+        """
+        Create a temporary TMPDIR which prevents test
+        methods of this class from leaving behind an empty
+        /tmp/Packages file if TMPDIR is initially unset.
+        """
+        cls._orig_tmpdir = os.environ.get("TMPDIR")
+        cls._tmpdir = tempfile.TemporaryDirectory()
+        os.environ["TMPDIR"] = cls._tmpdir.name
+
+    @classmethod
+    def tearDownClass(cls):
+        cls._tmpdir.cleanup()
+        if cls._orig_tmpdir is None:
+            os.environ.pop("TMPDIR", None)
+        else:
+            os.environ["TMPDIR"] = cls._orig_tmpdir
+        del cls._orig_tmpdir, cls._tmpdir
+
     def test_required_init_params(self):
         with self.assertRaises(TypeError) as cm:
             binarytree()

Reply via email to