cubicweb/test/unittest_cwconfig.py
changeset 11459 8987a05950dc
parent 11448 f79c6894381f
child 11472 bc04039acd2e
equal deleted inserted replaced
11458:db2d627e379e 11459:8987a05950dc
    21 import os
    21 import os
    22 import tempfile
    22 import tempfile
    23 from os.path import dirname, join, abspath
    23 from os.path import dirname, join, abspath
    24 import unittest
    24 import unittest
    25 
    25 
       
    26 from six import PY3
       
    27 
    26 from logilab.common.modutils import cleanup_sys_modules
    28 from logilab.common.modutils import cleanup_sys_modules
    27 from logilab.common.testlib import with_tempdir
    29 from logilab.common.testlib import with_tempdir
    28 from logilab.common.changelog import Version
    30 from logilab.common.changelog import Version
    29 
    31 
    30 from cubicweb.devtools import ApptestConfiguration, testlib
    32 from cubicweb.devtools import ApptestConfiguration, testlib
    31 from cubicweb.cwconfig import _find_prefix
    33 from cubicweb.cwconfig import _find_prefix
    32 
    34 
       
    35 
    33 def unabsolutize(path):
    36 def unabsolutize(path):
    34     parts = path.split(os.sep)
    37     parts = path.split(os.sep)
    35     for i, part in reversed(tuple(enumerate(parts))):
    38     for i, part in reversed(tuple(enumerate(parts))):
    36         if part.startswith('cubicweb') or part == 'cubes':
    39         if part.startswith('cubicweb_'):
    37             return '/'.join(parts[i+1:])
    40             return os.sep.join([part[len('cubicweb_'):]] + parts[i+1:])
       
    41         if part.startswith('cubicweb') or part == 'legacy_cubes':
       
    42             return os.sep.join(parts[i+1:])
    38     raise Exception('duh? %s' % path)
    43     raise Exception('duh? %s' % path)
    39 
    44 
    40 CUSTOM_CUBES_DIR = abspath(join(dirname(__file__), 'data', 'cubes'))
       
    41 
       
    42 
    45 
    43 class CubicWebConfigurationTC(testlib.BaseTestCase):
    46 class CubicWebConfigurationTC(testlib.BaseTestCase):
    44 
    47 
       
    48     @classmethod
       
    49     def setUpClass(cls):
       
    50         sys.path.append(cls.datapath('libpython'))
       
    51 
       
    52     @classmethod
       
    53     def tearDownClass(cls):
       
    54         sys.path.remove(cls.datapath('libpython'))
       
    55 
    45     def setUp(self):
    56     def setUp(self):
    46         cleanup_sys_modules([CUSTOM_CUBES_DIR, ApptestConfiguration.CUBES_DIR])
       
    47         self.config = ApptestConfiguration('data', __file__)
    57         self.config = ApptestConfiguration('data', __file__)
    48         self.config._cubes = ('email', 'file')
    58         self.config._cubes = ('email', 'file')
    49 
    59 
    50     def tearDown(self):
    60     def tearDown(self):
    51         ApptestConfiguration.CUBES_PATH = []
    61         ApptestConfiguration.CUBES_PATH = []
    52 
    62 
    53     def test_reorder_cubes(self):
    63     def test_reorder_cubes(self):
    54         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR]
       
    55         self.config.adjust_sys_path()
       
    56         # forge depends on email and file and comment
    64         # forge depends on email and file and comment
    57         # email depends on file
    65         # email depends on file
    58         self.assertEqual(self.config.reorder_cubes(['file', 'email', 'forge']),
    66         self.assertEqual(self.config.reorder_cubes(['file', 'email', 'forge']),
    59                           ('forge', 'email', 'file'))
    67                           ('forge', 'email', 'file'))
    60         self.assertEqual(self.config.reorder_cubes(['email', 'file', 'forge']),
    68         self.assertEqual(self.config.reorder_cubes(['email', 'file', 'forge']),
    67                           ('forge', 'email', 'file'))
    75                           ('forge', 'email', 'file'))
    68         self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file')),
    76         self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file')),
    69                           ('forge', 'email', 'file'))
    77                           ('forge', 'email', 'file'))
    70 
    78 
    71     def test_reorder_cubes_recommends(self):
    79     def test_reorder_cubes_recommends(self):
    72         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR]
    80         from cubicweb_comment import __pkginfo__ as comment_pkginfo
    73         self.config.adjust_sys_path()
    81         self._test_reorder_cubes_recommends(comment_pkginfo)
    74         from cubes.comment import __pkginfo__ as comment_pkginfo
    82 
       
    83     def _test_reorder_cubes_recommends(self, comment_pkginfo):
    75         comment_pkginfo.__recommends_cubes__ = {'file': None}
    84         comment_pkginfo.__recommends_cubes__ = {'file': None}
    76         try:
    85         try:
    77             # email recommends comment
    86             # email recommends comment
    78             # comment recommends file
    87             # comment recommends file
    79             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file', 'comment')),
    88             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file', 'comment')),
    86                               ('forge', 'email', 'comment', 'file'))
    95                               ('forge', 'email', 'comment', 'file'))
    87         finally:
    96         finally:
    88             comment_pkginfo.__recommends_cubes__ = {}
    97             comment_pkginfo.__recommends_cubes__ = {}
    89 
    98 
    90     def test_expand_cubes(self):
    99     def test_expand_cubes(self):
    91         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR]
       
    92         self.config.adjust_sys_path()
       
    93         self.assertEqual(self.config.expand_cubes(('email', 'comment')),
   100         self.assertEqual(self.config.expand_cubes(('email', 'comment')),
    94                           ['email', 'comment', 'file'])
   101                           ['email', 'comment', 'file'])
    95 
   102 
    96     def test_appobjects_path(self):
   103     def test_appobjects_path(self):
    97         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR]
       
    98         self.config.adjust_sys_path()
       
    99         path = [unabsolutize(p) for p in self.config.appobjects_path()]
   104         path = [unabsolutize(p) for p in self.config.appobjects_path()]
   100         self.assertEqual(path[0], 'entities')
   105         self.assertEqual(path[0], 'entities')
   101         self.assertCountEqual(path[1:4], ['web/views', 'sobjects', 'hooks'])
   106         self.assertCountEqual(path[1:4], ['web/views', 'sobjects', 'hooks'])
   102         self.assertEqual(path[4], 'file/entities')
   107         self.assertEqual(path[4], 'file/entities')
   103         self.assertCountEqual(path[5:7], ['file/views.py', 'file/hooks'])
   108         self.assertCountEqual(path[5:7],
       
   109                               ['file/views.py', 'file/hooks'])
   104         self.assertEqual(path[7], 'email/entities.py')
   110         self.assertEqual(path[7], 'email/entities.py')
   105         self.assertCountEqual(path[8:10], ['email/views', 'email/hooks.py'])
   111         self.assertCountEqual(path[8:10],
       
   112                               ['email/views', 'email/hooks.py'])
   106         self.assertEqual(path[10:], ['test/data/entities.py', 'test/data/views.py'])
   113         self.assertEqual(path[10:], ['test/data/entities.py', 'test/data/views.py'])
       
   114 
       
   115 
       
   116 class CubicWebConfigurationWithLegacyCubesTC(CubicWebConfigurationTC):
       
   117 
       
   118     @classmethod
       
   119     def setUpClass(cls):
       
   120         pass
       
   121 
       
   122     @classmethod
       
   123     def tearDownClass(cls):
       
   124         pass
       
   125 
       
   126     def setUp(self):
       
   127         self.custom_cubes_dir = self.datapath('legacy_cubes')
       
   128         cleanup_sys_modules([self.custom_cubes_dir, ApptestConfiguration.CUBES_DIR])
       
   129         super(CubicWebConfigurationWithLegacyCubesTC, self).setUp()
       
   130         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir]
       
   131         self.config.adjust_sys_path()
       
   132 
       
   133     def tearDown(self):
       
   134         ApptestConfiguration.CUBES_PATH = []
       
   135 
       
   136     def test_reorder_cubes_recommends(self):
       
   137         from cubes.comment import __pkginfo__ as comment_pkginfo
       
   138         self._test_reorder_cubes_recommends(comment_pkginfo)
   107 
   139 
   108     def test_cubes_path(self):
   140     def test_cubes_path(self):
   109         # make sure we don't import the email cube, but the stdlib email package
   141         # make sure we don't import the email cube, but the stdlib email package
   110         import email
   142         import email
   111         self.assertNotEqual(dirname(email.__file__), self.config.CUBES_DIR)
   143         self.assertNotEqual(dirname(email.__file__), self.config.CUBES_DIR)
   112         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR]
   144         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir]
   113         self.assertEqual(self.config.cubes_search_path(),
   145         self.assertEqual(self.config.cubes_search_path(),
   114                           [CUSTOM_CUBES_DIR, self.config.CUBES_DIR])
   146                           [self.custom_cubes_dir, self.config.CUBES_DIR])
   115         self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR,
   147         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir,
   116                                             self.config.CUBES_DIR, 'unexistant']
   148                                             self.config.CUBES_DIR, 'unexistant']
   117         # filter out unexistant and duplicates
   149         # filter out unexistant and duplicates
   118         self.assertEqual(self.config.cubes_search_path(),
   150         self.assertEqual(self.config.cubes_search_path(),
   119                           [CUSTOM_CUBES_DIR,
   151                           [self.custom_cubes_dir,
   120                            self.config.CUBES_DIR])
   152                            self.config.CUBES_DIR])
   121         self.assertIn('mycube', self.config.available_cubes())
   153         self.assertIn('mycube', self.config.available_cubes())
   122         # test cubes python path
   154         # test cubes python path
   123         self.config.adjust_sys_path()
   155         self.config.adjust_sys_path()
   124         import cubes
   156         import cubes
   125         self.assertEqual(cubes.__path__, self.config.cubes_search_path())
   157         self.assertEqual(cubes.__path__, self.config.cubes_search_path())
   126         # this import should succeed once path is adjusted
   158         # this import should succeed once path is adjusted
   127         from cubes import mycube
   159         from cubes import mycube
   128         self.assertEqual(mycube.__path__, [join(CUSTOM_CUBES_DIR, 'mycube')])
   160         self.assertEqual(mycube.__path__, [join(self.custom_cubes_dir, 'mycube')])
   129         # file cube should be overriden by the one found in data/cubes
   161         # file cube should be overriden by the one found in data/cubes
   130         sys.modules.pop('cubes.file', None)
   162         sys.modules.pop('cubes.file', None)
   131         del cubes.file
   163         if PY3:
       
   164             del cubes.file
   132         from cubes import file
   165         from cubes import file
   133         self.assertEqual(file.__path__, [join(CUSTOM_CUBES_DIR, 'file')])
   166         self.assertEqual(file.__path__, [join(self.custom_cubes_dir, 'file')])
   134 
   167 
   135 
   168 
   136 class FindPrefixTC(unittest.TestCase):
   169 class FindPrefixTC(unittest.TestCase):
   137     def make_dirs(self, *args):
   170     def make_dirs(self, *args):
   138         path = join(tempfile.tempdir, *args)
   171         path = join(tempfile.tempdir, *args)