cubicweb/devtools/test/unittest_devctl.py
changeset 11912 c9e6df20e5a4
parent 11456 077f32a7a4c3
child 11945 ef6b18c56b5a
equal deleted inserted replaced
11911:37726f66ff82 11912:c9e6df20e5a4
    18 """unit tests for cubicweb-ctl commands from devtools"""
    18 """unit tests for cubicweb-ctl commands from devtools"""
    19 
    19 
    20 import os
    20 import os
    21 import os.path as osp
    21 import os.path as osp
    22 import sys
    22 import sys
    23 import tempfile
       
    24 import shutil
       
    25 from subprocess import Popen, PIPE, STDOUT, check_output
    23 from subprocess import Popen, PIPE, STDOUT, check_output
    26 from unittest import TestCase
    24 from unittest import TestCase
       
    25 
       
    26 from cubicweb.devtools.testlib import TemporaryDirectory
    27 
    27 
    28 
    28 
    29 def newcube(directory, name):
    29 def newcube(directory, name):
    30     cmd = ['cubicweb-ctl', 'newcube', '--directory', directory, name]
    30     cmd = ['cubicweb-ctl', 'newcube', '--directory', directory, name]
    31     proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    31     proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    49                                     'cubicweb-foo.spec', 'debian', 'README',
    49                                     'cubicweb-foo.spec', 'debian', 'README',
    50                                     'tox.ini']
    50                                     'tox.ini']
    51         expected_package_content = ['i18n', 'hooks.py', 'views.py',
    51         expected_package_content = ['i18n', 'hooks.py', 'views.py',
    52                                     'migration', 'entities.py', 'schema.py',
    52                                     'migration', 'entities.py', 'schema.py',
    53                                     '__init__.py', 'data', '__pkginfo__.py']
    53                                     '__init__.py', 'data', '__pkginfo__.py']
    54         tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube")
    54         with TemporaryDirectory(prefix="temp-cwctl-newcube") as tmpdir:
    55         try:
       
    56             retcode, stdout = newcube(tmpdir, 'foo')
    55             retcode, stdout = newcube(tmpdir, 'foo')
    57             self.assertEqual(retcode, 0, msg=to_unicode(stdout))
    56             self.assertEqual(retcode, 0, msg=to_unicode(stdout))
    58             project_dir = osp.join(tmpdir, 'cubicweb-foo')
    57             project_dir = osp.join(tmpdir, 'cubicweb-foo')
    59             project_content = os.listdir(project_dir)
    58             project_content = os.listdir(project_dir)
    60             package_dir = osp.join(project_dir, 'cubicweb_foo')
    59             package_dir = osp.join(project_dir, 'cubicweb_foo')
    61             package_content = os.listdir(package_dir)
    60             package_content = os.listdir(package_dir)
    62             self.assertItemsEqual(project_content, expected_project_content)
    61             self.assertItemsEqual(project_content, expected_project_content)
    63             self.assertItemsEqual(package_content, expected_package_content)
    62             self.assertItemsEqual(package_content, expected_package_content)
    64         finally:
       
    65             shutil.rmtree(tmpdir, ignore_errors=True)
       
    66 
    63 
    67     def test_flake8(self):
    64     def test_flake8(self):
    68         """Ensure newcube built from skeleton is flake8-compliant"""
    65         """Ensure newcube built from skeleton is flake8-compliant"""
    69         tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-flake8")
    66         with TemporaryDirectory(prefix="temp-cwctl-newcube-flake8") as tmpdir:
    70         try:
       
    71             newcube(tmpdir, 'foo')
    67             newcube(tmpdir, 'foo')
    72             cmd = [sys.executable, '-m', 'flake8',
    68             cmd = [sys.executable, '-m', 'flake8',
    73                    osp.join(tmpdir, 'cubicweb-foo', 'cubicweb_foo')]
    69                    osp.join(tmpdir, 'cubicweb-foo', 'cubicweb_foo')]
    74             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT)
    70             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT)
    75             retcode = proc.wait()
    71             retcode = proc.wait()
    76         finally:
       
    77             shutil.rmtree(tmpdir, ignore_errors=True)
       
    78         self.assertEqual(retcode, 0,
    72         self.assertEqual(retcode, 0,
    79                          msg=to_unicode(proc.stdout.read()))
    73                          msg=to_unicode(proc.stdout.read()))
    80 
    74 
    81     def test_newcube_sdist(self):
    75     def test_newcube_sdist(self):
    82         """Ensure sdist can be built from a new cube"""
    76         """Ensure sdist can be built from a new cube"""
    83         tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-sdist")
    77         with TemporaryDirectory(prefix="temp-cwctl-newcube-sdist") as tmpdir:
    84         try:
       
    85             newcube(tmpdir, 'foo')
    78             newcube(tmpdir, 'foo')
    86             projectdir = osp.join(tmpdir, 'cubicweb-foo')
    79             projectdir = osp.join(tmpdir, 'cubicweb-foo')
    87             cmd = [sys.executable, 'setup.py', 'sdist']
    80             cmd = [sys.executable, 'setup.py', 'sdist']
    88             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT, cwd=projectdir)
    81             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT, cwd=projectdir)
    89             retcode = proc.wait()
    82             retcode = proc.wait()
    90             stdout = to_unicode(proc.stdout.read())
    83             stdout = to_unicode(proc.stdout.read())
    91             self.assertEqual(retcode, 0, stdout)
    84             self.assertEqual(retcode, 0, stdout)
    92             distfpath = osp.join(projectdir, 'dist', 'cubicweb-foo-0.1.0.tar.gz')
    85             distfpath = osp.join(projectdir, 'dist', 'cubicweb-foo-0.1.0.tar.gz')
    93             self.assertTrue(osp.isfile(distfpath))
    86             self.assertTrue(osp.isfile(distfpath))
    94         finally:
       
    95             shutil.rmtree(tmpdir, ignore_errors=True)
       
    96 
    87 
    97     def test_newcube_install(self):
    88     def test_newcube_install(self):
    98         """Ensure a new cube can be installed"""
    89         """Ensure a new cube can be installed"""
    99         tmpdir = tempfile.mkdtemp(prefix="temp-cwctl-newcube-install")
    90         with TemporaryDirectory(prefix="temp-cwctl-newcube-install") as tmpdir:
   100         try:
       
   101             newcube(tmpdir, 'foo')
    91             newcube(tmpdir, 'foo')
   102             projectdir = osp.join(tmpdir, 'cubicweb-foo')
    92             projectdir = osp.join(tmpdir, 'cubicweb-foo')
   103             env = os.environ.copy()
    93             env = os.environ.copy()
   104             env['HOME'] = tmpdir
    94             env['HOME'] = tmpdir
   105             cmd = [sys.executable, 'setup.py', 'install', '--user']
    95             cmd = [sys.executable, 'setup.py', 'install', '--user']
   118                             os.listdir(osp.join(targetdir, target_egg)))
   108                             os.listdir(osp.join(targetdir, target_egg)))
   119             pkgcontent = [f for f in os.listdir(pkgdir) if f.endswith(b'.py')]
   109             pkgcontent = [f for f in os.listdir(pkgdir) if f.endswith(b'.py')]
   120             self.assertItemsEqual(pkgcontent,
   110             self.assertItemsEqual(pkgcontent,
   121                                   [b'schema.py', b'entities.py', b'hooks.py', b'__init__.py',
   111                                   [b'schema.py', b'entities.py', b'hooks.py', b'__init__.py',
   122                                    b'__pkginfo__.py', b'views.py'])
   112                                    b'__pkginfo__.py', b'views.py'])
   123         finally:
       
   124             shutil.rmtree(tmpdir, ignore_errors=True)
       
   125 
   113 
   126 
   114 
   127 if __name__ == '__main__':
   115 if __name__ == '__main__':
   128     from unittest import main
   116     from unittest import main
   129     main()
   117     main()