cubicweb/test/unittest_cwconfig.py
branch3.25
changeset 12119 c4ed3f6463f6
parent 12118 d7947c3c0eeb
child 12165 127f7da447a4
equal deleted inserted replaced
12118:d7947c3c0eeb 12119:c4ed3f6463f6
    21 import compileall
    21 import compileall
    22 import functools
    22 import functools
    23 import sys
    23 import sys
    24 import os
    24 import os
    25 import pkgutil
    25 import pkgutil
    26 from os.path import dirname, join, abspath
    26 from os.path import dirname, join
    27 from pkg_resources import EntryPoint, Distribution
    27 from pkg_resources import EntryPoint, Distribution
    28 import unittest
    28 import unittest
    29 
    29 
    30 from mock import patch
    30 from mock import patch
    31 from six import PY3
    31 from six import PY3
    32 
    32 
    33 from logilab.common.modutils import cleanup_sys_modules
    33 from logilab.common.modutils import cleanup_sys_modules
    34 from logilab.common.changelog import Version
       
    35 
    34 
    36 from cubicweb.devtools import ApptestConfiguration
    35 from cubicweb.devtools import ApptestConfiguration
    37 from cubicweb.devtools.testlib import BaseTestCase, TemporaryDirectory
    36 from cubicweb.devtools.testlib import BaseTestCase, TemporaryDirectory
    38 from cubicweb.cwconfig import (
    37 from cubicweb.cwconfig import (
    39     CubicWebConfiguration, _find_prefix, _expand_modname)
    38     CubicWebConfiguration, _find_prefix, _expand_modname)
    41 
    40 
    42 def unabsolutize(path):
    41 def unabsolutize(path):
    43     parts = path.split(os.sep)
    42     parts = path.split(os.sep)
    44     for i, part in reversed(tuple(enumerate(parts))):
    43     for i, part in reversed(tuple(enumerate(parts))):
    45         if part.startswith('cubicweb_'):
    44         if part.startswith('cubicweb_'):
    46             return os.sep.join([part[len('cubicweb_'):]] + parts[i+1:])
    45             return os.sep.join([part[len('cubicweb_'):]] + parts[i + 1:])
    47         if part.startswith('cubicweb') or part == 'legacy_cubes':
    46         if part.startswith('cubicweb') or part == 'legacy_cubes':
    48             return os.sep.join(parts[i+1:])
    47             return os.sep.join(parts[i + 1:])
    49     raise Exception('duh? %s' % path)
    48     raise Exception('duh? %s' % path)
    50 
    49 
    51 
    50 
    52 def templibdir(func):
    51 def templibdir(func):
    53     """create a temporary directory and insert it in sys.path"""
    52     """create a temporary directory and insert it in sys.path"""
   142 
   141 
   143     def test_reorder_cubes(self):
   142     def test_reorder_cubes(self):
   144         # forge depends on email and file and comment
   143         # forge depends on email and file and comment
   145         # email depends on file
   144         # email depends on file
   146         self.assertEqual(self.config.reorder_cubes(['file', 'email', 'forge']),
   145         self.assertEqual(self.config.reorder_cubes(['file', 'email', 'forge']),
   147                           ('forge', 'email', 'file'))
   146                          ('forge', 'email', 'file'))
   148         self.assertEqual(self.config.reorder_cubes(['email', 'file', 'forge']),
   147         self.assertEqual(self.config.reorder_cubes(['email', 'file', 'forge']),
   149                           ('forge', 'email', 'file'))
   148                          ('forge', 'email', 'file'))
   150         self.assertEqual(self.config.reorder_cubes(['email', 'forge', 'file']),
   149         self.assertEqual(self.config.reorder_cubes(['email', 'forge', 'file']),
   151                           ('forge', 'email', 'file'))
   150                          ('forge', 'email', 'file'))
   152         self.assertEqual(self.config.reorder_cubes(['file', 'forge', 'email']),
   151         self.assertEqual(self.config.reorder_cubes(['file', 'forge', 'email']),
   153                           ('forge', 'email', 'file'))
   152                          ('forge', 'email', 'file'))
   154         self.assertEqual(self.config.reorder_cubes(['forge', 'file', 'email']),
   153         self.assertEqual(self.config.reorder_cubes(['forge', 'file', 'email']),
   155                           ('forge', 'email', 'file'))
   154                          ('forge', 'email', 'file'))
   156         self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file')),
   155         self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file')),
   157                           ('forge', 'email', 'file'))
   156                          ('forge', 'email', 'file'))
   158 
   157 
   159     def test_reorder_cubes_recommends(self):
   158     def test_reorder_cubes_recommends(self):
   160         from cubicweb_comment import __pkginfo__ as comment_pkginfo
   159         from cubicweb_comment import __pkginfo__ as comment_pkginfo
   161         self._test_reorder_cubes_recommends(comment_pkginfo)
   160         self._test_reorder_cubes_recommends(comment_pkginfo)
   162 
   161 
   164         comment_pkginfo.__recommends_cubes__ = {'file': None}
   163         comment_pkginfo.__recommends_cubes__ = {'file': None}
   165         try:
   164         try:
   166             # email recommends comment
   165             # email recommends comment
   167             # comment recommends file
   166             # comment recommends file
   168             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file', 'comment')),
   167             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file', 'comment')),
   169                               ('forge', 'email', 'comment', 'file'))
   168                              ('forge', 'email', 'comment', 'file'))
   170             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'comment', 'file')),
   169             self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'comment', 'file')),
   171                               ('forge', 'email', 'comment', 'file'))
   170                              ('forge', 'email', 'comment', 'file'))
   172             self.assertEqual(self.config.reorder_cubes(('forge', 'comment', 'email', 'file')),
   171             self.assertEqual(self.config.reorder_cubes(('forge', 'comment', 'email', 'file')),
   173                               ('forge', 'email', 'comment', 'file'))
   172                              ('forge', 'email', 'comment', 'file'))
   174             self.assertEqual(self.config.reorder_cubes(('comment', 'forge', 'email', 'file')),
   173             self.assertEqual(self.config.reorder_cubes(('comment', 'forge', 'email', 'file')),
   175                               ('forge', 'email', 'comment', 'file'))
   174                              ('forge', 'email', 'comment', 'file'))
   176         finally:
   175         finally:
   177             comment_pkginfo.__recommends_cubes__ = {}
   176             comment_pkginfo.__recommends_cubes__ = {}
   178 
   177 
   179     def test_expand_cubes(self):
   178     def test_expand_cubes(self):
   180         self.assertEqual(self.config.expand_cubes(('email', 'comment')),
   179         self.assertEqual(self.config.expand_cubes(('email', 'comment')),
   181                           ['email', 'comment', 'file'])
   180                          ['email', 'comment', 'file'])
   182 
       
   183 
   181 
   184     def test_init_cubes_ignore_pyramid_cube(self):
   182     def test_init_cubes_ignore_pyramid_cube(self):
   185         warning_msg = 'cubicweb-pyramid got integrated into CubicWeb'
   183         warning_msg = 'cubicweb-pyramid got integrated into CubicWeb'
   186         with self.assertLogs('cubicweb.configuration', level='WARNING') as cm:
   184         with self.assertLogs('cubicweb.configuration', level='WARNING') as cm:
   187             self.config.init_cubes(['pyramid', 'card'])
   185             self.config.init_cubes(['pyramid', 'card'])
   234         # make sure we don't import the email cube, but the stdlib email package
   232         # make sure we don't import the email cube, but the stdlib email package
   235         import email
   233         import email
   236         self.assertNotEqual(dirname(email.__file__), self.config.CUBES_DIR)
   234         self.assertNotEqual(dirname(email.__file__), self.config.CUBES_DIR)
   237         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir]
   235         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir]
   238         self.assertEqual(self.config.cubes_search_path(),
   236         self.assertEqual(self.config.cubes_search_path(),
   239                           [self.custom_cubes_dir, self.config.CUBES_DIR])
   237                          [self.custom_cubes_dir, self.config.CUBES_DIR])
   240         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir,
   238         self.config.__class__.CUBES_PATH = [self.custom_cubes_dir,
   241                                             self.config.CUBES_DIR, 'unexistant']
   239                                             self.config.CUBES_DIR, 'unexistant']
   242         # filter out unexistant and duplicates
   240         # filter out unexistant and duplicates
   243         self.assertEqual(self.config.cubes_search_path(),
   241         self.assertEqual(self.config.cubes_search_path(),
   244                           [self.custom_cubes_dir,
   242                          [self.custom_cubes_dir,
   245                            self.config.CUBES_DIR])
   243                           self.config.CUBES_DIR])
   246         self.assertIn('mycube', self.config.available_cubes())
   244         self.assertIn('mycube', self.config.available_cubes())
   247         # test cubes python path
   245         # test cubes python path
   248         self.config.adjust_sys_path()
   246         self.config.adjust_sys_path()
   249         import cubes
   247         import cubes
   250         self.assertEqual(cubes.__path__, self.config.cubes_search_path())
   248         self.assertEqual(cubes.__path__, self.config.cubes_search_path())
   364             self.assertEqual(_find_prefix(file_path), prefix)
   362             self.assertEqual(_find_prefix(file_path), prefix)
   365 
   363 
   366     def test_upper_candidate_prefix(self):
   364     def test_upper_candidate_prefix(self):
   367         with TemporaryDirectory() as prefix:
   365         with TemporaryDirectory() as prefix:
   368             self.make_dirs(prefix, 'share', 'cubicweb')
   366             self.make_dirs(prefix, 'share', 'cubicweb')
   369             self.make_dirs(prefix, 'bell', 'bob',  'share', 'cubicweb')
   367             self.make_dirs(prefix, 'bell', 'bob', 'share', 'cubicweb')
   370             file_path = self.make_file(prefix, 'bell', 'toto.py')
   368             file_path = self.make_file(prefix, 'bell', 'toto.py')
   371             self.assertEqual(_find_prefix(file_path), prefix)
   369             self.assertEqual(_find_prefix(file_path), prefix)
   372 
   370 
   373     def test_no_prefix(self):
   371     def test_no_prefix(self):
   374         with TemporaryDirectory() as prefix:
   372         with TemporaryDirectory() as prefix: