cubicweb/devtools/test/unittest_devctl.py
changeset 12071 fb0b74fecc4d
parent 12053 c3c9f2e1424c
child 12567 26744ad37953
child 12688 8da08cc8640f
equal deleted inserted replaced
12070:a44f67dd0474 12071:fb0b74fecc4d
    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 from subprocess import Popen, PIPE, STDOUT, check_output
    23 from subprocess import Popen, PIPE, STDOUT
    24 from unittest import TestCase
    24 from unittest import TestCase
    25 
    25 
    26 from cubicweb.devtools.testlib import TemporaryDirectory
    26 from cubicweb.devtools.testlib import TemporaryDirectory
    27 
    27 
    28 
    28 
    90         with TemporaryDirectory(prefix="temp-cwctl-newcube-install") as tmpdir:
    90         with TemporaryDirectory(prefix="temp-cwctl-newcube-install") as tmpdir:
    91             newcube(tmpdir, 'foo')
    91             newcube(tmpdir, 'foo')
    92             projectdir = osp.join(tmpdir, 'cubicweb-foo')
    92             projectdir = osp.join(tmpdir, 'cubicweb-foo')
    93             env = os.environ.copy()
    93             env = os.environ.copy()
    94             env['HOME'] = tmpdir
    94             env['HOME'] = tmpdir
       
    95             # Retrieve user-site first to later set the PYTHONPATH.
       
    96             proc = Popen([sys.executable, '-m', 'site', '--user-site'],
       
    97                          stdout=PIPE, stderr=PIPE, env=env, cwd=tmpdir)
       
    98             retcode = proc.wait()
       
    99             # Exit codes below 2 are not errors and are ok for us (see
       
   100             # 'python3 -m site --help' for details).
       
   101             self.assertFalse(retcode > 2, to_unicode(proc.stderr.read()))
       
   102             user_site = proc.stdout.read().strip()
       
   103             # We need this otherwise setuptools will complain that the
       
   104             # installation target is not in PYTHONPATH (probably because we
       
   105             # use a fake HOME).
       
   106             env['PYTHONPATH'] = user_site
    95             cmd = [sys.executable, 'setup.py', 'install', '--user']
   107             cmd = [sys.executable, 'setup.py', 'install', '--user']
    96             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT,
   108             proc = Popen(cmd, stdout=PIPE, stderr=STDOUT,
    97                          cwd=projectdir, env=env)
   109                          cwd=projectdir, env=env)
    98             retcode = proc.wait()
   110             retcode = proc.wait()
    99             stdout = to_unicode(proc.stdout.read())
   111             stdout = to_unicode(proc.stdout.read())
   100             self.assertEqual(retcode, 0, stdout)
   112             self.assertEqual(retcode, 0, stdout)
   101             targetdir = check_output([sys.executable, '-m', 'site', '--user-site'],
       
   102                                      env=env, cwd=projectdir).strip()
       
   103             target_egg = 'cubicweb_foo-0.1.0-py{0}.egg'.format(sys.version[:3]).encode()
   113             target_egg = 'cubicweb_foo-0.1.0-py{0}.egg'.format(sys.version[:3]).encode()
   104             self.assertTrue(osp.isdir(osp.join(targetdir, target_egg)),
   114             self.assertTrue(osp.isdir(osp.join(user_site, target_egg)),
   105                             'target directory content: %s' % os.listdir(targetdir))
   115                             'target directory content: %s' % os.listdir(user_site))
   106             pkgdir = osp.join(targetdir, target_egg, b'cubicweb_foo')
   116             pkgdir = osp.join(user_site, target_egg, b'cubicweb_foo')
   107             self.assertTrue(osp.isdir(pkgdir),
   117             self.assertTrue(osp.isdir(pkgdir),
   108                             os.listdir(osp.join(targetdir, target_egg)))
   118                             os.listdir(osp.join(user_site, target_egg)))
   109             pkgcontent = [f for f in os.listdir(pkgdir) if f.endswith(b'.py')]
   119             pkgcontent = [f for f in os.listdir(pkgdir) if f.endswith(b'.py')]
   110             self.assertItemsEqual(pkgcontent,
   120             self.assertItemsEqual(pkgcontent,
   111                                   [b'schema.py', b'entities.py', b'hooks.py', b'__init__.py',
   121                                   [b'schema.py', b'entities.py', b'hooks.py', b'__init__.py',
   112                                    b'__pkginfo__.py', b'views.py'])
   122                                    b'__pkginfo__.py', b'views.py'])
   113 
   123