18
18
from .. import data as nibd
19
19
20
20
from nose import with_setup
21
+
21
22
from nose .tools import assert_equal , \
22
23
assert_raises , raises
23
24
25
+ from .test_environment import (setup_environment ,
26
+ teardown_environment ,
27
+ DATA_KEY ,
28
+ USER_KEY )
24
29
25
- GIVEN_ENV = {}
26
- DATA_KEY = 'NIPY_DATA_PATH'
27
- USER_KEY = 'NIPY_USER_DIR'
28
-
30
+ DATA_FUNCS = {}
29
31
30
- def setup_environment ():
31
- """Setup test environment for some functions that are tested
32
- in this module. In particular this functions stores attributes
33
- and other things that we need to stub in some test functions.
34
- This needs to be done on a function level and not module level because
35
- each testfunction needs a pristine environment.
36
- """
37
- global GIVEN_ENV
38
- GIVEN_ENV ['env' ] = env .copy ()
39
- GIVEN_ENV ['sys_dir_func' ] = nibd .get_nipy_system_dir
40
- GIVEN_ENV ['path_func' ] = nibd .get_data_path
32
+ def setup_data_env ():
33
+ setup_environment ()
34
+ global DATA_FUNCS
35
+ DATA_FUNCS ['sys_dir_func' ] = nibd .get_nipy_system_dir
36
+ DATA_FUNCS ['path_func' ] = nibd .get_data_path
41
37
42
38
43
- def teardown_environment ():
44
- """Restore things that were remembered by the setup_environment function
45
- """
46
- orig_env = GIVEN_ENV ['env' ]
47
- for key in env .keys ():
48
- if key not in orig_env :
49
- del env [key ]
50
- env .update (orig_env )
51
- nibd .get_nipy_system_dir = GIVEN_ENV ['sys_dir_func' ]
52
- nibd .get_data_path = GIVEN_ENV ['path_func' ]
39
+ def teardown_data_env ():
40
+ teardown_environment ()
41
+ nibd .get_nipy_system_dir = DATA_FUNCS ['sys_dir_func' ]
42
+ nibd .get_data_path = DATA_FUNCS ['path_func' ]
53
43
54
44
55
45
# decorator to use setup, teardown environment
56
- with_environment = with_setup (setup_environment , teardown_environment )
46
+ with_environment = with_setup (setup_data_env , teardown_data_env )
57
47
58
48
59
49
def test_datasource ():
@@ -146,7 +136,7 @@ def test_data_path():
146
136
if USER_KEY in env :
147
137
del os .environ [USER_KEY ]
148
138
nibd .get_nipy_system_dir = lambda : ''
149
- # now we should only have the default
139
+ # now we should only have anything pointed to in the user's dir
150
140
old_pth = get_data_path ()
151
141
# We should have only sys.prefix and, iff sys.prefix == /usr,
152
142
# '/usr/local'. This last to is deal with Debian patching to
@@ -155,15 +145,15 @@ def test_data_path():
155
145
if sys .prefix == '/usr' :
156
146
def_dirs .append (pjoin ('/usr/local' , 'share' , 'nipy' ))
157
147
home_nipy = pjoin (os .path .expanduser ('~' ), '.nipy' )
158
- yield assert_equal , old_pth , def_dirs + [home_nipy ]
148
+ assert_equal ( old_pth , def_dirs + [home_nipy ])
159
149
# then we'll try adding some of our own
160
150
tst_pth = '/a/path' + os .path .pathsep + '/b/ path'
161
151
tst_list = ['/a/path' , '/b/ path' ]
162
152
# First, an environment variable
163
153
os .environ [DATA_KEY ] = tst_list [0 ]
164
- yield assert_equal , get_data_path (), tst_list [:1 ] + old_pth
154
+ assert_equal ( get_data_path (), tst_list [:1 ] + old_pth )
165
155
os .environ [DATA_KEY ] = tst_pth
166
- yield assert_equal , get_data_path (), tst_list + old_pth
156
+ assert_equal ( get_data_path (), tst_list + old_pth )
167
157
del os .environ [DATA_KEY ]
168
158
# Next, make a fake user directory, and put a file in there
169
159
with TemporaryDirectory () as tmpdir :
@@ -172,9 +162,9 @@ def test_data_path():
172
162
fobj .write ('[DATA]\n ' )
173
163
fobj .write ('path = %s' % tst_pth )
174
164
os .environ [USER_KEY ] = tmpdir
175
- yield assert_equal , get_data_path (), tst_list + def_dirs + [tmpdir ]
165
+ assert_equal ( get_data_path (), tst_list + def_dirs + [tmpdir ])
176
166
del os .environ [USER_KEY ]
177
- yield assert_equal , get_data_path (), old_pth
167
+ assert_equal ( get_data_path (), old_pth )
178
168
# with some trepidation, the system config files
179
169
with TemporaryDirectory () as tmpdir :
180
170
nibd .get_nipy_system_dir = lambda : tmpdir
@@ -186,8 +176,8 @@ def test_data_path():
186
176
with open (tmpfile , 'wt' ) as fobj :
187
177
fobj .write ('[DATA]\n ' )
188
178
fobj .write ('path = %s\n ' % '/path/two' )
189
- yield ( assert_equal , get_data_path (),
190
- tst_list + ['/path/two' ] + old_pth )
179
+ assert_equal ( get_data_path (),
180
+ tst_list + ['/path/two' ] + old_pth )
191
181
192
182
193
183
def test_find_data_dir ():
0 commit comments