Show More
@@ -1,160 +1,165 b'' | |||||
1 | """Decorators for labeling test objects. |
|
1 | """Decorators for labeling test objects. | |
2 |
|
2 | |||
3 | Decorators that merely return a modified version of the original |
|
3 | Decorators that merely return a modified version of the original | |
4 | function object are straightforward. Decorators that return a new |
|
4 | function object are straightforward. Decorators that return a new | |
5 | function object need to use |
|
5 | function object need to use | |
6 | nose.tools.make_decorator(original_function)(decorator) in returning |
|
6 | nose.tools.make_decorator(original_function)(decorator) in returning | |
7 | the decorator, in order to preserve metadata such as function name, |
|
7 | the decorator, in order to preserve metadata such as function name, | |
8 | setup and teardown functions and so on - see nose.tools for more |
|
8 | setup and teardown functions and so on - see nose.tools for more | |
9 | information. |
|
9 | information. | |
10 |
|
10 | |||
11 | This module provides a set of useful decorators meant to be ready to use in |
|
11 | This module provides a set of useful decorators meant to be ready to use in | |
12 | your own tests. See the bottom of the file for the ready-made ones, and if you |
|
12 | your own tests. See the bottom of the file for the ready-made ones, and if you | |
13 | find yourself writing a new one that may be of generic use, add it here. |
|
13 | find yourself writing a new one that may be of generic use, add it here. | |
14 |
|
14 | |||
15 | NOTE: This file contains IPython-specific decorators and imports the |
|
15 | NOTE: This file contains IPython-specific decorators and imports the | |
16 | numpy.testing.decorators file, which we've copied verbatim. Any of our own |
|
16 | numpy.testing.decorators file, which we've copied verbatim. Any of our own | |
17 | code will be added at the bottom if we end up extending this. |
|
17 | code will be added at the bottom if we end up extending this. | |
18 | """ |
|
18 | """ | |
19 |
|
19 | |||
20 | # Stdlib imports |
|
20 | # Stdlib imports | |
21 | import inspect |
|
21 | import inspect | |
22 | import sys |
|
22 | import sys | |
23 |
|
23 | |||
24 | # Third-party imports |
|
24 | # Third-party imports | |
25 |
|
25 | |||
26 | # This is Michele Simionato's decorator module, also kept verbatim. |
|
26 | # This is Michele Simionato's decorator module, also kept verbatim. | |
27 | from decorator_msim import decorator, update_wrapper |
|
27 | from decorator_msim import decorator, update_wrapper | |
28 |
|
28 | |||
29 | # Grab the numpy-specific decorators which we keep in a file that we |
|
29 | # Grab the numpy-specific decorators which we keep in a file that we | |
30 | # occasionally update from upstream: decorators_numpy.py is an IDENTICAL copy |
|
30 | # occasionally update from upstream: decorators_numpy.py is an IDENTICAL copy | |
31 | # of numpy.testing.decorators. |
|
31 | # of numpy.testing.decorators. | |
32 | from decorators_numpy import * |
|
32 | from decorators_numpy import * | |
33 |
|
33 | |||
34 | ############################################################################## |
|
34 | ############################################################################## | |
35 | # Local code begins |
|
35 | # Local code begins | |
36 |
|
36 | |||
37 | # Utility functions |
|
37 | # Utility functions | |
38 |
|
38 | |||
39 | def apply_wrapper(wrapper,func): |
|
39 | def apply_wrapper(wrapper,func): | |
40 | """Apply a wrapper to a function for decoration. |
|
40 | """Apply a wrapper to a function for decoration. | |
41 |
|
41 | |||
42 | This mixes Michele Simionato's decorator tool with nose's make_decorator, |
|
42 | This mixes Michele Simionato's decorator tool with nose's make_decorator, | |
43 | to apply a wrapper in a decorator so that all nose attributes, as well as |
|
43 | to apply a wrapper in a decorator so that all nose attributes, as well as | |
44 | function signature and other properties, survive the decoration cleanly. |
|
44 | function signature and other properties, survive the decoration cleanly. | |
45 | This will ensure that wrapped functions can still be well introspected via |
|
45 | This will ensure that wrapped functions can still be well introspected via | |
46 | IPython, for example. |
|
46 | IPython, for example. | |
47 | """ |
|
47 | """ | |
48 | import nose.tools |
|
48 | import nose.tools | |
49 |
|
49 | |||
50 | return decorator(wrapper,nose.tools.make_decorator(func)(wrapper)) |
|
50 | return decorator(wrapper,nose.tools.make_decorator(func)(wrapper)) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def make_label_dec(label,ds=None): |
|
53 | def make_label_dec(label,ds=None): | |
54 | """Factory function to create a decorator that applies one or more labels. |
|
54 | """Factory function to create a decorator that applies one or more labels. | |
55 |
|
55 | |||
56 | :Parameters: |
|
56 | :Parameters: | |
57 | label : string or sequence |
|
57 | label : string or sequence | |
58 | One or more labels that will be applied by the decorator to the functions |
|
58 | One or more labels that will be applied by the decorator to the functions | |
59 | it decorates. Labels are attributes of the decorated function with their |
|
59 | it decorates. Labels are attributes of the decorated function with their | |
60 | value set to True. |
|
60 | value set to True. | |
61 |
|
61 | |||
62 | :Keywords: |
|
62 | :Keywords: | |
63 | ds : string |
|
63 | ds : string | |
64 | An optional docstring for the resulting decorator. If not given, a |
|
64 | An optional docstring for the resulting decorator. If not given, a | |
65 | default docstring is auto-generated. |
|
65 | default docstring is auto-generated. | |
66 |
|
66 | |||
67 | :Returns: |
|
67 | :Returns: | |
68 | A decorator. |
|
68 | A decorator. | |
69 |
|
69 | |||
70 | :Examples: |
|
70 | :Examples: | |
71 |
|
71 | |||
72 | A simple labeling decorator: |
|
72 | A simple labeling decorator: | |
73 | >>> slow = make_label_dec('slow') |
|
73 | >>> slow = make_label_dec('slow') | |
74 | >>> print slow.__doc__ |
|
74 | >>> print slow.__doc__ | |
75 | Labels a test as 'slow'. |
|
75 | Labels a test as 'slow'. | |
76 |
|
76 | |||
77 | And one that uses multiple labels and a custom docstring: |
|
77 | And one that uses multiple labels and a custom docstring: | |
78 | >>> rare = make_label_dec(['slow','hard'], |
|
78 | >>> rare = make_label_dec(['slow','hard'], | |
79 | ... "Mix labels 'slow' and 'hard' for rare tests.") |
|
79 | ... "Mix labels 'slow' and 'hard' for rare tests.") | |
80 | >>> print rare.__doc__ |
|
80 | >>> print rare.__doc__ | |
81 | Mix labels 'slow' and 'hard' for rare tests. |
|
81 | Mix labels 'slow' and 'hard' for rare tests. | |
82 |
|
82 | |||
83 | Now, let's test using this one: |
|
83 | Now, let's test using this one: | |
84 | >>> @rare |
|
84 | >>> @rare | |
85 | ... def f(): pass |
|
85 | ... def f(): pass | |
86 | ... |
|
86 | ... | |
87 | >>> |
|
87 | >>> | |
88 | >>> f.slow |
|
88 | >>> f.slow | |
89 | True |
|
89 | True | |
90 | >>> f.hard |
|
90 | >>> f.hard | |
91 | True |
|
91 | True | |
92 | """ |
|
92 | """ | |
93 |
|
93 | |||
94 | if isinstance(label,basestring): |
|
94 | if isinstance(label,basestring): | |
95 | labels = [label] |
|
95 | labels = [label] | |
96 | else: |
|
96 | else: | |
97 | labels = label |
|
97 | labels = label | |
98 |
|
98 | |||
99 | # Validate that the given label(s) are OK for use in setattr() by doing a |
|
99 | # Validate that the given label(s) are OK for use in setattr() by doing a | |
100 | # dry run on a dummy function. |
|
100 | # dry run on a dummy function. | |
101 | tmp = lambda : None |
|
101 | tmp = lambda : None | |
102 | for label in labels: |
|
102 | for label in labels: | |
103 | setattr(tmp,label,True) |
|
103 | setattr(tmp,label,True) | |
104 |
|
104 | |||
105 | # This is the actual decorator we'll return |
|
105 | # This is the actual decorator we'll return | |
106 | def decor(f): |
|
106 | def decor(f): | |
107 | for label in labels: |
|
107 | for label in labels: | |
108 | setattr(f,label,True) |
|
108 | setattr(f,label,True) | |
109 | return f |
|
109 | return f | |
110 |
|
110 | |||
111 | # Apply the user's docstring, or autogenerate a basic one |
|
111 | # Apply the user's docstring, or autogenerate a basic one | |
112 | if ds is None: |
|
112 | if ds is None: | |
113 | ds = "Labels a test as %r." % label |
|
113 | ds = "Labels a test as %r." % label | |
114 | decor.__doc__ = ds |
|
114 | decor.__doc__ = ds | |
115 |
|
115 | |||
116 | return decor |
|
116 | return decor | |
117 |
|
117 | |||
118 | #----------------------------------------------------------------------------- |
|
118 | #----------------------------------------------------------------------------- | |
119 | # Decorators for public use |
|
119 | # Decorators for public use | |
120 |
|
120 | |||
121 | skip_doctest = make_label_dec('skip_doctest', |
|
121 | skip_doctest = make_label_dec('skip_doctest', | |
122 | """Decorator - mark a function or method for skipping its doctest. |
|
122 | """Decorator - mark a function or method for skipping its doctest. | |
123 |
|
123 | |||
124 | This decorator allows you to mark a function whose docstring you wish to |
|
124 | This decorator allows you to mark a function whose docstring you wish to | |
125 | omit from testing, while preserving the docstring for introspection, help, |
|
125 | omit from testing, while preserving the docstring for introspection, help, | |
126 | etc.""") |
|
126 | etc.""") | |
127 |
|
127 | |||
128 | def skip(msg=''): |
|
128 | def skip(msg=''): | |
129 | """Decorator - mark a test function for skipping from test suite. |
|
129 | """Decorator - mark a test function for skipping from test suite. | |
130 |
|
130 | |||
131 | This function *is* already a decorator, it is not a factory like |
|
131 | This function *is* already a decorator, it is not a factory like | |
132 | make_label_dec or some of those in decorators_numpy. |
|
132 | make_label_dec or some of those in decorators_numpy. | |
133 |
|
133 | |||
134 | :Parameters: |
|
134 | :Parameters: | |
135 |
|
135 | |||
136 | func : function |
|
136 | func : function | |
137 | Test function to be skipped |
|
137 | Test function to be skipped | |
138 |
|
138 | |||
139 | msg : string |
|
139 | msg : string | |
140 | Optional message to be added. |
|
140 | Optional message to be added. | |
141 | """ |
|
141 | """ | |
142 |
|
142 | |||
143 | import nose |
|
143 | import nose | |
144 |
|
144 | |||
145 | def inner(func): |
|
145 | def inner(func): | |
146 |
|
146 | |||
147 | def wrapper(*a,**k): |
|
147 | def wrapper(*a,**k): | |
148 | if msg: out = '\n'+msg |
|
148 | if msg: out = '\n'+msg | |
149 | else: out = '' |
|
149 | else: out = '' | |
150 | raise nose.SkipTest("Skipping test for function: %s%s" % |
|
150 | raise nose.SkipTest("Skipping test for function: %s%s" % | |
151 | (func.__name__,out)) |
|
151 | (func.__name__,out)) | |
152 |
|
152 | |||
153 | return apply_wrapper(wrapper,func) |
|
153 | return apply_wrapper(wrapper,func) | |
154 |
|
154 | |||
155 | return inner |
|
155 | return inner | |
156 |
|
156 | |||
157 | # Decorators to skip certain tests on specific platforms. |
|
157 | # Decorators to skip certain tests on specific platforms. | |
158 | skip_win32 = skipif(sys.platform=='win32',"This test does not run under Windows") |
|
158 | skip_win32 = skipif(sys.platform == 'win32',"This test does not run under Windows") | |
159 | skip_linux = skipif(sys.platform=='linux2',"This test does not run under Linux") |
|
159 | skip_linux = skipif(sys.platform == 'linux2',"This test does not run under Linux") | |
160 | skip_osx = skipif(sys.platform=='darwin',"This test does not run under OSX") |
|
160 | skip_osx = skipif(sys.platform == 'darwin',"This test does not run under OSX") | |
|
161 | ||||
|
162 | # Decorators to skip tests if not on specific platforms. | |||
|
163 | skip_if_not_win32 = skipif(sys.platform != 'win32', "This test only runs under Windows") | |||
|
164 | skip_if_not_linux = skipif(sys.platform != 'linux2', "This test only runs under Linux") | |||
|
165 | skip_if_not_osx = skipif(sys.platform != 'darwin', "This test only runs under OSX") |
@@ -1,227 +1,240 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Tests for genutils.py""" |
|
3 | """Tests for genutils.py""" | |
4 |
|
4 | |||
5 | __docformat__ = "restructuredtext en" |
|
5 | __docformat__ = "restructuredtext en" | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008 The IPython Development Team |
|
8 | # Copyright (C) 2008 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | from IPython import genutils |
|
18 | from IPython import genutils | |
19 | from IPython.testing.decorators import skipif |
|
19 | from IPython.testing.decorators import skipif | |
20 | from nose import with_setup |
|
20 | from nose import with_setup | |
21 | from nose.tools import raises |
|
21 | from nose.tools import raises | |
22 |
|
22 | |||
23 | from os.path import join, abspath, split |
|
23 | from os.path import join, abspath, split | |
24 | import os, sys, IPython |
|
24 | import os, sys, IPython | |
25 | import nose.tools as nt |
|
25 | import nose.tools as nt | |
26 |
|
26 | |||
27 | env = os.environ |
|
27 | env = os.environ | |
28 |
|
28 | |||
29 | try: |
|
29 | try: | |
30 | import _winreg as wreg |
|
30 | import _winreg as wreg | |
31 | except ImportError: |
|
31 | except ImportError: | |
32 | #Fake _winreg module on none windows platforms |
|
32 | #Fake _winreg module on none windows platforms | |
33 | import new |
|
33 | import new | |
34 | sys.modules["_winreg"] = new.module("_winreg") |
|
34 | sys.modules["_winreg"] = new.module("_winreg") | |
35 | import _winreg as wreg |
|
35 | import _winreg as wreg | |
36 | #Add entries that needs to be stubbed by the testing code |
|
36 | #Add entries that needs to be stubbed by the testing code | |
37 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
37 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
38 |
|
38 | |||
39 | test_file_path = split(abspath(__file__))[0] |
|
39 | test_file_path = split(abspath(__file__))[0] | |
40 |
|
40 | |||
41 | #skip_if_not_win32 = skipif(sys.platform!='win32',"This test only runs under Windows") |
|
41 | # | |
|
42 | ||||
|
43 | def setup(): | |||
|
44 | try: | |||
|
45 | os.makedirs("home_test_dir/_ipython") | |||
|
46 | except WindowsError: | |||
|
47 | pass #Or should we complain that the test directory already exists?? | |||
|
48 | ||||
|
49 | def teardown(): | |||
|
50 | try: | |||
|
51 | os.removedirs("home_test_dir/_ipython") | |||
|
52 | except WindowsError: | |||
|
53 | pass #Or should we complain that the test directory already exists?? | |||
|
54 | ||||
42 |
|
55 | |||
43 | def setup_environment(): |
|
56 | def setup_environment(): | |
44 | global oldstuff, platformstuff |
|
57 | global oldstuff, platformstuff | |
45 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) |
|
58 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) | |
46 |
|
59 | |||
47 | if os.name == 'nt': |
|
60 | if os.name == 'nt': | |
48 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
61 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | |
49 |
|
62 | |||
50 | if 'IPYTHONDIR' in env: |
|
63 | if 'IPYTHONDIR' in env: | |
51 | del env['IPYTHONDIR'] |
|
64 | del env['IPYTHONDIR'] | |
52 |
|
65 | |||
53 | def teardown_environment(): |
|
66 | def teardown_environment(): | |
54 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff |
|
67 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff | |
55 | for key in env.keys(): |
|
68 | for key in env.keys(): | |
56 | if key not in oldenv: |
|
69 | if key not in oldenv: | |
57 | del env[key] |
|
70 | del env[key] | |
58 | env.update(oldenv) |
|
71 | env.update(oldenv) | |
59 | if hasattr(sys, 'frozen'): |
|
72 | if hasattr(sys, 'frozen'): | |
60 | del sys.frozen |
|
73 | del sys.frozen | |
61 | if os.name == 'nt': |
|
74 | if os.name == 'nt': | |
62 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
75 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | |
63 |
|
76 | |||
64 | with_enivronment = with_setup(setup_environment, teardown_environment) |
|
77 | with_enivronment = with_setup(setup_environment, teardown_environment) | |
65 |
|
78 | |||
66 | @with_enivronment |
|
79 | @with_enivronment | |
67 | def test_get_home_dir_1(): |
|
80 | def test_get_home_dir_1(): | |
68 | """Testcase for py2exe logic, un-compressed lib |
|
81 | """Testcase for py2exe logic, un-compressed lib | |
69 | """ |
|
82 | """ | |
70 | sys.frozen = True |
|
83 | sys.frozen = True | |
71 |
|
84 | |||
72 | #fake filename for IPython.__init__ |
|
85 | #fake filename for IPython.__init__ | |
73 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Lib/IPython/__init__.py")) |
|
86 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Lib/IPython/__init__.py")) | |
74 |
|
87 | |||
75 | home_dir = genutils.get_home_dir() |
|
88 | home_dir = genutils.get_home_dir() | |
76 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
89 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
77 |
|
90 | |||
78 | @with_enivronment |
|
91 | @with_enivronment | |
79 | def test_get_home_dir_2(): |
|
92 | def test_get_home_dir_2(): | |
80 | """Testcase for py2exe logic, compressed lib |
|
93 | """Testcase for py2exe logic, compressed lib | |
81 | """ |
|
94 | """ | |
82 | sys.frozen = True |
|
95 | sys.frozen = True | |
83 | #fake filename for IPython.__init__ |
|
96 | #fake filename for IPython.__init__ | |
84 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Library.zip/IPython/__init__.py")) |
|
97 | IPython.__file__ = abspath(join(test_file_path, "home_test_dir/Library.zip/IPython/__init__.py")) | |
85 |
|
98 | |||
86 | home_dir = genutils.get_home_dir() |
|
99 | home_dir = genutils.get_home_dir() | |
87 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")).lower()) |
|
100 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir")).lower()) | |
88 |
|
101 | |||
89 | @with_enivronment |
|
102 | @with_enivronment | |
90 | def test_get_home_dir_3(): |
|
103 | def test_get_home_dir_3(): | |
91 | """Testcase $HOME is set, then use its value as home directory.""" |
|
104 | """Testcase $HOME is set, then use its value as home directory.""" | |
92 | env["HOME"] = join(test_file_path, "home_test_dir") |
|
105 | env["HOME"] = join(test_file_path, "home_test_dir") | |
93 | home_dir = genutils.get_home_dir() |
|
106 | home_dir = genutils.get_home_dir() | |
94 | nt.assert_equal(home_dir, env["HOME"]) |
|
107 | nt.assert_equal(home_dir, env["HOME"]) | |
95 |
|
108 | |||
96 | @with_enivronment |
|
109 | @with_enivronment | |
97 | def test_get_home_dir_4(): |
|
110 | def test_get_home_dir_4(): | |
98 | """Testcase $HOME is not set, os=='posix'. |
|
111 | """Testcase $HOME is not set, os=='posix'. | |
99 | This should fail with HomeDirError""" |
|
112 | This should fail with HomeDirError""" | |
100 |
|
113 | |||
101 | os.name = 'posix' |
|
114 | os.name = 'posix' | |
102 | del os.environ["HOME"] |
|
115 | del os.environ["HOME"] | |
103 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) |
|
116 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) | |
104 |
|
117 | |||
105 | @with_enivronment |
|
118 | @with_enivronment | |
106 | def test_get_home_dir_5(): |
|
119 | def test_get_home_dir_5(): | |
107 | """Testcase $HOME is not set, os=='nt' |
|
120 | """Testcase $HOME is not set, os=='nt' | |
108 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
121 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" | |
109 |
|
122 | |||
110 | os.name = 'nt' |
|
123 | os.name = 'nt' | |
111 | del os.environ["HOME"] |
|
124 | del os.environ["HOME"] | |
112 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "home_test_dir" |
|
125 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "home_test_dir" | |
113 |
|
126 | |||
114 | home_dir = genutils.get_home_dir() |
|
127 | home_dir = genutils.get_home_dir() | |
115 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
128 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
116 |
|
129 | |||
117 | @with_enivronment |
|
130 | @with_enivronment | |
118 | def test_get_home_dir_6(): |
|
131 | def test_get_home_dir_6(): | |
119 | """Testcase $HOME is not set, os=='nt' |
|
132 | """Testcase $HOME is not set, os=='nt' | |
120 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
133 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. | |
121 | env['USERPROFILE'] points to path |
|
134 | env['USERPROFILE'] points to path | |
122 | """ |
|
135 | """ | |
123 |
|
136 | |||
124 | os.name = 'nt' |
|
137 | os.name = 'nt' | |
125 | del os.environ["HOME"] |
|
138 | del os.environ["HOME"] | |
126 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "DOES NOT EXIST" |
|
139 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(test_file_path), "DOES NOT EXIST" | |
127 | env["USERPROFILE"] = abspath(join(test_file_path, "home_test_dir")) |
|
140 | env["USERPROFILE"] = abspath(join(test_file_path, "home_test_dir")) | |
128 |
|
141 | |||
129 | home_dir = genutils.get_home_dir() |
|
142 | home_dir = genutils.get_home_dir() | |
130 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
143 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
131 |
|
144 | |||
132 | # Should we stub wreg fully so we can run the test on all platforms? |
|
145 | # Should we stub wreg fully so we can run the test on all platforms? | |
133 | #@skip_if_not_win32 |
|
146 | #@skip_if_not_win32 | |
134 | @with_enivronment |
|
147 | @with_enivronment | |
135 | def test_get_home_dir_7(): |
|
148 | def test_get_home_dir_7(): | |
136 | """Testcase $HOME is not set, os=='nt' |
|
149 | """Testcase $HOME is not set, os=='nt' | |
137 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
150 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing | |
138 | """ |
|
151 | """ | |
139 | os.name = 'nt' |
|
152 | os.name = 'nt' | |
140 | del env["HOME"], env['HOMEDRIVE'] |
|
153 | del env["HOME"], env['HOMEDRIVE'] | |
141 |
|
154 | |||
142 | #Stub windows registry functions |
|
155 | #Stub windows registry functions | |
143 | def OpenKey(x, y): |
|
156 | def OpenKey(x, y): | |
144 | class key: |
|
157 | class key: | |
145 | def Close(self): |
|
158 | def Close(self): | |
146 | pass |
|
159 | pass | |
147 | return key() |
|
160 | return key() | |
148 | def QueryValueEx(x, y): |
|
161 | def QueryValueEx(x, y): | |
149 | return [abspath(join(test_file_path, "home_test_dir"))] |
|
162 | return [abspath(join(test_file_path, "home_test_dir"))] | |
150 |
|
163 | |||
151 | wreg.OpenKey = OpenKey |
|
164 | wreg.OpenKey = OpenKey | |
152 | wreg.QueryValueEx = QueryValueEx |
|
165 | wreg.QueryValueEx = QueryValueEx | |
153 |
|
166 | |||
154 | home_dir = genutils.get_home_dir() |
|
167 | home_dir = genutils.get_home_dir() | |
155 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) |
|
168 | nt.assert_equal(home_dir, abspath(join(test_file_path, "home_test_dir"))) | |
156 |
|
169 | |||
157 |
|
170 | |||
158 | # |
|
171 | # | |
159 | # Tests for get_ipython_dir |
|
172 | # Tests for get_ipython_dir | |
160 | # |
|
173 | # | |
161 |
|
174 | |||
162 | @with_enivronment |
|
175 | @with_enivronment | |
163 | def test_get_ipython_dir_1(): |
|
176 | def test_get_ipython_dir_1(): | |
164 | """2 Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
177 | """2 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
165 | env['IPYTHONDIR'] = "someplace/.ipython" |
|
178 | env['IPYTHONDIR'] = "someplace/.ipython" | |
166 | ipdir = genutils.get_ipython_dir() |
|
179 | ipdir = genutils.get_ipython_dir() | |
167 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) |
|
180 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) | |
168 |
|
181 | |||
169 |
|
182 | |||
170 | @with_enivronment |
|
183 | @with_enivronment | |
171 | def test_get_ipython_dir_2(): |
|
184 | def test_get_ipython_dir_2(): | |
172 | """3 Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
185 | """3 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
173 | genutils.get_home_dir = lambda : "someplace" |
|
186 | genutils.get_home_dir = lambda : "someplace" | |
174 | os.name = "posix" |
|
187 | os.name = "posix" | |
175 | ipdir = genutils.get_ipython_dir() |
|
188 | ipdir = genutils.get_ipython_dir() | |
176 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) |
|
189 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) | |
177 |
|
190 | |||
178 | @with_enivronment |
|
191 | @with_enivronment | |
179 | def test_get_ipython_dir_3(): |
|
192 | def test_get_ipython_dir_3(): | |
180 | """4 Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
193 | """4 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
181 | genutils.get_home_dir = lambda : "someplace" |
|
194 | genutils.get_home_dir = lambda : "someplace" | |
182 | os.name = "nt" |
|
195 | os.name = "nt" | |
183 | ipdir = genutils.get_ipython_dir() |
|
196 | ipdir = genutils.get_ipython_dir() | |
184 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) |
|
197 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) | |
185 |
|
198 | |||
186 |
|
199 | |||
187 | # |
|
200 | # | |
188 | # Tests for get_security_dir |
|
201 | # Tests for get_security_dir | |
189 | # |
|
202 | # | |
190 |
|
203 | |||
191 | @with_enivronment |
|
204 | @with_enivronment | |
192 | def test_get_security_dir(): |
|
205 | def test_get_security_dir(): | |
193 | """Testcase to see if we can call get_security_dir without Exceptions.""" |
|
206 | """Testcase to see if we can call get_security_dir without Exceptions.""" | |
194 | sdir = genutils.get_security_dir() |
|
207 | sdir = genutils.get_security_dir() | |
195 |
|
208 | |||
196 |
|
209 | |||
197 | # |
|
210 | # | |
198 | # Tests for popkey |
|
211 | # Tests for popkey | |
199 | # |
|
212 | # | |
200 |
|
213 | |||
201 | def test_popkey_1(): |
|
214 | def test_popkey_1(): | |
202 | dct = dict(a=1, b=2, c=3) |
|
215 | dct = dict(a=1, b=2, c=3) | |
203 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
216 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
204 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
217 | nt.assert_equal(dct, dict(b=2, c=3)) | |
205 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
218 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
206 | nt.assert_equal(dct, dict(c=3)) |
|
219 | nt.assert_equal(dct, dict(c=3)) | |
207 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
220 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
208 | nt.assert_equal(dct, dict()) |
|
221 | nt.assert_equal(dct, dict()) | |
209 |
|
222 | |||
210 | def test_popkey_2(): |
|
223 | def test_popkey_2(): | |
211 | dct = dict(a=1, b=2, c=3) |
|
224 | dct = dict(a=1, b=2, c=3) | |
212 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") |
|
225 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") | |
213 |
|
226 | |||
214 | def test_popkey_3(): |
|
227 | def test_popkey_3(): | |
215 | dct = dict(a=1, b=2, c=3) |
|
228 | dct = dict(a=1, b=2, c=3) | |
216 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) |
|
229 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) | |
217 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
230 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
218 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) |
|
231 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) | |
219 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
232 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
220 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) |
|
233 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) | |
221 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
234 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
222 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
235 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
223 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
236 | nt.assert_equal(dct, dict(b=2, c=3)) | |
224 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
237 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
225 | nt.assert_equal(dct, dict(c=3)) |
|
238 | nt.assert_equal(dct, dict(c=3)) | |
226 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
239 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
227 | nt.assert_equal(dct, dict()) |
|
240 | nt.assert_equal(dct, dict()) |
General Comments 0
You need to be logged in to leave comments.
Login now