--- 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