Show More
@@ -16,61 +16,82 b' __docformat__ = "restructuredtext en"' | |||
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | from IPython import genutils |
|
19 | ||
|
19 | from IPython.testing.decorators import skipif | |
|
20 | from nose import with_setup | |
|
20 | 21 | import os, sys, IPython |
|
21 | 22 | env = os.environ |
|
22 | 23 | |
|
23 | 24 | from os.path import join, abspath |
|
24 | 25 | |
|
26 | try: | |
|
27 | import _winreg as wreg | |
|
28 | except ImportError: | |
|
29 | pass | |
|
30 | ||
|
31 | skip_if_not_win32 = skipif(sys.platform!='win32',"This test only runs under Windows") | |
|
32 | ||
|
33 | def setup_environment(): | |
|
34 | global oldstuff, platformstuff | |
|
35 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) | |
|
36 | ||
|
37 | if os.name=='nt': | |
|
38 | platformstuff=(wreg.OpenKey, wreg.QueryValueEx,) | |
|
39 | ||
|
40 | if 'IPYTHONDIR' in env: | |
|
41 | del env['IPYTHONDIR'] | |
|
42 | ||
|
43 | def teardown_environment(): | |
|
44 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff | |
|
45 | for key in env.keys(): | |
|
46 | if key not in oldenv: | |
|
47 | del env[key] | |
|
48 | env.update(oldenv) | |
|
49 | if hasattr(sys, 'frozen'): | |
|
50 | del sys.frozen | |
|
51 | if os.name=='nt': | |
|
52 | (wreg.OpenKey, wreg.QueryValueEx,)=platformstuff | |
|
53 | ||
|
54 | with_enivronment=with_setup(setup_environment, teardown_environment) | |
|
55 | ||
|
56 | @with_enivronment | |
|
25 | 57 | def test_get_home_dir_1(): |
|
26 | 58 | """Testcase to see if we can call get_home_dir without Exceptions.""" |
|
27 | 59 | home_dir = genutils.get_home_dir() |
|
28 | 60 | |
|
61 | @with_enivronment | |
|
29 | 62 | def test_get_home_dir_2(): |
|
30 | 63 | """Testcase for py2exe logic, un-compressed lib |
|
31 | 64 | """ |
|
32 | 65 | sys.frozen=True |
|
33 | oldstuff=IPython.__file__ | |
|
34 | 66 | |
|
35 | 67 | #fake filename for IPython.__init__ |
|
36 | 68 | IPython.__file__=abspath(join(".", "home_test_dir/Lib/IPython/__init__.py")) |
|
37 | 69 | |
|
38 | 70 | home_dir = genutils.get_home_dir() |
|
39 | 71 | assert home_dir==abspath(join(".", "home_test_dir")) |
|
40 | IPython.__file__=oldstuff | |
|
41 | del sys.frozen | |
|
42 | 72 | |
|
73 | @with_enivronment | |
|
43 | 74 | def test_get_home_dir_3(): |
|
44 | 75 | """Testcase for py2exe logic, compressed lib |
|
45 | 76 | """ |
|
46 | ||
|
47 | 77 | sys.frozen=True |
|
48 | oldstuff=IPython.__file__ | |
|
49 | ||
|
50 | 78 | #fake filename for IPython.__init__ |
|
51 | 79 | IPython.__file__=abspath(join(".", "home_test_dir/Library.zip/IPython/__init__.py")) |
|
52 | 80 | |
|
53 | 81 | home_dir = genutils.get_home_dir() |
|
54 | 82 | assert home_dir==abspath(join(".", "home_test_dir")).lower() |
|
55 | 83 | |
|
56 | del sys.frozen | |
|
57 | IPython.__file__=oldstuff | |
|
58 | ||
|
59 | ||
|
84 | @with_enivronment | |
|
60 | 85 | def test_get_home_dir_4(): |
|
61 | 86 | """Testcase $HOME is set, then use its value as home directory.""" |
|
62 | oldstuff=env["HOME"] | |
|
63 | ||
|
64 | 87 | env["HOME"]=join(".","home_test_dir") |
|
65 | 88 | home_dir = genutils.get_home_dir() |
|
66 | 89 | assert home_dir==env["HOME"] |
|
67 | ||
|
68 | env["HOME"]=oldstuff | |
|
69 | 90 | |
|
91 | @with_enivronment | |
|
70 | 92 | def test_get_home_dir_5(): |
|
71 | 93 | """Testcase $HOME is not set, os=='posix'. |
|
72 | 94 | This should fail with HomeDirError""" |
|
73 | oldstuff=env["HOME"],os.name | |
|
74 | 95 | |
|
75 | 96 | os.name='posix' |
|
76 | 97 | del os.environ["HOME"] |
@@ -79,31 +100,26 b' def test_get_home_dir_5():' | |||
|
79 | 100 | assert False |
|
80 | 101 | except genutils.HomeDirError: |
|
81 | 102 | pass |
|
82 | finally: | |
|
83 | env["HOME"],os.name=oldstuff | |
|
84 | 103 | |
|
104 | @with_enivronment | |
|
85 | 105 | def test_get_home_dir_6(): |
|
86 | 106 | """Testcase $HOME is not set, os=='nt' |
|
87 | 107 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
88 | 108 | |
|
89 | oldstuff=env["HOME"],os.name,env['HOMEDRIVE'],env['HOMEPATH'] | |
|
90 | ||
|
91 | 109 | os.name='nt' |
|
92 | 110 | del os.environ["HOME"] |
|
93 | 111 | env['HOMEDRIVE'],env['HOMEPATH']=os.path.abspath("."),"home_test_dir" |
|
94 | 112 | |
|
95 | 113 | home_dir = genutils.get_home_dir() |
|
96 | 114 | assert home_dir==abspath(join(".", "home_test_dir")) |
|
97 | ||
|
98 | env["HOME"],os.name,env['HOMEDRIVE'],env['HOMEPATH']=oldstuff | |
|
99 | 115 | |
|
116 | @with_enivronment | |
|
100 | 117 | def test_get_home_dir_8(): |
|
101 | 118 | """Testcase $HOME is not set, os=='nt' |
|
102 | 119 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
103 | 120 | env['USERPROFILE'] points to path |
|
104 | 121 | """ |
|
105 | oldstuff=(env["HOME"],os.name,env['HOMEDRIVE'],env['HOMEPATH']) | |
|
106 | ||
|
122 | ||
|
107 | 123 | os.name='nt' |
|
108 | 124 | del os.environ["HOME"] |
|
109 | 125 | env['HOMEDRIVE'],env['HOMEPATH']=os.path.abspath("."),"DOES NOT EXIST" |
@@ -111,18 +127,16 b' def test_get_home_dir_8():' | |||
|
111 | 127 | |
|
112 | 128 | home_dir = genutils.get_home_dir() |
|
113 | 129 | assert home_dir==abspath(join(".", "home_test_dir")) |
|
114 | ||
|
115 | (env["HOME"],os.name,env['HOMEDRIVE'],env['HOMEPATH'])=oldstuff | |
|
116 | 130 | |
|
131 | ||
|
132 | ||
|
133 | # Should we stub wreg fully so we can run the test on all platforms? | |
|
134 | @skip_if_not_win32 | |
|
135 | @with_enivronment | |
|
117 | 136 | def test_get_home_dir_9(): |
|
118 | 137 | """Testcase $HOME is not set, os=='nt' |
|
119 | 138 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
120 | 139 | """ |
|
121 | import _winreg as wreg | |
|
122 | oldstuff = (env["HOME"],os.name,env['HOMEDRIVE'], | |
|
123 | env['HOMEPATH'],env["USERPROFILE"], | |
|
124 | wreg.OpenKey, wreg.QueryValueEx, | |
|
125 | ) | |
|
126 | 140 | os.name='nt' |
|
127 | 141 | del env["HOME"],env['HOMEDRIVE'] |
|
128 | 142 | |
@@ -138,67 +152,51 b' def test_get_home_dir_9():' | |||
|
138 | 152 | wreg.OpenKey=OpenKey |
|
139 | 153 | wreg.QueryValueEx=QueryValueEx |
|
140 | 154 | |
|
141 | ||
|
142 | 155 | home_dir = genutils.get_home_dir() |
|
143 | 156 | assert home_dir==abspath(join(".", "home_test_dir")) |
|
144 | 157 | |
|
145 | (env["HOME"],os.name,env['HOMEDRIVE'], | |
|
146 | env['HOMEPATH'],env["USERPROFILE"], | |
|
147 | wreg.OpenKey, wreg.QueryValueEx,) = oldstuff | |
|
148 | ||
|
149 | 158 | |
|
159 | # | |
|
160 | # Tests for get_ipython_dir | |
|
161 | # | |
|
162 | ||
|
163 | @with_enivronment | |
|
150 | 164 | def test_get_ipython_dir_1(): |
|
151 | """Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
165 | """1 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
152 | 166 | ipdir = genutils.get_ipython_dir() |
|
153 | 167 | |
|
154 | def test_get_ipython_dir_2(): | |
|
155 | """Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
156 | oldstuff = (env['IPYTHONDIR'],) | |
|
157 | 168 | |
|
169 | @with_enivronment | |
|
170 | def test_get_ipython_dir_2(): | |
|
171 | """2 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
158 | 172 | env['IPYTHONDIR']="someplace/.ipython" |
|
159 | 173 | ipdir = genutils.get_ipython_dir() |
|
160 | 174 | assert ipdir == os.path.abspath("someplace/.ipython") |
|
161 | 175 | |
|
162 | (env['IPYTHONDIR'],)=oldstuff | |
|
163 | 176 | |
|
164 | class test_get_ipython_dir_3: | |
|
165 | @classmethod | |
|
166 | def setup_class(cls): | |
|
167 | cls.oldstuff = (env['IPYTHONDIR'], os.name, genutils.get_home_dir) | |
|
168 | del env['IPYTHONDIR'] | |
|
169 | genutils.get_home_dir=lambda : "someplace" | |
|
177 | @with_enivronment | |
|
178 | def test_get_ipython_dir_3(): | |
|
179 | """3 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
180 | genutils.get_home_dir=lambda : "someplace" | |
|
181 | os.name="posix" | |
|
182 | ipdir = genutils.get_ipython_dir() | |
|
183 | assert ipdir == os.path.abspath(os.path.join("someplace", ".ipython")) | |
|
170 | 184 | |
|
171 | @classmethod | |
|
172 | def teardown_class(cls): | |
|
173 | (env['IPYTHONDIR'], os.name, genutils.get_home_dir)=cls.oldstuff | |
|
174 | ||
|
175 | def test_get_ipython_dir_a(self): | |
|
176 | """Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
185 | @with_enivronment | |
|
186 | def test_get_ipython_dir_4(): | |
|
187 | """4 Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
188 | genutils.get_home_dir=lambda : "someplace" | |
|
189 | os.name="nt" | |
|
190 | ipdir = genutils.get_ipython_dir() | |
|
191 | assert ipdir == os.path.abspath(os.path.join("someplace", "_ipython")) | |
|
177 | 192 | |
|
178 | os.name="posix" | |
|
179 | ipdir = genutils.get_ipython_dir() | |
|
180 | assert ipdir == os.path.abspath(os.path.join("someplace", ".ipython")) | |
|
181 | ||
|
182 | def test_get_ipython_dir_b(self): | |
|
183 | """Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
|
184 | 193 | |
|
185 | os.name="nt" | |
|
186 | ipdir = genutils.get_ipython_dir() | |
|
187 | assert ipdir == os.path.abspath(os.path.join("someplace", "_ipython")) | |
|
188 | ||
|
189 | class test_get_security_dir: | |
|
190 | @classmethod | |
|
191 | def setup_class(cls): | |
|
192 | cls.oldstuff = (env['IPYTHONDIR'], os.name, genutils.get_home_dir) | |
|
193 | del env['IPYTHONDIR'] | |
|
194 | genutils.get_home_dir=lambda : "someplace" | |
|
195 | 194 | |
|
196 | @classmethod | |
|
197 | def teardown_class(cls): | |
|
198 | (env['IPYTHONDIR'], os.name, genutils.get_home_dir)=cls.oldstuff | |
|
199 | ||
|
195 | # | |
|
196 | # Tests for get_security_dir | |
|
197 | # | |
|
200 | 198 | |
|
201 | def test_get_security_dir(): | |
|
202 | """Testcase to see if we can call get_security_dir without Exceptions.""" | |
|
203 | sdir = genutils.get_security_dir() | |
|
204 | No newline at end of file | |
|
199 | @with_enivronment | |
|
200 | def test_get_security_dir(): | |
|
201 | """Testcase to see if we can call get_security_dir without Exceptions.""" | |
|
202 | sdir = genutils.get_security_dir() |
General Comments 0
You need to be logged in to leave comments.
Login now