Skip to content

Commit 6839c61

Browse files
committed
RF - added tests for environment and refactored data tests
1 parent 31f8d7f commit 6839c61

File tree

2 files changed

+100
-34
lines changed

2 files changed

+100
-34
lines changed

nibabel/tests/test_data.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,32 @@
1818
from .. import data as nibd
1919

2020
from nose import with_setup
21+
2122
from nose.tools import assert_equal, \
2223
assert_raises, raises
2324

25+
from .test_environment import (setup_environment,
26+
teardown_environment,
27+
DATA_KEY,
28+
USER_KEY)
2429

25-
GIVEN_ENV = {}
26-
DATA_KEY = 'NIPY_DATA_PATH'
27-
USER_KEY = 'NIPY_USER_DIR'
28-
30+
DATA_FUNCS = {}
2931

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
4137

4238

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']
5343

5444

5545
# 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)
5747

5848

5949
def test_datasource():
@@ -146,7 +136,7 @@ def test_data_path():
146136
if USER_KEY in env:
147137
del os.environ[USER_KEY]
148138
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
150140
old_pth = get_data_path()
151141
# We should have only sys.prefix and, iff sys.prefix == /usr,
152142
# '/usr/local'. This last to is deal with Debian patching to
@@ -155,15 +145,15 @@ def test_data_path():
155145
if sys.prefix == '/usr':
156146
def_dirs.append(pjoin('/usr/local', 'share', 'nipy'))
157147
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])
159149
# then we'll try adding some of our own
160150
tst_pth = '/a/path' + os.path.pathsep + '/b/ path'
161151
tst_list = ['/a/path', '/b/ path']
162152
# First, an environment variable
163153
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)
165155
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)
167157
del os.environ[DATA_KEY]
168158
# Next, make a fake user directory, and put a file in there
169159
with TemporaryDirectory() as tmpdir:
@@ -172,9 +162,9 @@ def test_data_path():
172162
fobj.write('[DATA]\n')
173163
fobj.write('path = %s' % tst_pth)
174164
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])
176166
del os.environ[USER_KEY]
177-
yield assert_equal, get_data_path(), old_pth
167+
assert_equal(get_data_path(), old_pth)
178168
# with some trepidation, the system config files
179169
with TemporaryDirectory() as tmpdir:
180170
nibd.get_nipy_system_dir = lambda : tmpdir
@@ -186,8 +176,8 @@ def test_data_path():
186176
with open(tmpfile, 'wt') as fobj:
187177
fobj.write('[DATA]\n')
188178
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)
191181

192182

193183
def test_find_data_dir():

nibabel/tests/test_environment.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
""" Testing environment settings
2+
"""
3+
4+
import os
5+
from os import environ as env
6+
from os.path import join as pjoin
7+
import sys
8+
9+
import numpy as np
10+
11+
from .. import environment as nibe
12+
13+
from numpy.testing import (assert_array_almost_equal,
14+
assert_array_equal)
15+
16+
from nose.tools import assert_true, assert_equal, assert_raises
17+
18+
from nose import with_setup
19+
20+
GIVEN_ENV = {}
21+
DATA_KEY = 'NIPY_DATA_PATH'
22+
USER_KEY = 'NIPY_USER_DIR'
23+
24+
25+
def setup_environment():
26+
"""Setup test environment for some functions that are tested
27+
in this module. In particular this functions stores attributes
28+
and other things that we need to stub in some test functions.
29+
This needs to be done on a function level and not module level because
30+
each testfunction needs a pristine environment.
31+
"""
32+
global GIVEN_ENV
33+
GIVEN_ENV['env'] = env.copy()
34+
35+
36+
def teardown_environment():
37+
"""Restore things that were remembered by the setup_environment function
38+
"""
39+
orig_env = GIVEN_ENV['env']
40+
for key in env.keys():
41+
if key not in orig_env:
42+
del env[key]
43+
env.update(orig_env)
44+
45+
46+
# decorator to use setup, teardown environment
47+
with_environment = with_setup(setup_environment, teardown_environment)
48+
49+
50+
def test_nipy_home():
51+
# Test logic for nipy home directory
52+
assert_equal(nibe.get_home_dir(), os.path.expanduser('~'))
53+
54+
55+
@with_environment
56+
def test_user_dir():
57+
if USER_KEY in env:
58+
del env[USER_KEY]
59+
home_dir = nibe.get_home_dir()
60+
if os.name == "posix":
61+
exp = pjoin(home_dir, '.nipy')
62+
else:
63+
exp = pjoin(home_dir, '_nipy')
64+
assert_equal(exp, nibe.get_nipy_user_dir())
65+
env[USER_KEY] = '/a/path'
66+
assert_equal('/a/path', nibe.get_nipy_user_dir())
67+
68+
69+
def test_sys_dir():
70+
sys_dir = nibe.get_nipy_system_dir()
71+
if os.name == 'nt':
72+
assert_equal(sys_dir, r'C:\etc\nipy')
73+
elif os.name == 'posix':
74+
assert_equal(sys_dir, r'/etc/nipy')
75+
else:
76+
assert_equal(sys_dir, None)

0 commit comments

Comments
 (0)