|
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """cubicweb.cwconfig unit tests""" |
|
19 |
|
20 import sys |
|
21 import os |
|
22 import tempfile |
|
23 from os.path import dirname, join, abspath |
|
24 |
|
25 from logilab.common.modutils import cleanup_sys_modules |
|
26 from logilab.common.testlib import (TestCase, unittest_main, |
|
27 with_tempdir) |
|
28 from logilab.common.changelog import Version |
|
29 |
|
30 from cubicweb.devtools import ApptestConfiguration |
|
31 from cubicweb.cwconfig import _find_prefix |
|
32 |
|
33 def unabsolutize(path): |
|
34 parts = path.split(os.sep) |
|
35 for i, part in reversed(tuple(enumerate(parts))): |
|
36 if part.startswith('cubicweb') or part == 'cubes': |
|
37 return '/'.join(parts[i+1:]) |
|
38 raise Exception('duh? %s' % path) |
|
39 |
|
40 CUSTOM_CUBES_DIR = abspath(join(dirname(__file__), 'data', 'cubes')) |
|
41 |
|
42 |
|
43 class CubicWebConfigurationTC(TestCase): |
|
44 def setUp(self): |
|
45 cleanup_sys_modules([CUSTOM_CUBES_DIR, ApptestConfiguration.CUBES_DIR]) |
|
46 self.config = ApptestConfiguration('data', apphome=self.datadir) |
|
47 self.config._cubes = ('email', 'file') |
|
48 |
|
49 def tearDown(self): |
|
50 ApptestConfiguration.CUBES_PATH = [] |
|
51 |
|
52 def test_reorder_cubes(self): |
|
53 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR] |
|
54 self.config.adjust_sys_path() |
|
55 # forge depends on email and file and comment |
|
56 # email depends on file |
|
57 self.assertEqual(self.config.reorder_cubes(['file', 'email', 'forge']), |
|
58 ('forge', 'email', 'file')) |
|
59 self.assertEqual(self.config.reorder_cubes(['email', 'file', 'forge']), |
|
60 ('forge', 'email', 'file')) |
|
61 self.assertEqual(self.config.reorder_cubes(['email', 'forge', 'file']), |
|
62 ('forge', 'email', 'file')) |
|
63 self.assertEqual(self.config.reorder_cubes(['file', 'forge', 'email']), |
|
64 ('forge', 'email', 'file')) |
|
65 self.assertEqual(self.config.reorder_cubes(['forge', 'file', 'email']), |
|
66 ('forge', 'email', 'file')) |
|
67 self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file')), |
|
68 ('forge', 'email', 'file')) |
|
69 |
|
70 def test_reorder_cubes_recommends(self): |
|
71 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR] |
|
72 self.config.adjust_sys_path() |
|
73 from cubes.comment import __pkginfo__ as comment_pkginfo |
|
74 comment_pkginfo.__recommends_cubes__ = {'file': None} |
|
75 try: |
|
76 # email recommends comment |
|
77 # comment recommends file |
|
78 self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'file', 'comment')), |
|
79 ('forge', 'email', 'comment', 'file')) |
|
80 self.assertEqual(self.config.reorder_cubes(('forge', 'email', 'comment', 'file')), |
|
81 ('forge', 'email', 'comment', 'file')) |
|
82 self.assertEqual(self.config.reorder_cubes(('forge', 'comment', 'email', 'file')), |
|
83 ('forge', 'email', 'comment', 'file')) |
|
84 self.assertEqual(self.config.reorder_cubes(('comment', 'forge', 'email', 'file')), |
|
85 ('forge', 'email', 'comment', 'file')) |
|
86 finally: |
|
87 comment_pkginfo.__recommends_cubes__ = {} |
|
88 |
|
89 def test_expand_cubes(self): |
|
90 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR] |
|
91 self.config.adjust_sys_path() |
|
92 self.assertEqual(self.config.expand_cubes(('email', 'comment')), |
|
93 ['email', 'comment', 'file']) |
|
94 |
|
95 def test_appobjects_path(self): |
|
96 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR] |
|
97 self.config.adjust_sys_path() |
|
98 path = [unabsolutize(p) for p in self.config.appobjects_path()] |
|
99 self.assertEqual(path[0], 'entities') |
|
100 self.assertCountEqual(path[1:4], ['web/views', 'sobjects', 'hooks']) |
|
101 self.assertEqual(path[4], 'file/entities') |
|
102 self.assertCountEqual(path[5:7], ['file/views.py', 'file/hooks']) |
|
103 self.assertEqual(path[7], 'email/entities.py') |
|
104 self.assertCountEqual(path[8:10], ['email/views', 'email/hooks.py']) |
|
105 self.assertEqual(path[10:], ['test/data/entities.py', 'test/data/views.py']) |
|
106 |
|
107 def test_cubes_path(self): |
|
108 # make sure we don't import the email cube, but the stdlib email package |
|
109 import email |
|
110 self.assertNotEqual(dirname(email.__file__), self.config.CUBES_DIR) |
|
111 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR] |
|
112 self.assertEqual(self.config.cubes_search_path(), |
|
113 [CUSTOM_CUBES_DIR, self.config.CUBES_DIR]) |
|
114 self.config.__class__.CUBES_PATH = [CUSTOM_CUBES_DIR, |
|
115 self.config.CUBES_DIR, 'unexistant'] |
|
116 # filter out unexistant and duplicates |
|
117 self.assertEqual(self.config.cubes_search_path(), |
|
118 [CUSTOM_CUBES_DIR, |
|
119 self.config.CUBES_DIR]) |
|
120 self.assertIn('mycube', self.config.available_cubes()) |
|
121 # test cubes python path |
|
122 self.config.adjust_sys_path() |
|
123 import cubes |
|
124 self.assertEqual(cubes.__path__, self.config.cubes_search_path()) |
|
125 # this import should succeed once path is adjusted |
|
126 from cubes import mycube |
|
127 self.assertEqual(mycube.__path__, [join(CUSTOM_CUBES_DIR, 'mycube')]) |
|
128 # file cube should be overriden by the one found in data/cubes |
|
129 sys.modules.pop('cubes.file', None) |
|
130 del cubes.file |
|
131 from cubes import file |
|
132 self.assertEqual(file.__path__, [join(CUSTOM_CUBES_DIR, 'file')]) |
|
133 |
|
134 |
|
135 class FindPrefixTC(TestCase): |
|
136 def make_dirs(self, *args): |
|
137 path = join(tempfile.tempdir, *args) |
|
138 if not os.path.exists(path): |
|
139 os.makedirs(path) |
|
140 return path |
|
141 |
|
142 def make_file(self, *args): |
|
143 self.make_dirs(*args[: -1]) |
|
144 file_path = join(tempfile.tempdir, *args) |
|
145 file_obj = open(file_path, 'w') |
|
146 file_obj.write('""" None """') |
|
147 file_obj.close() |
|
148 return file_path |
|
149 |
|
150 @with_tempdir |
|
151 def test_samedir(self): |
|
152 prefix = tempfile.tempdir |
|
153 self.make_dirs('share', 'cubicweb') |
|
154 self.assertEqual(_find_prefix(prefix), prefix) |
|
155 |
|
156 @with_tempdir |
|
157 def test_samedir_filepath(self): |
|
158 prefix = tempfile.tempdir |
|
159 self.make_dirs('share', 'cubicweb') |
|
160 file_path = self.make_file('bob.py') |
|
161 self.assertEqual(_find_prefix(file_path), prefix) |
|
162 |
|
163 @with_tempdir |
|
164 def test_dir_inside_prefix(self): |
|
165 prefix = tempfile.tempdir |
|
166 self.make_dirs('share', 'cubicweb') |
|
167 dir_path = self.make_dirs('bob') |
|
168 self.assertEqual(_find_prefix(dir_path), prefix) |
|
169 |
|
170 @with_tempdir |
|
171 def test_file_in_dir_inside_prefix(self): |
|
172 prefix = tempfile.tempdir |
|
173 self.make_dirs('share', 'cubicweb') |
|
174 file_path = self.make_file('bob', 'toto.py') |
|
175 self.assertEqual(_find_prefix(file_path), prefix) |
|
176 |
|
177 @with_tempdir |
|
178 def test_file_in_deeper_dir_inside_prefix(self): |
|
179 prefix = tempfile.tempdir |
|
180 self.make_dirs('share', 'cubicweb') |
|
181 file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py') |
|
182 self.assertEqual(_find_prefix(file_path), prefix) |
|
183 |
|
184 @with_tempdir |
|
185 def test_multiple_candidate_prefix(self): |
|
186 self.make_dirs('share', 'cubicweb') |
|
187 prefix = self.make_dirs('bob') |
|
188 self.make_dirs('bob', 'share', 'cubicweb') |
|
189 file_path = self.make_file('bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py') |
|
190 self.assertEqual(_find_prefix(file_path), prefix) |
|
191 |
|
192 @with_tempdir |
|
193 def test_sister_candidate_prefix(self): |
|
194 prefix = tempfile.tempdir |
|
195 self.make_dirs('share', 'cubicweb') |
|
196 self.make_dirs('bob', 'share', 'cubicweb') |
|
197 file_path = self.make_file('bell', 'toto.py') |
|
198 self.assertEqual(_find_prefix(file_path), prefix) |
|
199 |
|
200 @with_tempdir |
|
201 def test_multiple_parent_candidate_prefix(self): |
|
202 self.make_dirs('share', 'cubicweb') |
|
203 prefix = self.make_dirs('share', 'cubicweb', 'bob') |
|
204 self.make_dirs('share', 'cubicweb', 'bob', 'share', 'cubicweb') |
|
205 file_path = self.make_file('share', 'cubicweb', 'bob', 'pyves', 'alain', 'adim', 'syt', 'toto.py') |
|
206 self.assertEqual(_find_prefix(file_path), prefix) |
|
207 |
|
208 @with_tempdir |
|
209 def test_upper_candidate_prefix(self): |
|
210 prefix = tempfile.tempdir |
|
211 self.make_dirs('share', 'cubicweb') |
|
212 self.make_dirs('bell','bob', 'share', 'cubicweb') |
|
213 file_path = self.make_file('bell', 'toto.py') |
|
214 self.assertEqual(_find_prefix(file_path), prefix) |
|
215 |
|
216 @with_tempdir |
|
217 def test_no_prefix(self): |
|
218 prefix = tempfile.tempdir |
|
219 self.assertEqual(_find_prefix(prefix), sys.prefix) |
|
220 |
|
221 if __name__ == '__main__': |
|
222 unittest_main() |