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