[test] use TemporaryDirectory context manager
For consistency with others tests, use TemporaryDirectory from
cubicweb.devtools.testlib.
--- a/cubicweb/devtools/test/unittest_devctl.py Fri Jan 20 14:32:34 2017 +0100
+++ b/cubicweb/devtools/test/unittest_devctl.py Fri Jan 20 16:53:28 2017 +0100
@@ -20,11 +20,11 @@
import os
import os.path as osp
import sys
-import tempfile
-import shutil
from subprocess import Popen, PIPE, STDOUT, check_output
from unittest import TestCase
+from cubicweb.devtools.testlib import TemporaryDirectory
+
def newcube(directory, name):
cmd = ['cubicweb-ctl', 'newcube', '--directory', directory, name]
@@ -51,8 +51,7 @@
expected_package_content = ['i18n', 'hooks.py', 'views.py',
'migration', 'entities.py', 'schema.py',
'__init__.py', 'data', '__pkginfo__.py']
- tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube")
- try:
+ with TemporaryDirectory(prefix="temp-cwctl-newcube") as tmpdir:
retcode, stdout = newcube(tmpdir, 'foo')
self.assertEqual(retcode, 0, msg=to_unicode(stdout))
project_dir = osp.join(tmpdir, 'cubicweb-foo')
@@ -61,27 +60,21 @@
package_content = os.listdir(package_dir)
self.assertItemsEqual(project_content, expected_project_content)
self.assertItemsEqual(package_content, expected_package_content)
- finally:
- shutil.rmtree(tmpdir, ignore_errors=True)
def test_flake8(self):
"""Ensure newcube built from skeleton is flake8-compliant"""
- tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-flake8")
- try:
+ with TemporaryDirectory(prefix="temp-cwctl-newcube-flake8") as tmpdir:
newcube(tmpdir, 'foo')
cmd = [sys.executable, '-m', 'flake8',
osp.join(tmpdir, 'cubicweb-foo', 'cubicweb_foo')]
proc = Popen(cmd, stdout=PIPE, stderr=STDOUT)
retcode = proc.wait()
- finally:
- shutil.rmtree(tmpdir, ignore_errors=True)
self.assertEqual(retcode, 0,
msg=to_unicode(proc.stdout.read()))
def test_newcube_sdist(self):
"""Ensure sdist can be built from a new cube"""
- tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-sdist")
- try:
+ with TemporaryDirectory(prefix="temp-cwctl-newcube-sdist") as tmpdir:
newcube(tmpdir, 'foo')
projectdir = osp.join(tmpdir, 'cubicweb-foo')
cmd = [sys.executable, 'setup.py', 'sdist']
@@ -91,13 +84,10 @@
self.assertEqual(retcode, 0, stdout)
distfpath = osp.join(projectdir, 'dist', 'cubicweb-foo-0.1.0.tar.gz')
self.assertTrue(osp.isfile(distfpath))
- finally:
- shutil.rmtree(tmpdir, ignore_errors=True)
def test_newcube_install(self):
"""Ensure a new cube can be installed"""
- tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-install")
- try:
+ with TemporaryDirectory(prefix="temp-cwctl-newcube-install") as tmpdir:
newcube(tmpdir, 'foo')
projectdir = osp.join(tmpdir, 'cubicweb-foo')
env = os.environ.copy()
@@ -120,8 +110,6 @@
self.assertItemsEqual(pkgcontent,
[b'schema.py', b'entities.py', b'hooks.py', b'__init__.py',
b'__pkginfo__.py', b'views.py'])
- finally:
- shutil.rmtree(tmpdir, ignore_errors=True)
if __name__ == '__main__':
--- a/cubicweb/test/unittest_cubes.py Fri Jan 20 14:32:34 2017 +0100
+++ b/cubicweb/test/unittest_cubes.py Fri Jan 20 16:53:28 2017 +0100
@@ -20,33 +20,31 @@
from contextlib import contextmanager
import os
from os import path
-import shutil
import sys
-import tempfile
import unittest
from six import PY2
from cubicweb import _CubesImporter
from cubicweb.cwconfig import CubicWebConfiguration
+from cubicweb.devtools.testlib import TemporaryDirectory
@contextmanager
def temp_cube():
- tempdir = tempfile.mkdtemp()
- try:
- libdir = path.join(tempdir, 'libpython')
- cubedir = path.join(libdir, 'cubicweb_foo')
- os.makedirs(cubedir)
- with open(path.join(cubedir, '__init__.py'), 'w') as f:
- f.write('"""cubicweb_foo application package"""')
- with open(path.join(cubedir, 'bar.py'), 'w') as f:
- f.write('baz = 1')
- sys.path.append(libdir)
- yield cubedir
- finally:
- shutil.rmtree(tempdir)
- sys.path.remove(libdir)
+ with TemporaryDirectory() as tempdir:
+ try:
+ libdir = path.join(tempdir, 'libpython')
+ cubedir = path.join(libdir, 'cubicweb_foo')
+ os.makedirs(cubedir)
+ with open(path.join(cubedir, '__init__.py'), 'w') as f:
+ f.write('"""cubicweb_foo application package"""')
+ with open(path.join(cubedir, 'bar.py'), 'w') as f:
+ f.write('baz = 1')
+ sys.path.append(libdir)
+ yield cubedir
+ finally:
+ sys.path.remove(libdir)
class CubesImporterTC(unittest.TestCase):