[test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 24 Oct 2016 09:31:55 +0200
changeset 11744 a6dc650bc230
parent 11743 48d70d143dc1
child 11745 f79dc500a4e7
[test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory logilab-common's with_tempdir decorator, which is considered harmful (see https://www.logilab.org/ticket/8267966). Rely on backports.tempfile to provide TemporaryDirectory on Python 2. Re-export it in testlib module for convenience.
cubicweb/devtools/qunit.py
cubicweb/devtools/testlib.py
cubicweb/server/test/unittest_migractions.py
cubicweb/test/unittest_cwconfig.py
tox.ini
--- a/cubicweb/devtools/qunit.py	Mon Oct 24 09:29:08 2016 +0200
+++ b/cubicweb/devtools/qunit.py	Mon Oct 24 09:31:55 2016 +0200
@@ -25,7 +25,7 @@
 from six.moves.queue import Queue, Empty
 
 # imported by default to simplify further import statements
-from logilab.common.testlib import with_tempdir, Tags
+from logilab.common.testlib import Tags
 import webtest.http
 
 import cubicweb
@@ -33,6 +33,7 @@
 from cubicweb.web.controller import Controller
 from cubicweb.web.views.staticcontrollers import StaticFileController, STATIC_CONTROLLERS
 from cubicweb.devtools import webtest as cwwebtest
+from cubicweb.devtools.testlib import TemporaryDirectory
 
 
 class FirefoxHelper(object):
@@ -109,11 +110,11 @@
                 depends = args[1]
             else:
                 depends = ()
-            for name, func, args in self._test_qunit(test_file, depends):
-                with self.subTest(name=name):
-                    func(*args)
+            with TemporaryDirectory():
+                for name, func, args in self._test_qunit(test_file, depends):
+                    with self.subTest(name=name):
+                        func(*args)
 
-    @with_tempdir
     def _test_qunit(self, test_file, depends=(), timeout=10):
         QUnitView.test_file = test_file
         QUnitView.depends = depends
--- a/cubicweb/devtools/testlib.py	Mon Oct 24 09:29:08 2016 +0200
+++ b/cubicweb/devtools/testlib.py	Mon Oct 24 09:31:55 2016 +0200
@@ -26,6 +26,7 @@
 from contextlib import contextmanager
 from inspect import isgeneratorfunction
 from itertools import chain
+import tempfile
 
 from six import text_type, string_types
 from six.moves import range
@@ -56,8 +57,10 @@
     from unittest2 import TestCase
     if not hasattr(TestCase, 'subTest'):
         raise ImportError('no subTest support in available unittest2')
+    from backports.tempfile import TemporaryDirectory  # noqa
 else:
     from unittest import TestCase
+    from tempfile import TemporaryDirectory  # noqa
 
 # in python 2.7, DeprecationWarning are not shown anymore by default
 warnings.filterwarnings('default', category=DeprecationWarning)
--- a/cubicweb/server/test/unittest_migractions.py	Mon Oct 24 09:29:08 2016 +0200
+++ b/cubicweb/server/test/unittest_migractions.py	Mon Oct 24 09:31:55 2016 +0200
@@ -24,7 +24,7 @@
 import tempfile
 from hashlib import md5
 
-from logilab.common.testlib import unittest_main, Tags, tag, with_tempdir
+from logilab.common.testlib import unittest_main, Tags, tag
 from logilab.common import tempattr
 
 from yams.constraints import UniqueConstraint
@@ -32,7 +32,7 @@
 from cubicweb import (ConfigurationError, ValidationError,
                       ExecutionError, Binary)
 from cubicweb.devtools import startpgcluster, stoppgcluster
-from cubicweb.devtools.testlib import CubicWebTC
+from cubicweb.devtools.testlib import CubicWebTC, TemporaryDirectory
 from cubicweb.server.sqlutils import SQL_PREFIX
 from cubicweb.server.migractions import ServerMigrationHelper
 from cubicweb.server.sources import storages
@@ -798,28 +798,28 @@
             mh.drop_relation_definition('Note', 'ecrit_par', 'CWUser')
             self.assertFalse(mh.sqlexec('SELECT * FROM cw_Note WHERE cw_ecrit_par IS NOT NULL'))
 
-    @with_tempdir
     def test_storage_changed(self):
         with self.mh() as (cnx, mh):
             john = mh.cmd_create_entity('Personne', nom=u'john',
                                         photo=Binary(b'something'))
             bill = mh.cmd_create_entity('Personne', nom=u'bill')
             mh.commit()
-            bfs_storage = storages.BytesFileSystemStorage(tempfile.tempdir)
-            storages.set_attribute_storage(self.repo, 'Personne', 'photo', bfs_storage)
-            mh.cmd_storage_changed('Personne', 'photo')
-            bob = mh.cmd_create_entity('Personne', nom=u'bob')
-            bffss_dir_content = os.listdir(tempfile.tempdir)
-            self.assertEqual(len(bffss_dir_content), 1)
-            john.cw_clear_all_caches()
-            self.assertEqual(john.photo.getvalue(),
-                             osp.join(tempfile.tempdir,
-                                      bffss_dir_content[0]).encode('utf8'))
-            bob.cw_clear_all_caches()
-            self.assertIsNone(bob.photo)
-            bill.cw_clear_all_caches()
-            self.assertIsNone(bill.photo)
-            storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
+            with TemporaryDirectory() as tempdir:
+                bfs_storage = storages.BytesFileSystemStorage(tempdir)
+                storages.set_attribute_storage(self.repo, 'Personne', 'photo', bfs_storage)
+                mh.cmd_storage_changed('Personne', 'photo')
+                bob = mh.cmd_create_entity('Personne', nom=u'bob')
+                bffss_dir_content = os.listdir(tempdir)
+                self.assertEqual(len(bffss_dir_content), 1)
+                john.cw_clear_all_caches()
+                self.assertEqual(john.photo.getvalue(),
+                                 osp.join(tempdir,
+                                          bffss_dir_content[0]).encode('utf8'))
+                bob.cw_clear_all_caches()
+                self.assertIsNone(bob.photo)
+                bill.cw_clear_all_caches()
+                self.assertIsNone(bill.photo)
+                storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
 
 
 class MigrationCommandsComputedTC(MigrationTC):
--- a/cubicweb/test/unittest_cwconfig.py	Mon Oct 24 09:29:08 2016 +0200
+++ b/cubicweb/test/unittest_cwconfig.py	Mon Oct 24 09:31:55 2016 +0200
@@ -19,7 +19,6 @@
 
 import sys
 import os
-import tempfile
 from os.path import dirname, join, abspath
 from pkg_resources import EntryPoint, Distribution
 import unittest
@@ -28,10 +27,10 @@
 from six import PY3
 
 from logilab.common.modutils import cleanup_sys_modules
-from logilab.common.testlib import with_tempdir
 from logilab.common.changelog import Version
 
-from cubicweb.devtools import ApptestConfiguration, testlib
+from cubicweb.devtools import ApptestConfiguration
+from cubicweb.devtools.testlib import BaseTestCase, TemporaryDirectory
 from cubicweb.cwconfig import _find_prefix
 
 
@@ -45,7 +44,7 @@
     raise Exception('duh? %s' % path)
 
 
-class CubicWebConfigurationTC(testlib.BaseTestCase):
+class CubicWebConfigurationTC(BaseTestCase):
 
     @classmethod
     def setUpClass(cls):
@@ -220,97 +219,95 @@
 
 
 class FindPrefixTC(unittest.TestCase):
-    def make_dirs(self, *args):
-        path = join(tempfile.tempdir, *args)
+
+    def make_dirs(self, basedir, *args):
+        path = join(basedir, *args)
         if not os.path.exists(path):
             os.makedirs(path)
         return path
 
-    def make_file(self, *args):
-        self.make_dirs(*args[: -1])
-        file_path = join(tempfile.tempdir, *args)
+    def make_file(self, basedir, *args):
+        self.make_dirs(basedir, *args[:-1])
+        file_path = join(basedir, *args)
         with open(file_path, 'w') as f:
             f.write('""" None """')
         return file_path
 
-    @with_tempdir
     def test_samedir(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        self.assertEqual(_find_prefix(prefix), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            self.assertEqual(_find_prefix(prefix), prefix)
 
-    @with_tempdir
     def test_samedir_filepath(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        file_path = self.make_file('bob.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'bob.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_dir_inside_prefix(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        dir_path = self.make_dirs('bob')
-        self.assertEqual(_find_prefix(dir_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            dir_path = self.make_dirs(prefix, 'bob')
+            self.assertEqual(_find_prefix(dir_path), prefix)
 
-    @with_tempdir
     def test_file_in_dir_inside_prefix(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        file_path = self.make_file('bob', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'bob', 'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_file_in_deeper_dir_inside_prefix(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'bob', 'pyves', 'alain',
+                                       'adim', 'syt', 'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_multiple_candidate_prefix(self):
-        self.make_dirs('share', 'cubicweb')
-        prefix = self.make_dirs('bob')
-        self.make_dirs('bob', 'share', 'cubicweb')
-        file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as tempdir:
+            self.make_dirs(tempdir, 'share', 'cubicweb')
+            prefix = self.make_dirs(tempdir, 'bob')
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'pyves', 'alain',
+                                       'adim', 'syt', 'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_sister_candidate_prefix(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        self.make_dirs('bob', 'share', 'cubicweb')
-        file_path = self.make_file('bell', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            self.make_dirs(prefix, 'bob', 'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'bell', 'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_multiple_parent_candidate_prefix(self):
-        self.make_dirs('share', 'cubicweb')
-        prefix = self.make_dirs('share', 'cubicweb', 'bob')
-        self.make_dirs('share', 'cubicweb', 'bob', 'share', 'cubicweb')
-        file_path = self.make_file('share', 'cubicweb', 'bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as tempdir:
+            self.make_dirs(tempdir, 'share', 'cubicweb')
+            prefix = self.make_dirs(tempdir, 'share', 'cubicweb', 'bob')
+            self.make_dirs(tempdir, 'share', 'cubicweb', 'bob', 'share',
+                           'cubicweb')
+            file_path = self.make_file(tempdir, 'share', 'cubicweb', 'bob',
+                                       'pyves', 'alain', 'adim', 'syt',
+                                       'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_upper_candidate_prefix(self):
-        prefix = tempfile.tempdir
-        self.make_dirs('share', 'cubicweb')
-        self.make_dirs('bell','bob',  'share', 'cubicweb')
-        file_path = self.make_file('bell', 'toto.py')
-        self.assertEqual(_find_prefix(file_path), prefix)
+        with TemporaryDirectory() as prefix:
+            self.make_dirs(prefix, 'share', 'cubicweb')
+            self.make_dirs(prefix, 'bell', 'bob',  'share', 'cubicweb')
+            file_path = self.make_file(prefix, 'bell', 'toto.py')
+            self.assertEqual(_find_prefix(file_path), prefix)
 
-    @with_tempdir
     def test_no_prefix(self):
-        prefix = tempfile.tempdir
-        self.assertEqual(_find_prefix(prefix), sys.prefix)
+        with TemporaryDirectory() as prefix:
+            self.assertEqual(_find_prefix(prefix), sys.prefix)
 
-    @with_tempdir
     def test_virtualenv(self):
         venv = os.environ.get('VIRTUAL_ENV')
         try:
-            prefix = os.environ['VIRTUAL_ENV'] = tempfile.tempdir
-            self.make_dirs('share', 'cubicweb')
-            self.assertEqual(_find_prefix(), prefix)
+            with TemporaryDirectory() as prefix:
+                os.environ['VIRTUAL_ENV'] = prefix
+                self.make_dirs(prefix, 'share', 'cubicweb')
+                self.assertEqual(_find_prefix(), prefix)
         finally:
             if venv:
                 os.environ['VIRTUAL_ENV'] = venv
--- a/tox.ini	Mon Oct 24 09:29:08 2016 +0200
+++ b/tox.ini	Mon Oct 24 09:31:55 2016 +0200
@@ -9,6 +9,7 @@
   /usr/bin/touch
 deps =
   -r{toxinidir}/requirements/dev.txt
+  py27: backports.tempfile
   misc: -r{toxinidir}/requirements/test-misc.txt
   server: -r{toxinidir}/requirements/test-server.txt
   web: -r{toxinidir}/requirements/test-web.txt