4
from numpy.testing import *
6
from weave import catalog
10
from weave_test_utils import *
14
class test_default_dir(NumpyTestCase):
15
def check_is_writable(self):
16
path = catalog.default_dir()
17
name = os.path.join(path,'dummy_catalog')
18
test_file = open(name,'w')
20
test_file.write('making sure default location is writable\n')
25
class test_os_dependent_catalog_name(NumpyTestCase):
28
class test_catalog_path(NumpyTestCase):
29
def check_default(self):
30
in_path = catalog.default_dir()
31
path = catalog.catalog_path(in_path)
32
d,f = os.path.split(path)
34
assert(f == catalog.os_dependent_catalog_name())
35
def check_current(self):
37
path = catalog.catalog_path(in_path)
38
d,f = os.path.split(path)
39
assert(d == os.path.abspath(in_path))
40
assert(f == catalog.os_dependent_catalog_name())
42
if sys.platform != 'win32':
44
path = catalog.catalog_path(in_path)
45
d,f = os.path.split(path)
46
assert(d == os.path.expanduser(in_path))
47
assert(f == catalog.os_dependent_catalog_name())
48
def check_module(self):
49
# hand it a module and see if it uses the parent directory
51
path = catalog.catalog_path(os.__file__)
52
d,f = os.path.split(os.__file__)
53
d2,f = os.path.split(path)
56
# use os.__file__ to get a usable directory.
57
in_path,f = os.path.split(os.__file__)
58
path = catalog.catalog_path(in_path)
59
d,f = os.path.split(path)
61
def check_bad_path(self):
63
in_path = 'stupid_path_name'
64
path = catalog.catalog_path(in_path)
67
class test_get_catalog(NumpyTestCase):
68
""" This only tests whether new catalogs are created correctly.
69
And whether non-existent return None correctly with read mode.
70
Putting catalogs in the right place is all tested with
73
def get_test_dir(self,erase = 0):
74
# make sure tempdir catalog doesn't exist
76
#temp = tempfile.gettempdir()
77
pardir = tempfile.mktemp(suffix='cat_test')
78
if not os.path.exists(pardir):
80
cat_glob = os.path.join(pardir,catalog.os_dependent_catalog_name()+'.*')
81
cat_files = glob.glob(cat_glob)
83
for cat_file in cat_files:
86
def remove_dir(self,d):
87
import distutils.dir_util
88
distutils.dir_util.remove_tree(d)
90
def check_nonexistent_catalog_is_none(self):
91
pardir = self.get_test_dir(erase=1)
92
cat = catalog.get_catalog(pardir,'r')
93
self.remove_dir(pardir)
95
def check_create_catalog(self):
96
pardir = self.get_test_dir(erase=1)
97
cat = catalog.get_catalog(pardir,'c')
98
self.remove_dir(pardir)
99
assert(cat is not None)
101
class test_catalog(NumpyTestCase):
103
def clear_environ(self):
104
if os.environ.has_key('PYTHONCOMPILED'):
105
self.old_PYTHONCOMPILED = os.environ['PYTHONCOMPILED']
106
del os.environ['PYTHONCOMPILED']
108
self.old_PYTHONCOMPILED = None
109
def reset_environ(self):
110
if self.old_PYTHONCOMPILED:
111
os.environ['PYTHONCOMPILED'] = self.old_PYTHONCOMPILED
112
self.old_PYTHONCOMPILED = None
118
def check_set_module_directory(self):
119
q = catalog.catalog()
120
q.set_module_directory('bob')
121
r = q.get_module_directory()
123
def check_clear_module_directory(self):
124
q = catalog.catalog()
125
r = q.get_module_directory()
127
q.set_module_directory('bob')
128
r = q.clear_module_directory()
130
def check_get_environ_path(self):
131
if sys.platform == 'win32': sep = ';'
133
os.environ['PYTHONCOMPILED'] = sep.join(('path1','path2','path3'))
134
q = catalog.catalog()
135
path = q.get_environ_path()
136
assert(path == ['path1','path2','path3'])
137
def check_build_search_order1(self):
138
""" MODULE in search path should be replaced by module_dir.
140
q = catalog.catalog(['first','MODULE','third'])
141
q.set_module_directory('second')
142
order = q.build_search_order()
143
assert(order == ['first','second','third',catalog.default_dir()])
144
def check_build_search_order2(self):
145
""" MODULE in search path should be removed if module_dir==None.
147
q = catalog.catalog(['first','MODULE','third'])
148
order = q.build_search_order()
149
assert(order == ['first','third',catalog.default_dir()])
150
def check_build_search_order3(self):
151
""" If MODULE is absent, module_dir shouldn't be in search path.
153
q = catalog.catalog(['first','second'])
154
q.set_module_directory('third')
155
order = q.build_search_order()
156
assert(order == ['first','second',catalog.default_dir()])
157
def check_build_search_order4(self):
158
""" Make sure environment variable is getting used.
160
q = catalog.catalog(['first','second'])
161
if sys.platform == 'win32': sep = ';'
163
os.environ['PYTHONCOMPILED'] = sep.join(('MODULE','fourth','fifth'))
164
q.set_module_directory('third')
165
order = q.build_search_order()
166
assert(order == ['first','second','third','fourth','fifth',catalog.default_dir()])
168
def check_catalog_files1(self):
169
""" Be sure we get at least one file even without specifying the path.
171
q = catalog.catalog()
172
files = q.get_catalog_files()
173
assert(len(files) == 1)
175
def check_catalog_files2(self):
176
""" Ignore bad paths in the path.
178
q = catalog.catalog()
179
os.environ['PYTHONCOMPILED'] = '_some_bad_path_'
180
files = q.get_catalog_files()
181
assert(len(files) == 1)
183
def check_get_existing_files1(self):
184
""" Shouldn't get any files when temp doesn't exist and no path set.
187
q = catalog.catalog()
188
files = q.get_existing_files()
189
restore_temp_catalog()
190
assert(len(files) == 0)
191
def check_get_existing_files2(self):
192
""" Shouldn't get a single file from the temp dir.
195
q = catalog.catalog()
196
# create a dummy file
198
q.add_function('code', os.getpid)
200
q = catalog.catalog()
201
files = q.get_existing_files()
202
restore_temp_catalog()
203
assert(len(files) == 1)
205
def check_access_writable_file(self):
206
""" There should always be a writable file -- even if it is in temp
208
q = catalog.catalog()
209
file = q.get_writable_file()
216
def check_writable_with_bad_path(self):
217
""" There should always be a writable file -- even if search paths contain
220
if sys.platform == 'win32': sep = ';'
222
os.environ['PYTHONCOMPILED'] = sep.join(('_bad_path_name_'))
223
q = catalog.catalog()
224
file = q.get_writable_file()
231
def check_writable_dir(self):
232
""" Check that we can create a file in the writable directory
234
q = catalog.catalog()
235
d = q.get_writable_dir()
236
file = os.path.join(d,'some_silly_file')
243
def check_unique_module_name(self):
244
""" Check that we can create a file in the writable directory
246
q = catalog.catalog()
247
file = q.unique_module_name('bob')
249
assert(not os.path.exists(cfile1))
250
#make sure it is writable
256
# try again with same code fragment -- should get unique name
257
file = q.unique_module_name('bob')
259
assert(not os.path.exists(cfile2+'.cpp'))
261
def check_add_function_persistent1(self):
262
""" Test persisting a function in the default catalog
265
q = catalog.catalog()
266
# just use some already available functions
268
funcs = [string.upper, string.lower, string.find,string.replace]
270
q.add_function_persistent('code',i)
271
pfuncs = q.get_cataloged_functions('code')
272
# any way to clean modules???
273
restore_temp_catalog()
277
def check_add_function_ordered(self):
279
q = catalog.catalog()
282
q.add_function('f',string.upper)
283
q.add_function('f',string.lower)
284
q.add_function('ff',string.find)
285
q.add_function('ff',string.replace)
286
q.add_function('fff',string.atof)
287
q.add_function('fff',string.atoi)
290
# now we're gonna make a new catalog with same code
291
# but different functions in a specified module directory
292
env_dir = empty_temp_dir()
293
r = catalog.catalog(env_dir)
294
r.add_function('ff',os.abort)
295
r.add_function('ff',os.chdir)
296
r.add_function('fff',os.access)
297
r.add_function('fff',os.open)
299
# now we're gonna make a new catalog with same code
300
# but different functions in a user specified directory
301
user_dir = empty_temp_dir()
302
s = catalog.catalog(user_dir)
304
s.add_function('fff',re.match)
305
s.add_function('fff',re.purge)
308
# open new catalog and make sure it retreives the functions
309
# from d catalog instead of the temp catalog (made by q)
310
os.environ['PYTHONCOMPILED'] = env_dir
311
t = catalog.catalog(user_dir)
312
funcs1 = t.get_functions('f')
313
funcs2 = t.get_functions('ff')
314
funcs3 = t.get_functions('fff')
315
restore_temp_catalog()
316
# make sure everything is read back in the correct order
317
# a little cheating... I'm ignoring any functions that might have
318
# been read in from a prior catalog file (such as the defualt one).
319
# the test should really be made so that these aren't read in, but
320
# until I get this figured out...
321
#assert(funcs1 == [string.lower,string.upper])
322
#assert(funcs2 == [os.chdir,os.abort,string.replace,string.find])
323
#assert(funcs3 == [re.purge,re.match,os.open,
324
# os.access,string.atoi,string.atof])
325
assert(funcs1[:2] == [string.lower,string.upper]),`funcs1`
326
assert(funcs2[:4] == [os.chdir,os.abort,string.replace,string.find])
327
assert(funcs3[:6] == [re.purge,re.match,os.open,
328
os.access,string.atoi,string.atof])
329
cleanup_temp_dir(user_dir)
330
cleanup_temp_dir(env_dir)
333
if __name__ == '__main__':
334
NumpyTest('weave.catalog').run()