##// END OF EJS Templates
Some white space fixing, and comments
Some white space fixing, and comments

File last commit:

r1804:9b6575bc
r1804:9b6575bc
Show More
test_genutils.py
249 lines | 7.3 KiB | text/x-python | PythonLexer
Brian Granger
Added tests for the new get_ipython_dir and get_security_dir ...
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
#-----------------------------------------------------------------------------
from IPython import genutils
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 from IPython.testing.decorators import skipif
from nose import with_setup
Jorgen Stenarson
added test for genutils.popkey
r1751 from nose.tools import raises
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 from os.path import join, abspath, split
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 import os, sys, IPython
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 import nose.tools as nt
Brian Granger
Added tests for the new get_ipython_dir and get_security_dir ...
r1617
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 env = os.environ
Brian Granger
Added tests for the new get_ipython_dir and get_security_dir ...
r1617
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 try:
import _winreg as wreg
except ImportError:
Jorgen Stenarson
Fake _winreg in tests for non windows platforms
r1793 #Fake _winreg module on none windows platforms
import new
Jorgen Stenarson
Some reformatting of code
r1796 sys.modules["_winreg"] = new.module("_winreg")
Jorgen Stenarson
Fake _winreg in tests for non windows platforms
r1793 import _winreg as wreg
#Add entries that needs to be stubbed by the testing code
(wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
Jorgen Stenarson
Fixed bug on tests so they work when iptest is not called from within IPython/tests
r1799 test_file_path = split(abspath(__file__))[0]
Jorgen Stenarson
Some white space fixing, and comments
r1804
#
# Setup/teardown functions/decorators
Jorgen Stenarson
Moved skip decorator to testing and created similar ones for OSX and linux, create delete testdirs in module setup/teardown
r1803 #
Jorgen Stenarson
Some white space fixing, and comments
r1804
Jorgen Stenarson
Moved skip decorator to testing and created similar ones for OSX and linux, create delete testdirs in module setup/teardown
r1803 def setup():
try:
os.makedirs("home_test_dir/_ipython")
except WindowsError:
pass #Or should we complain that the test directory already exists??
def teardown():
try:
os.removedirs("home_test_dir/_ipython")
except WindowsError:
pass #Or should we complain that the test directory already exists??
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
def setup_environment():
global oldstuff, platformstuff
oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,)
Jorgen Stenarson
Some reformatting of code
r1796 if os.name == 'nt':
platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
if 'IPYTHONDIR' in env:
del env['IPYTHONDIR']
def teardown_environment():
(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
Some reformatting of code
r1796 if os.name == 'nt':
(wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
Jorgen Stenarson
Some reformatting of code
r1796 with_enivronment = with_setup(setup_environment, teardown_environment)
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
Jorgen Stenarson
Some white space fixing, and comments
r1804
#
# Tests for get_home_dir
#
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Fix for py2exe when using uncompressed lib/
r1745 def test_get_home_dir_1():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase for py2exe logic, un-compressed lib
"""
Jorgen Stenarson
Some reformatting of code
r1796 sys.frozen = True
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
#fake filename for IPython.__init__
Jorgen Stenarson
Fixed bug on tests so they work when iptest is not called from within IPython/tests
r1799 IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Lib/IPython/__init__.py"))
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")))
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_2():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase for py2exe logic, compressed lib
"""
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 sys.frozen = True
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 #fake filename for IPython.__init__
Jorgen Stenarson
Fixed bug on tests so they work when iptest is not called from within IPython/tests
r1799 IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Library.zip/IPython/__init__.py"))
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")).lower())
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_3():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase $HOME is set, then use its value as home directory."""
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 env["HOME"] = join(test_file_path, "home_test_dir")
Jorgen Stenarson
Fix for py2exe when using uncompressed lib/
r1745 home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, env["HOME"])
Jorgen Stenarson
Fix for py2exe when using uncompressed lib/
r1745
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_4():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase $HOME is not set, os=='posix'.
This should fail with HomeDirError"""
Jorgen Stenarson
Some reformatting of code
r1796 os.name = 'posix'
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 del os.environ["HOME"]
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir)
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_5():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase $HOME is not set, os=='nt'
env['HOMEDRIVE'],env['HOMEPATH'] points to path."""
Jorgen Stenarson
Some reformatting of code
r1796 os.name = 'nt'
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 del os.environ["HOME"]
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "home_test_dir"
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")))
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_6():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase $HOME is not set, os=='nt'
env['HOMEDRIVE'],env['HOMEPATH'] do not point to path.
env['USERPROFILE'] points to path
"""
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750
Jorgen Stenarson
Some reformatting of code
r1796 os.name = 'nt'
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 del os.environ["HOME"]
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "DOES NOT EXIST"
env["USERPROFILE"] = abspath(join(test_file_path, "home_test_dir"))
Jorgen Stenarson
Fix for py2exe when using uncompressed lib/
r1745
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")))
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 # Should we stub wreg fully so we can run the test on all platforms?
Jorgen Stenarson
Removed skip_on_win32 should dependencies should be stubbed
r1795 #@skip_if_not_win32
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_home_dir_7():
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 """Testcase $HOME is not set, os=='nt'
env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing
"""
Jorgen Stenarson
Some reformatting of code
r1796 os.name = 'nt'
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 del env["HOME"], env['HOMEDRIVE']
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
#Stub windows registry functions
Jorgen Stenarson
Some reformatting of code
r1796 def OpenKey(x, y):
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746 class key:
def Close(self):
pass
return key()
Jorgen Stenarson
Some reformatting of code
r1796 def QueryValueEx(x, y):
Jorgen Stenarson
Fixed bug on tests so they work when iptest is not called from within IPython/tests
r1799 return [abspath(join(test_file_path, "home_test_dir"))]
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
Jorgen Stenarson
Some reformatting of code
r1796 wreg.OpenKey = OpenKey
wreg.QueryValueEx = QueryValueEx
Jorgen Stenarson
Added tests test_get_home_dir_3-test_get_home_dir_9...
r1746
home_dir = genutils.get_home_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")))
Jorgen Stenarson
Fix for py2exe when using uncompressed lib/
r1745
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 #
# Tests for get_ipython_dir
#
@with_enivronment
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749 def test_get_ipython_dir_1():
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 """2 Testcase to see if we can call get_ipython_dir without Exceptions."""
Jorgen Stenarson
Some reformatting of code
r1796 env['IPYTHONDIR'] = "someplace/.ipython"
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749 ipdir = genutils.get_ipython_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython"))
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_ipython_dir_2():
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 """3 Testcase to see if we can call get_ipython_dir without Exceptions."""
Jorgen Stenarson
Fixing pep-8 conformance issues
r1801 genutils.get_home_dir = lambda : "someplace"
Jorgen Stenarson
Some reformatting of code
r1796 os.name = "posix"
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 ipdir = genutils.get_ipython_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython")))
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 @with_enivronment
Jorgen Stenarson
Removing simple test cases for get_home_dir and get_ipython_dir
r1800 def test_get_ipython_dir_3():
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 """4 Testcase to see if we can call get_ipython_dir without Exceptions."""
Jorgen Stenarson
Some reformatting of code
r1796 genutils.get_home_dir = lambda : "someplace"
os.name = "nt"
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 ipdir = genutils.get_ipython_dir()
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython")))
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
r1750 #
# Tests for get_security_dir
#
Jorgen Stenarson
Changed get_ipython_dir to return unicode otherwise the ...
r1749
Jorgen Stenarson
Changed tests to use decorator to setup, teardown environment
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
added test for genutils.popkey
r1751
Jorgen Stenarson
Fake _winreg in tests for non windows platforms
r1793 #
# Tests for popkey
#
Jorgen Stenarson
added test for genutils.popkey
r1751
def test_popkey_1():
Jorgen Stenarson
Some reformatting of code
r1796 dct = dict(a=1, b=2, c=3)
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
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
added test for genutils.popkey
r1751 def test_popkey_2():
Jorgen Stenarson
Some reformatting of code
r1796 dct = dict(a=1, b=2, c=3)
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
r1802 nt.assert_raises(KeyError, genutils.popkey, dct, "d")
Jorgen Stenarson
added test for genutils.popkey
r1751
def test_popkey_3():
Jorgen Stenarson
Some reformatting of code
r1796 dct = dict(a=1, b=2, c=3)
Jorgen Stenarson
Remove bare asserts by switching to use nose.tools.assert_* functions to check test conditions.
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())