1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 unit tests for module modutils (module manipulation utilities)
15 """
16
17 import sys
18 try:
19 __file__
20 except NameError:
21 __file__ = sys.argv[0]
22
23 from logilab.common.testlib import TestCase as TLTestCase, unittest_main
24 from logilab.common import modutils
25 from logilab.common.compat import set
26
27 from os import path, getcwd
28 from logilab import common
29 from logilab.common import tree
30
31 sys.path.insert(0, path.dirname(__file__))
32 DATADIR = path.join(path.dirname(__file__), 'data')
33
34
37 super(TestCase,self).setUp()
38 self.__common_in_path = common.__path__[0] in sys.path
39 if self.__common_in_path:
40 sys.path.remove(common.__path__[0])
41
46
49 mtype, mfile = _module_file('mypypa', [path.join(DATADIR, 'MyPyPa-0.1.0-py2.5.zip')])
50 self.assertEquals(mtype, modutils.ZIPFILE)
51 self.assertEquals(mfile, '')
52
54 mtype, mfile = _module_file('mypypa', [path.join(DATADIR, 'MyPyPa-0.1.0-py2.5.egg')])
55 self.assertEquals(mtype, modutils.ZIPFILE)
56 self.assertEquals(mfile, '')
57
58
59
61 """ load a python module from it's name """
62
65
68
72
73
75 """given a dotted name return the module part of the name"""
76
80
82 self.assertEqual(modutils.get_module_part('logilab.common.modutils.get_module_part'),
83 'logilab.common.modutils')
84
88
92
96
97
99 """ given an absolute file path return the python module's path as a list """
100
104
106 self.assertEqual(modutils.modpath_from_file('unittest_modutils.py',
107 {getcwd(): 'arbitrary.pkg'}),
108 ['arbitrary', 'pkg', 'unittest_modutils'])
109
112
113
115 """given a mod path (i.e. splited module / package name), return the
116 corresponding file, giving priority to source file over precompiled file
117 if it exists"""
118
122
127
129 try:
130
131 from xml.dom import ext
132 except ImportError:
133 pass
134 else:
135 self.assertEqual(path.realpath(modutils.file_from_modpath(['xml', 'dom', 'ext']).replace('.pyc', '.py')),
136 path.realpath(ext.__file__.replace('.pyc', '.py')))
137
141
144
154
156 """
157 return true if the module may be considered as a module from the standard
158 library
159 """
160
163
166
169
172
175
179
180
193
195
197 """given a directory return a list of all available python modules, even
198 in subdirectories
199 """
200 import data.find_test as data
201 mod_path = ("data", 'find_test')
202 modules = modutils.get_modules(path.join(*mod_path), data.__path__[0])
203 modules.sort()
204 self.assertSetEquals(set(modules),
205 set([ '.'.join(mod_path + (mod, )) for mod in 'module', 'module2',
206 'noendingnewline', 'nonregr']))
207
208
210
212 """given a directory return a list of all available python module's files, even
213 in subdirectories
214 """
215 import data
216 modules = modutils.get_module_files(path.join(DATADIR,'find_test'), data.__path__[0])
217 modules.sort()
218 self.assertEqual(modules,
219 [path.join(DATADIR, 'find_test', x) for x in ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py']])
220
230
231 from logilab.common.testlib import DocTest
235 del DocTest
236
237
238 if __name__ == '__main__':
239 unittest_main()
240