Skip to content

Safer environment #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Nov 6, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions nibabel/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,34 @@
from .. import data as nibd

from nose import with_setup

from nose.tools import assert_equal, \
assert_raises, raises

from .test_environment import (setup_environment,
teardown_environment,
DATA_KEY,
USER_KEY)

GIVEN_ENV = {}
DATA_KEY = 'NIPY_DATA_PATH'
USER_KEY = 'NIPY_USER_DIR'

DATA_FUNCS = {}

def setup_environment():
"""Setup test environment for some functions that are tested
in this module. In particular this functions stores attributes
and other things that we need to stub in some test functions.
This needs to be done on a function level and not module level because
each testfunction needs a pristine environment.
"""
global GIVEN_ENV
GIVEN_ENV['env'] = env.copy()
GIVEN_ENV['sys_dir_func'] = nibd.get_nipy_system_dir
GIVEN_ENV['path_func'] = nibd.get_data_path
def setup_data_env():
setup_environment()
global DATA_FUNCS
DATA_FUNCS['home_dir_func'] = nibd.get_nipy_user_dir
DATA_FUNCS['sys_dir_func'] = nibd.get_nipy_system_dir
DATA_FUNCS['path_func'] = nibd.get_data_path


def teardown_environment():
"""Restore things that were remembered by the setup_environment function
"""
orig_env = GIVEN_ENV['env']
for key in env.keys():
if key not in orig_env:
del env[key]
env.update(orig_env)
nibd.get_nipy_system_dir = GIVEN_ENV['sys_dir_func']
nibd.get_data_path = GIVEN_ENV['path_func']
def teardown_data_env():
teardown_environment()
nibd.get_nipy_user_dir = DATA_FUNCS['home_dir_func']
nibd.get_nipy_system_dir = DATA_FUNCS['sys_dir_func']
nibd.get_data_path = DATA_FUNCS['path_func']


# decorator to use setup, teardown environment
with_environment = with_setup(setup_environment, teardown_environment)
with_environment = with_setup(setup_data_env, teardown_data_env)


def test_datasource():
Expand Down Expand Up @@ -145,36 +137,37 @@ def test_data_path():
del env[DATA_KEY]
if USER_KEY in env:
del os.environ[USER_KEY]
fake_user_dir = '/user/path'
nibd.get_nipy_system_dir = lambda : ''
# now we should only have the default
nibd.get_nipy_user_dir = lambda : fake_user_dir
# now we should only have anything pointed to in the user's dir
old_pth = get_data_path()
# We should have only sys.prefix and, iff sys.prefix == /usr,
# '/usr/local'. This last to is deal with Debian patching to
# distutils.
def_dirs = [pjoin(sys.prefix, 'share', 'nipy')]
if sys.prefix == '/usr':
def_dirs.append(pjoin('/usr/local', 'share', 'nipy'))
home_nipy = pjoin(os.path.expanduser('~'), '.nipy')
yield assert_equal, old_pth, def_dirs + [home_nipy]
assert_equal(old_pth, def_dirs + ['/user/path'])
# then we'll try adding some of our own
tst_pth = '/a/path' + os.path.pathsep + '/b/ path'
tst_list = ['/a/path', '/b/ path']
# First, an environment variable
os.environ[DATA_KEY] = tst_list[0]
yield assert_equal, get_data_path(), tst_list[:1] + old_pth
assert_equal(get_data_path(), tst_list[:1] + old_pth)
os.environ[DATA_KEY] = tst_pth
yield assert_equal, get_data_path(), tst_list + old_pth
assert_equal(get_data_path(), tst_list + old_pth)
del os.environ[DATA_KEY]
# Next, make a fake user directory, and put a file in there
with TemporaryDirectory() as tmpdir:
tmpfile = pjoin(tmpdir, 'config.ini')
with open(tmpfile, 'wt') as fobj:
fobj.write('[DATA]\n')
fobj.write('path = %s' % tst_pth)
os.environ[USER_KEY] = tmpdir
yield assert_equal, get_data_path(), tst_list + def_dirs + [tmpdir]
del os.environ[USER_KEY]
yield assert_equal, get_data_path(), old_pth
nibd.get_nipy_user_dir = lambda : tmpdir
assert_equal(get_data_path(), tst_list + def_dirs + [tmpdir])
nibd.get_nipy_user_dir = lambda : fake_user_dir
assert_equal(get_data_path(), old_pth)
# with some trepidation, the system config files
with TemporaryDirectory() as tmpdir:
nibd.get_nipy_system_dir = lambda : tmpdir
Expand All @@ -186,8 +179,8 @@ def test_data_path():
with open(tmpfile, 'wt') as fobj:
fobj.write('[DATA]\n')
fobj.write('path = %s\n' % '/path/two')
yield (assert_equal, get_data_path(),
tst_list + ['/path/two'] + old_pth)
assert_equal(get_data_path(),
tst_list + ['/path/two'] + old_pth)


def test_find_data_dir():
Expand Down
76 changes: 76 additions & 0 deletions nibabel/tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
""" Testing environment settings
"""

import os
from os import environ as env
from os.path import join as pjoin
import sys

import numpy as np

from .. import environment as nibe

from numpy.testing import (assert_array_almost_equal,
assert_array_equal)

from nose.tools import assert_true, assert_equal, assert_raises

from nose import with_setup

GIVEN_ENV = {}
DATA_KEY = 'NIPY_DATA_PATH'
USER_KEY = 'NIPY_USER_DIR'


def setup_environment():
"""Setup test environment for some functions that are tested
in this module. In particular this functions stores attributes
and other things that we need to stub in some test functions.
This needs to be done on a function level and not module level because
each testfunction needs a pristine environment.
"""
global GIVEN_ENV
GIVEN_ENV['env'] = env.copy()


def teardown_environment():
"""Restore things that were remembered by the setup_environment function
"""
orig_env = GIVEN_ENV['env']
for key in env.keys():
if key not in orig_env:
del env[key]
env.update(orig_env)


# decorator to use setup, teardown environment
with_environment = with_setup(setup_environment, teardown_environment)


def test_nipy_home():
# Test logic for nipy home directory
assert_equal(nibe.get_home_dir(), os.path.expanduser('~'))


@with_environment
def test_user_dir():
if USER_KEY in env:
del env[USER_KEY]
home_dir = nibe.get_home_dir()
if os.name == "posix":
exp = pjoin(home_dir, '.nipy')
else:
exp = pjoin(home_dir, '_nipy')
assert_equal(exp, nibe.get_nipy_user_dir())
env[USER_KEY] = '/a/path'
assert_equal('/a/path', nibe.get_nipy_user_dir())


def test_sys_dir():
sys_dir = nibe.get_nipy_system_dir()
if os.name == 'nt':
assert_equal(sys_dir, r'C:\etc\nipy')
elif os.name == 'posix':
assert_equal(sys_dir, r'/etc/nipy')
else:
assert_equal(sys_dir, None)