test_genutils.py
291 lines
| 8.8 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r1617 | # encoding: utf-8 | ||
"""Tests for genutils.py""" | ||||
__docformat__ = "restructuredtext en" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r1843 | # stdlib | ||
import os | ||||
import shutil | ||||
import sys | ||||
import tempfile | ||||
Jorgen Stenarson
|
r1751 | |||
Jorgen Stenarson
|
r1801 | from os.path import join, abspath, split | ||
Fernando Perez
|
r1843 | |||
# third-party | ||||
Jorgen Stenarson
|
r1802 | import nose.tools as nt | ||
Brian Granger
|
r1617 | |||
Fernando Perez
|
r1843 | from nose import with_setup | ||
from nose.tools import raises | ||||
Brian Granger
|
r1617 | |||
Fernando Perez
|
r1843 | # Our own | ||
import IPython | ||||
from IPython import genutils | ||||
from IPython.testing.decorators import skipif, skip_if_not_win32 | ||||
# Platform-dependent imports | ||||
Jorgen Stenarson
|
r1750 | try: | ||
import _winreg as wreg | ||||
except ImportError: | ||||
Jorgen Stenarson
|
r1793 | #Fake _winreg module on none windows platforms | ||
import new | ||||
Jorgen Stenarson
|
r1796 | sys.modules["_winreg"] = new.module("_winreg") | ||
Jorgen Stenarson
|
r1793 | import _winreg as wreg | ||
#Add entries that needs to be stubbed by the testing code | ||||
(wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | ||||
Jorgen Stenarson
|
r1750 | |||
Fernando Perez
|
r1843 | #----------------------------------------------------------------------------- | ||
# Globals | ||||
#----------------------------------------------------------------------------- | ||||
env = os.environ | ||||
TEST_FILE_PATH = split(abspath(__file__))[0] | ||||
TMP_TEST_DIR = tempfile.mkdtemp() | ||||
HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | ||||
IP_TEST_DIR = join(HOME_TEST_DIR,'_ipython') | ||||
Jorgen Stenarson
|
r1804 | # | ||
# Setup/teardown functions/decorators | ||||
Jorgen Stenarson
|
r1803 | # | ||
def setup(): | ||||
Jorgen Stenarson
|
r1805 | """Setup testenvironment for the module: | ||
- Adds dummy home dir tree | ||||
""" | ||||
Fernando Perez
|
r1843 | # Do not mask exceptions here. In particular, catching WindowsError is a | ||
# problem because that exception is only defined on Windows... | ||||
os.makedirs(IP_TEST_DIR) | ||||
Jorgen Stenarson
|
r1803 | |||
def teardown(): | ||||
Jorgen Stenarson
|
r1805 | """Teardown testenvironment for the module: | ||
- Remove dummy home dir tree | ||||
""" | ||||
Fernando Perez
|
r1843 | # Note: we remove the parent test dir, which is the root of all test | ||
# subdirs we may have created. Use shutil instead of os.removedirs, so | ||||
# that non-empty directories are all recursively removed. | ||||
shutil.rmtree(TMP_TEST_DIR) | ||||
Jorgen Stenarson
|
r1803 | |||
Jorgen Stenarson
|
r1750 | |||
def setup_environment(): | ||||
Jorgen Stenarson
|
r1805 | """Setup testenvironment 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. | ||||
""" | ||||
Jorgen Stenarson
|
r1750 | global oldstuff, platformstuff | ||
oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) | ||||
Jorgen Stenarson
|
r1796 | if os.name == 'nt': | ||
platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | ||||
Jorgen Stenarson
|
r1750 | |||
if 'IPYTHONDIR' in env: | ||||
del env['IPYTHONDIR'] | ||||
def teardown_environment(): | ||||
Jorgen Stenarson
|
r1805 | """Restore things that were remebered by the setup_environment function | ||
""" | ||||
Jorgen Stenarson
|
r1750 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff | ||
for key in env.keys(): | ||||
if key not in oldenv: | ||||
del env[key] | ||||
env.update(oldenv) | ||||
if hasattr(sys, 'frozen'): | ||||
del sys.frozen | ||||
Jorgen Stenarson
|
r1796 | if os.name == 'nt': | ||
(wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | ||||
Jorgen Stenarson
|
r1750 | |||
Jorgen Stenarson
|
r1805 | # Build decorator that uses the setup_environment/setup_environment | ||
Jorgen Stenarson
|
r1812 | with_enivronment = with_setup(setup_environment, teardown_environment) | ||
Jorgen Stenarson
|
r1750 | |||
Jorgen Stenarson
|
r1804 | |||
# | ||||
# Tests for get_home_dir | ||||
# | ||||
Jorgen Stenarson
|
r1820 | @skip_if_not_win32 | ||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1745 | def test_get_home_dir_1(): | ||
Jorgen Stenarson
|
r1746 | """Testcase for py2exe logic, un-compressed lib | ||
""" | ||||
Jorgen Stenarson
|
r1796 | sys.frozen = True | ||
Jorgen Stenarson
|
r1746 | |||
#fake filename for IPython.__init__ | ||||
Jorgen Stenarson
|
r1855 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | ||
Jorgen Stenarson
|
r1746 | |||
home_dir = genutils.get_home_dir() | ||||
Fernando Perez
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1812 | @skip_if_not_win32 | ||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_2(): | ||
Jorgen Stenarson
|
r1746 | """Testcase for py2exe logic, compressed lib | ||
""" | ||||
Jorgen Stenarson
|
r1801 | sys.frozen = True | ||
Jorgen Stenarson
|
r1746 | #fake filename for IPython.__init__ | ||
Jorgen Stenarson
|
r1855 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | ||
Jorgen Stenarson
|
r1746 | |||
home_dir = genutils.get_home_dir() | ||||
Fernando Perez
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_3(): | ||
Jorgen Stenarson
|
r1746 | """Testcase $HOME is set, then use its value as home directory.""" | ||
Fernando Perez
|
r1843 | env["HOME"] = HOME_TEST_DIR | ||
Jorgen Stenarson
|
r1745 | home_dir = genutils.get_home_dir() | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(home_dir, env["HOME"]) | ||
Jorgen Stenarson
|
r1745 | |||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_4(): | ||
Jorgen Stenarson
|
r1812 | """Testcase $HOME is not set, os=='poix'. | ||
Jorgen Stenarson
|
r1746 | This should fail with HomeDirError""" | ||
Jorgen Stenarson
|
r1796 | os.name = 'posix' | ||
Jorgen Stenarson
|
r1812 | if 'HOME' in env: del env['HOME'] | ||
Jorgen Stenarson
|
r1802 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1820 | @skip_if_not_win32 | ||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_5(): | ||
Jorgen Stenarson
|
r1746 | """Testcase $HOME is not set, os=='nt' | ||
env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" | ||||
Jorgen Stenarson
|
r1812 | |||
Jorgen Stenarson
|
r1796 | os.name = 'nt' | ||
Jorgen Stenarson
|
r1812 | if 'HOME' in env: del env['HOME'] | ||
Jorgen Stenarson
|
r1855 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) | ||
Jorgen Stenarson
|
r1746 | |||
home_dir = genutils.get_home_dir() | ||||
Fernando Perez
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1820 | @skip_if_not_win32 | ||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_6(): | ||
Jorgen Stenarson
|
r1746 | """Testcase $HOME is not set, os=='nt' | ||
env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. | ||||
env['USERPROFILE'] points to path | ||||
""" | ||||
Jorgen Stenarson
|
r1750 | |||
Jorgen Stenarson
|
r1796 | os.name = 'nt' | ||
Jorgen Stenarson
|
r1812 | if 'HOME' in env: del env['HOME'] | ||
Fernando Perez
|
r1843 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST" | ||
env["USERPROFILE"] = abspath(HOME_TEST_DIR) | ||||
Jorgen Stenarson
|
r1745 | |||
Jorgen Stenarson
|
r1746 | home_dir = genutils.get_home_dir() | ||
Fernando Perez
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1750 | # Should we stub wreg fully so we can run the test on all platforms? | ||
Jorgen Stenarson
|
r1812 | @skip_if_not_win32 | ||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_home_dir_7(): | ||
Jorgen Stenarson
|
r1746 | """Testcase $HOME is not set, os=='nt' | ||
env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing | ||||
""" | ||||
Jorgen Stenarson
|
r1796 | os.name = 'nt' | ||
Jorgen Stenarson
|
r1812 | if 'HOME' in env: del env['HOME'] | ||
if 'HOMEDRIVE' in env: del env['HOMEDRIVE'] | ||||
Jorgen Stenarson
|
r1746 | |||
#Stub windows registry functions | ||||
Jorgen Stenarson
|
r1796 | def OpenKey(x, y): | ||
Jorgen Stenarson
|
r1746 | class key: | ||
def Close(self): | ||||
pass | ||||
return key() | ||||
Jorgen Stenarson
|
r1796 | def QueryValueEx(x, y): | ||
Fernando Perez
|
r1843 | return [abspath(HOME_TEST_DIR)] | ||
Jorgen Stenarson
|
r1746 | |||
Jorgen Stenarson
|
r1796 | wreg.OpenKey = OpenKey | ||
wreg.QueryValueEx = QueryValueEx | ||||
Jorgen Stenarson
|
r1746 | |||
home_dir = genutils.get_home_dir() | ||||
Fernando Perez
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
Jorgen Stenarson
|
r1745 | |||
Jorgen Stenarson
|
r1750 | # | ||
# Tests for get_ipython_dir | ||||
# | ||||
@with_enivronment | ||||
Jorgen Stenarson
|
r1749 | def test_get_ipython_dir_1(): | ||
Jorgen Stenarson
|
r1805 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||
Jorgen Stenarson
|
r1796 | env['IPYTHONDIR'] = "someplace/.ipython" | ||
Jorgen Stenarson
|
r1749 | ipdir = genutils.get_ipython_dir() | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) | ||
Jorgen Stenarson
|
r1749 | |||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_ipython_dir_2(): | ||
Jorgen Stenarson
|
r1805 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||
Jorgen Stenarson
|
r1801 | genutils.get_home_dir = lambda : "someplace" | ||
Jorgen Stenarson
|
r1796 | os.name = "posix" | ||
Jorgen Stenarson
|
r1750 | ipdir = genutils.get_ipython_dir() | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) | ||
Jorgen Stenarson
|
r1749 | |||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
Jorgen Stenarson
|
r1800 | def test_get_ipython_dir_3(): | ||
Jorgen Stenarson
|
r1805 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||
Jorgen Stenarson
|
r1796 | genutils.get_home_dir = lambda : "someplace" | ||
os.name = "nt" | ||||
Jorgen Stenarson
|
r1750 | ipdir = genutils.get_ipython_dir() | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) | ||
Jorgen Stenarson
|
r1749 | |||
Jorgen Stenarson
|
r1750 | # | ||
# Tests for get_security_dir | ||||
# | ||||
Jorgen Stenarson
|
r1749 | |||
Jorgen Stenarson
|
r1750 | @with_enivronment | ||
def test_get_security_dir(): | ||||
"""Testcase to see if we can call get_security_dir without Exceptions.""" | ||||
sdir = genutils.get_security_dir() | ||||
Jorgen Stenarson
|
r1751 | |||
Jorgen Stenarson
|
r1793 | # | ||
# Tests for popkey | ||||
# | ||||
Jorgen Stenarson
|
r1751 | |||
def test_popkey_1(): | ||||
Jorgen Stenarson
|
r1805 | """test_popkey_1, Basic usage test of popkey | ||
""" | ||||
Jorgen Stenarson
|
r1796 | dct = dict(a=1, b=2, c=3) | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | ||
nt.assert_equal(dct, dict(b=2, c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "b"), 2) | ||||
nt.assert_equal(dct, dict(c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "c"), 3) | ||||
nt.assert_equal(dct, dict()) | ||||
Jorgen Stenarson
|
r1751 | def test_popkey_2(): | ||
Jorgen Stenarson
|
r1805 | """test_popkey_2, Test to see that popkey of non occuring keys | ||
generates a KeyError exception | ||||
""" | ||||
Jorgen Stenarson
|
r1796 | dct = dict(a=1, b=2, c=3) | ||
Jorgen Stenarson
|
r1802 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") | ||
Jorgen Stenarson
|
r1751 | |||
def test_popkey_3(): | ||||
Jorgen Stenarson
|
r1805 | """test_popkey_3, Tests to see that popkey calls returns the correct value | ||
and that the key/value was removed from the dict. | ||||
""" | ||||
Jorgen Stenarson
|
r1796 | dct = dict(a=1, b=2, c=3) | ||
Jorgen Stenarson
|
r1802 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) | ||
nt.assert_equal(dct, dict(a=1, b=2, c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "B", 14), 14) | ||||
nt.assert_equal(dct, dict(a=1, b=2, c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "C", 15), 15) | ||||
nt.assert_equal(dct, dict(a=1, b=2, c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "a"), 1) | ||||
nt.assert_equal(dct, dict(b=2, c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "b"), 2) | ||||
nt.assert_equal(dct, dict(c=3)) | ||||
nt.assert_equal(genutils.popkey(dct, "c"), 3) | ||||
nt.assert_equal(dct, dict()) | ||||