cubicweb/test/unittest_cwconfig.py
changeset 11744 a6dc650bc230
parent 11743 48d70d143dc1
child 11859 2e1a1ebd4730
child 11899 bf6106b91633
equal deleted inserted replaced
11743:48d70d143dc1 11744:a6dc650bc230
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """cubicweb.cwconfig unit tests"""
    18 """cubicweb.cwconfig unit tests"""
    19 
    19 
    20 import sys
    20 import sys
    21 import os
    21 import os
    22 import tempfile
       
    23 from os.path import dirname, join, abspath
    22 from os.path import dirname, join, abspath
    24 from pkg_resources import EntryPoint, Distribution
    23 from pkg_resources import EntryPoint, Distribution
    25 import unittest
    24 import unittest
    26 
    25 
    27 from mock import patch
    26 from mock import patch
    28 from six import PY3
    27 from six import PY3
    29 
    28 
    30 from logilab.common.modutils import cleanup_sys_modules
    29 from logilab.common.modutils import cleanup_sys_modules
    31 from logilab.common.testlib import with_tempdir
       
    32 from logilab.common.changelog import Version
    30 from logilab.common.changelog import Version
    33 
    31 
    34 from cubicweb.devtools import ApptestConfiguration, testlib
    32 from cubicweb.devtools import ApptestConfiguration
       
    33 from cubicweb.devtools.testlib import BaseTestCase, TemporaryDirectory
    35 from cubicweb.cwconfig import _find_prefix
    34 from cubicweb.cwconfig import _find_prefix
    36 
    35 
    37 
    36 
    38 def unabsolutize(path):
    37 def unabsolutize(path):
    39     parts = path.split(os.sep)
    38     parts = path.split(os.sep)
    43         if part.startswith('cubicweb') or part == 'legacy_cubes':
    42         if part.startswith('cubicweb') or part == 'legacy_cubes':
    44             return os.sep.join(parts[i+1:])
    43             return os.sep.join(parts[i+1:])
    45     raise Exception('duh? %s' % path)
    44     raise Exception('duh? %s' % path)
    46 
    45 
    47 
    46 
    48 class CubicWebConfigurationTC(testlib.BaseTestCase):
    47 class CubicWebConfigurationTC(BaseTestCase):
    49 
    48 
    50     @classmethod
    49     @classmethod
    51     def setUpClass(cls):
    50     def setUpClass(cls):
    52         sys.path.append(cls.datapath('libpython'))
    51         sys.path.append(cls.datapath('libpython'))
    53 
    52 
   218         finally:
   217         finally:
   219             del os.environ['CW_BASE_URL']
   218             del os.environ['CW_BASE_URL']
   220 
   219 
   221 
   220 
   222 class FindPrefixTC(unittest.TestCase):
   221 class FindPrefixTC(unittest.TestCase):
   223     def make_dirs(self, *args):
   222 
   224         path = join(tempfile.tempdir, *args)
   223     def make_dirs(self, basedir, *args):
       
   224         path = join(basedir, *args)
   225         if not os.path.exists(path):
   225         if not os.path.exists(path):
   226             os.makedirs(path)
   226             os.makedirs(path)
   227         return path
   227         return path
   228 
   228 
   229     def make_file(self, *args):
   229     def make_file(self, basedir, *args):
   230         self.make_dirs(*args[: -1])
   230         self.make_dirs(basedir, *args[:-1])
   231         file_path = join(tempfile.tempdir, *args)
   231         file_path = join(basedir, *args)
   232         with open(file_path, 'w') as f:
   232         with open(file_path, 'w') as f:
   233             f.write('""" None """')
   233             f.write('""" None """')
   234         return file_path
   234         return file_path
   235 
   235 
   236     @with_tempdir
       
   237     def test_samedir(self):
   236     def test_samedir(self):
   238         prefix = tempfile.tempdir
   237         with TemporaryDirectory() as prefix:
   239         self.make_dirs('share', 'cubicweb')
   238             self.make_dirs(prefix, 'share', 'cubicweb')
   240         self.assertEqual(_find_prefix(prefix), prefix)
   239             self.assertEqual(_find_prefix(prefix), prefix)
   241 
   240 
   242     @with_tempdir
       
   243     def test_samedir_filepath(self):
   241     def test_samedir_filepath(self):
   244         prefix = tempfile.tempdir
   242         with TemporaryDirectory() as prefix:
   245         self.make_dirs('share', 'cubicweb')
   243             self.make_dirs(prefix, 'share', 'cubicweb')
   246         file_path = self.make_file('bob.py')
   244             file_path = self.make_file(prefix, 'bob.py')
   247         self.assertEqual(_find_prefix(file_path), prefix)
   245             self.assertEqual(_find_prefix(file_path), prefix)
   248 
   246 
   249     @with_tempdir
       
   250     def test_dir_inside_prefix(self):
   247     def test_dir_inside_prefix(self):
   251         prefix = tempfile.tempdir
   248         with TemporaryDirectory() as prefix:
   252         self.make_dirs('share', 'cubicweb')
   249             self.make_dirs(prefix, 'share', 'cubicweb')
   253         dir_path = self.make_dirs('bob')
   250             dir_path = self.make_dirs(prefix, 'bob')
   254         self.assertEqual(_find_prefix(dir_path), prefix)
   251             self.assertEqual(_find_prefix(dir_path), prefix)
   255 
   252 
   256     @with_tempdir
       
   257     def test_file_in_dir_inside_prefix(self):
   253     def test_file_in_dir_inside_prefix(self):
   258         prefix = tempfile.tempdir
   254         with TemporaryDirectory() as prefix:
   259         self.make_dirs('share', 'cubicweb')
   255             self.make_dirs(prefix, 'share', 'cubicweb')
   260         file_path = self.make_file('bob', 'toto.py')
   256             file_path = self.make_file(prefix, 'bob', 'toto.py')
   261         self.assertEqual(_find_prefix(file_path), prefix)
   257             self.assertEqual(_find_prefix(file_path), prefix)
   262 
   258 
   263     @with_tempdir
       
   264     def test_file_in_deeper_dir_inside_prefix(self):
   259     def test_file_in_deeper_dir_inside_prefix(self):
   265         prefix = tempfile.tempdir
   260         with TemporaryDirectory() as prefix:
   266         self.make_dirs('share', 'cubicweb')
   261             self.make_dirs(prefix, 'share', 'cubicweb')
   267         file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
   262             file_path = self.make_file(prefix, 'bob', 'pyves', 'alain',
   268         self.assertEqual(_find_prefix(file_path), prefix)
   263                                        'adim', 'syt', 'toto.py')
   269 
   264             self.assertEqual(_find_prefix(file_path), prefix)
   270     @with_tempdir
   265 
   271     def test_multiple_candidate_prefix(self):
   266     def test_multiple_candidate_prefix(self):
   272         self.make_dirs('share', 'cubicweb')
   267         with TemporaryDirectory() as tempdir:
   273         prefix = self.make_dirs('bob')
   268             self.make_dirs(tempdir, 'share', 'cubicweb')
   274         self.make_dirs('bob', 'share', 'cubicweb')
   269             prefix = self.make_dirs(tempdir, 'bob')
   275         file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
   270             self.make_dirs(prefix, 'share', 'cubicweb')
   276         self.assertEqual(_find_prefix(file_path), prefix)
   271             file_path = self.make_file(prefix, 'pyves', 'alain',
   277 
   272                                        'adim', 'syt', 'toto.py')
   278     @with_tempdir
   273             self.assertEqual(_find_prefix(file_path), prefix)
       
   274 
   279     def test_sister_candidate_prefix(self):
   275     def test_sister_candidate_prefix(self):
   280         prefix = tempfile.tempdir
   276         with TemporaryDirectory() as prefix:
   281         self.make_dirs('share', 'cubicweb')
   277             self.make_dirs(prefix, 'share', 'cubicweb')
   282         self.make_dirs('bob', 'share', 'cubicweb')
   278             self.make_dirs(prefix, 'bob', 'share', 'cubicweb')
   283         file_path = self.make_file('bell', 'toto.py')
   279             file_path = self.make_file(prefix, 'bell', 'toto.py')
   284         self.assertEqual(_find_prefix(file_path), prefix)
   280             self.assertEqual(_find_prefix(file_path), prefix)
   285 
   281 
   286     @with_tempdir
       
   287     def test_multiple_parent_candidate_prefix(self):
   282     def test_multiple_parent_candidate_prefix(self):
   288         self.make_dirs('share', 'cubicweb')
   283         with TemporaryDirectory() as tempdir:
   289         prefix = self.make_dirs('share', 'cubicweb', 'bob')
   284             self.make_dirs(tempdir, 'share', 'cubicweb')
   290         self.make_dirs('share', 'cubicweb', 'bob', 'share', 'cubicweb')
   285             prefix = self.make_dirs(tempdir, 'share', 'cubicweb', 'bob')
   291         file_path = self.make_file('share', 'cubicweb', 'bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py')
   286             self.make_dirs(tempdir, 'share', 'cubicweb', 'bob', 'share',
   292         self.assertEqual(_find_prefix(file_path), prefix)
   287                            'cubicweb')
   293 
   288             file_path = self.make_file(tempdir, 'share', 'cubicweb', 'bob',
   294     @with_tempdir
   289                                        'pyves', 'alain', 'adim', 'syt',
       
   290                                        'toto.py')
       
   291             self.assertEqual(_find_prefix(file_path), prefix)
       
   292 
   295     def test_upper_candidate_prefix(self):
   293     def test_upper_candidate_prefix(self):
   296         prefix = tempfile.tempdir
   294         with TemporaryDirectory() as prefix:
   297         self.make_dirs('share', 'cubicweb')
   295             self.make_dirs(prefix, 'share', 'cubicweb')
   298         self.make_dirs('bell','bob',  'share', 'cubicweb')
   296             self.make_dirs(prefix, 'bell', 'bob',  'share', 'cubicweb')
   299         file_path = self.make_file('bell', 'toto.py')
   297             file_path = self.make_file(prefix, 'bell', 'toto.py')
   300         self.assertEqual(_find_prefix(file_path), prefix)
   298             self.assertEqual(_find_prefix(file_path), prefix)
   301 
   299 
   302     @with_tempdir
       
   303     def test_no_prefix(self):
   300     def test_no_prefix(self):
   304         prefix = tempfile.tempdir
   301         with TemporaryDirectory() as prefix:
   305         self.assertEqual(_find_prefix(prefix), sys.prefix)
   302             self.assertEqual(_find_prefix(prefix), sys.prefix)
   306 
   303 
   307     @with_tempdir
       
   308     def test_virtualenv(self):
   304     def test_virtualenv(self):
   309         venv = os.environ.get('VIRTUAL_ENV')
   305         venv = os.environ.get('VIRTUAL_ENV')
   310         try:
   306         try:
   311             prefix = os.environ['VIRTUAL_ENV'] = tempfile.tempdir
   307             with TemporaryDirectory() as prefix:
   312             self.make_dirs('share', 'cubicweb')
   308                 os.environ['VIRTUAL_ENV'] = prefix
   313             self.assertEqual(_find_prefix(), prefix)
   309                 self.make_dirs(prefix, 'share', 'cubicweb')
       
   310                 self.assertEqual(_find_prefix(), prefix)
   314         finally:
   311         finally:
   315             if venv:
   312             if venv:
   316                 os.environ['VIRTUAL_ENV'] = venv
   313                 os.environ['VIRTUAL_ENV'] = venv
   317 
   314 
   318 
   315