Show More
@@ -1,631 +1,635 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for IPython.utils.path.py""" |
|
2 | """Tests for IPython.utils.path.py""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
5 | # Copyright (C) 2008-2011 The IPython Development Team | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
8 | # the file COPYING, distributed as part of this software. |
|
8 | # the file COPYING, distributed as part of this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | from __future__ import with_statement |
|
15 | from __future__ import with_statement | |
16 |
|
16 | |||
17 | import os |
|
17 | import os | |
18 | import shutil |
|
18 | import shutil | |
19 | import sys |
|
19 | import sys | |
20 | import tempfile |
|
20 | import tempfile | |
21 | from contextlib import contextmanager |
|
21 | from contextlib import contextmanager | |
22 |
|
22 | |||
23 | from os.path import join, abspath, split |
|
23 | from os.path import join, abspath, split | |
24 |
|
24 | |||
25 | import nose.tools as nt |
|
25 | import nose.tools as nt | |
26 |
|
26 | |||
27 | from nose import with_setup |
|
27 | from nose import with_setup | |
28 |
|
28 | |||
29 | import IPython |
|
29 | import IPython | |
30 | from IPython.testing import decorators as dec |
|
30 | from IPython.testing import decorators as dec | |
31 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
31 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 | |
32 | from IPython.testing.tools import make_tempfile, AssertPrints |
|
32 | from IPython.testing.tools import make_tempfile, AssertPrints | |
33 | from IPython.utils import path |
|
33 | from IPython.utils import path | |
34 | from IPython.utils import py3compat |
|
34 | from IPython.utils import py3compat | |
35 | from IPython.utils.tempdir import TemporaryDirectory |
|
35 | from IPython.utils.tempdir import TemporaryDirectory | |
36 |
|
36 | |||
37 | # Platform-dependent imports |
|
37 | # Platform-dependent imports | |
38 | try: |
|
38 | try: | |
39 | import _winreg as wreg |
|
39 | import _winreg as wreg | |
40 | except ImportError: |
|
40 | except ImportError: | |
41 | #Fake _winreg module on none windows platforms |
|
41 | #Fake _winreg module on none windows platforms | |
42 | import types |
|
42 | import types | |
43 | wr_name = "winreg" if py3compat.PY3 else "_winreg" |
|
43 | wr_name = "winreg" if py3compat.PY3 else "_winreg" | |
44 | sys.modules[wr_name] = types.ModuleType(wr_name) |
|
44 | sys.modules[wr_name] = types.ModuleType(wr_name) | |
45 | import _winreg as wreg |
|
45 | import _winreg as wreg | |
46 | #Add entries that needs to be stubbed by the testing code |
|
46 | #Add entries that needs to be stubbed by the testing code | |
47 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
47 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
48 |
|
48 | |||
49 | try: |
|
49 | try: | |
50 | reload |
|
50 | reload | |
51 | except NameError: # Python 3 |
|
51 | except NameError: # Python 3 | |
52 | from imp import reload |
|
52 | from imp import reload | |
53 |
|
53 | |||
54 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- | |
55 | # Globals |
|
55 | # Globals | |
56 | #----------------------------------------------------------------------------- |
|
56 | #----------------------------------------------------------------------------- | |
57 | env = os.environ |
|
57 | env = os.environ | |
58 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
58 | TEST_FILE_PATH = split(abspath(__file__))[0] | |
59 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
59 | TMP_TEST_DIR = tempfile.mkdtemp() | |
60 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
60 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | |
61 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") |
|
61 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") | |
62 | XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir") |
|
62 | XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir") | |
63 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') |
|
63 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') | |
64 | # |
|
64 | # | |
65 | # Setup/teardown functions/decorators |
|
65 | # Setup/teardown functions/decorators | |
66 | # |
|
66 | # | |
67 |
|
67 | |||
68 | def setup(): |
|
68 | def setup(): | |
69 | """Setup testenvironment for the module: |
|
69 | """Setup testenvironment for the module: | |
70 |
|
70 | |||
71 | - Adds dummy home dir tree |
|
71 | - Adds dummy home dir tree | |
72 | """ |
|
72 | """ | |
73 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
73 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
74 | # problem because that exception is only defined on Windows... |
|
74 | # problem because that exception is only defined on Windows... | |
75 | os.makedirs(IP_TEST_DIR) |
|
75 | os.makedirs(IP_TEST_DIR) | |
76 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
76 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | |
77 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) |
|
77 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | def teardown(): |
|
80 | def teardown(): | |
81 | """Teardown testenvironment for the module: |
|
81 | """Teardown testenvironment for the module: | |
82 |
|
82 | |||
83 | - Remove dummy home dir tree |
|
83 | - Remove dummy home dir tree | |
84 | """ |
|
84 | """ | |
85 | # Note: we remove the parent test dir, which is the root of all test |
|
85 | # Note: we remove the parent test dir, which is the root of all test | |
86 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
86 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
87 | # that non-empty directories are all recursively removed. |
|
87 | # that non-empty directories are all recursively removed. | |
88 | shutil.rmtree(TMP_TEST_DIR) |
|
88 | shutil.rmtree(TMP_TEST_DIR) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def setup_environment(): |
|
91 | def setup_environment(): | |
92 | """Setup testenvironment for some functions that are tested |
|
92 | """Setup testenvironment for some functions that are tested | |
93 | in this module. In particular this functions stores attributes |
|
93 | in this module. In particular this functions stores attributes | |
94 | and other things that we need to stub in some test functions. |
|
94 | and other things that we need to stub in some test functions. | |
95 | This needs to be done on a function level and not module level because |
|
95 | This needs to be done on a function level and not module level because | |
96 | each testfunction needs a pristine environment. |
|
96 | each testfunction needs a pristine environment. | |
97 | """ |
|
97 | """ | |
98 | global oldstuff, platformstuff |
|
98 | global oldstuff, platformstuff | |
99 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) |
|
99 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) | |
100 |
|
100 | |||
101 | if os.name == 'nt': |
|
101 | if os.name == 'nt': | |
102 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
102 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | def teardown_environment(): |
|
105 | def teardown_environment(): | |
106 | """Restore things that were remembered by the setup_environment function |
|
106 | """Restore things that were remembered by the setup_environment function | |
107 | """ |
|
107 | """ | |
108 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff |
|
108 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff | |
109 | os.chdir(old_wd) |
|
109 | os.chdir(old_wd) | |
110 | reload(path) |
|
110 | reload(path) | |
111 |
|
111 | |||
112 | for key in env.keys(): |
|
112 | for key in env.keys(): | |
113 | if key not in oldenv: |
|
113 | if key not in oldenv: | |
114 | del env[key] |
|
114 | del env[key] | |
115 | env.update(oldenv) |
|
115 | env.update(oldenv) | |
116 | if hasattr(sys, 'frozen'): |
|
116 | if hasattr(sys, 'frozen'): | |
117 | del sys.frozen |
|
117 | del sys.frozen | |
118 | if os.name == 'nt': |
|
118 | if os.name == 'nt': | |
119 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
119 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | |
120 |
|
120 | |||
121 | # Build decorator that uses the setup_environment/setup_environment |
|
121 | # Build decorator that uses the setup_environment/setup_environment | |
122 | with_environment = with_setup(setup_environment, teardown_environment) |
|
122 | with_environment = with_setup(setup_environment, teardown_environment) | |
123 |
|
123 | |||
124 | @skip_if_not_win32 |
|
124 | @skip_if_not_win32 | |
125 | @with_environment |
|
125 | @with_environment | |
126 | def test_get_home_dir_1(): |
|
126 | def test_get_home_dir_1(): | |
127 | """Testcase for py2exe logic, un-compressed lib |
|
127 | """Testcase for py2exe logic, un-compressed lib | |
128 | """ |
|
128 | """ | |
129 | unfrozen = path.get_home_dir() |
|
129 | unfrozen = path.get_home_dir() | |
130 | sys.frozen = True |
|
130 | sys.frozen = True | |
131 |
|
131 | |||
132 | #fake filename for IPython.__init__ |
|
132 | #fake filename for IPython.__init__ | |
133 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
133 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | |
134 |
|
134 | |||
135 | home_dir = path.get_home_dir() |
|
135 | home_dir = path.get_home_dir() | |
136 | nt.assert_equal(home_dir, unfrozen) |
|
136 | nt.assert_equal(home_dir, unfrozen) | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | @skip_if_not_win32 |
|
139 | @skip_if_not_win32 | |
140 | @with_environment |
|
140 | @with_environment | |
141 | def test_get_home_dir_2(): |
|
141 | def test_get_home_dir_2(): | |
142 | """Testcase for py2exe logic, compressed lib |
|
142 | """Testcase for py2exe logic, compressed lib | |
143 | """ |
|
143 | """ | |
144 | unfrozen = path.get_home_dir() |
|
144 | unfrozen = path.get_home_dir() | |
145 | sys.frozen = True |
|
145 | sys.frozen = True | |
146 | #fake filename for IPython.__init__ |
|
146 | #fake filename for IPython.__init__ | |
147 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
147 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | |
148 |
|
148 | |||
149 | home_dir = path.get_home_dir(True) |
|
149 | home_dir = path.get_home_dir(True) | |
150 | nt.assert_equal(home_dir, unfrozen) |
|
150 | nt.assert_equal(home_dir, unfrozen) | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | @with_environment |
|
153 | @with_environment | |
154 | def test_get_home_dir_3(): |
|
154 | def test_get_home_dir_3(): | |
155 | """get_home_dir() uses $HOME if set""" |
|
155 | """get_home_dir() uses $HOME if set""" | |
156 | env["HOME"] = HOME_TEST_DIR |
|
156 | env["HOME"] = HOME_TEST_DIR | |
157 | home_dir = path.get_home_dir(True) |
|
157 | home_dir = path.get_home_dir(True) | |
158 | # get_home_dir expands symlinks |
|
158 | # get_home_dir expands symlinks | |
159 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) |
|
159 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | @with_environment |
|
162 | @with_environment | |
163 | def test_get_home_dir_4(): |
|
163 | def test_get_home_dir_4(): | |
164 | """get_home_dir() still works if $HOME is not set""" |
|
164 | """get_home_dir() still works if $HOME is not set""" | |
165 |
|
165 | |||
166 | if 'HOME' in env: del env['HOME'] |
|
166 | if 'HOME' in env: del env['HOME'] | |
167 | # this should still succeed, but we don't care what the answer is |
|
167 | # this should still succeed, but we don't care what the answer is | |
168 | home = path.get_home_dir(False) |
|
168 | home = path.get_home_dir(False) | |
169 |
|
169 | |||
170 | @with_environment |
|
170 | @with_environment | |
171 | def test_get_home_dir_5(): |
|
171 | def test_get_home_dir_5(): | |
172 | """raise HomeDirError if $HOME is specified, but not a writable dir""" |
|
172 | """raise HomeDirError if $HOME is specified, but not a writable dir""" | |
173 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') |
|
173 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') | |
174 | # set os.name = posix, to prevent My Documents fallback on Windows |
|
174 | # set os.name = posix, to prevent My Documents fallback on Windows | |
175 | os.name = 'posix' |
|
175 | os.name = 'posix' | |
176 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) |
|
176 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | # Should we stub wreg fully so we can run the test on all platforms? |
|
179 | # Should we stub wreg fully so we can run the test on all platforms? | |
180 | @skip_if_not_win32 |
|
180 | @skip_if_not_win32 | |
181 | @with_environment |
|
181 | @with_environment | |
182 | def test_get_home_dir_8(): |
|
182 | def test_get_home_dir_8(): | |
183 | """Using registry hack for 'My Documents', os=='nt' |
|
183 | """Using registry hack for 'My Documents', os=='nt' | |
184 |
|
184 | |||
185 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. |
|
185 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. | |
186 | """ |
|
186 | """ | |
187 | os.name = 'nt' |
|
187 | os.name = 'nt' | |
188 | # Remove from stub environment all keys that may be set |
|
188 | # Remove from stub environment all keys that may be set | |
189 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: |
|
189 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: | |
190 | env.pop(key, None) |
|
190 | env.pop(key, None) | |
191 |
|
191 | |||
192 | #Stub windows registry functions |
|
192 | #Stub windows registry functions | |
193 | def OpenKey(x, y): |
|
193 | def OpenKey(x, y): | |
194 | class key: |
|
194 | class key: | |
195 | def Close(self): |
|
195 | def Close(self): | |
196 | pass |
|
196 | pass | |
197 | return key() |
|
197 | return key() | |
198 | def QueryValueEx(x, y): |
|
198 | def QueryValueEx(x, y): | |
199 | return [abspath(HOME_TEST_DIR)] |
|
199 | return [abspath(HOME_TEST_DIR)] | |
200 |
|
200 | |||
201 | wreg.OpenKey = OpenKey |
|
201 | wreg.OpenKey = OpenKey | |
202 | wreg.QueryValueEx = QueryValueEx |
|
202 | wreg.QueryValueEx = QueryValueEx | |
203 |
|
203 | |||
204 | home_dir = path.get_home_dir() |
|
204 | home_dir = path.get_home_dir() | |
205 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
205 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
206 |
|
206 | |||
207 |
|
207 | |||
208 | @with_environment |
|
208 | @with_environment | |
209 | def test_get_ipython_dir_1(): |
|
209 | def test_get_ipython_dir_1(): | |
210 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
210 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
211 | env_ipdir = os.path.join("someplace", ".ipython") |
|
211 | env_ipdir = os.path.join("someplace", ".ipython") | |
212 | path._writable_dir = lambda path: True |
|
212 | path._writable_dir = lambda path: True | |
213 | env['IPYTHONDIR'] = env_ipdir |
|
213 | env['IPYTHONDIR'] = env_ipdir | |
214 | ipdir = path.get_ipython_dir() |
|
214 | ipdir = path.get_ipython_dir() | |
215 | nt.assert_equal(ipdir, env_ipdir) |
|
215 | nt.assert_equal(ipdir, env_ipdir) | |
216 |
|
216 | |||
217 |
|
217 | |||
218 | @with_environment |
|
218 | @with_environment | |
219 | def test_get_ipython_dir_2(): |
|
219 | def test_get_ipython_dir_2(): | |
220 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
220 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
221 | path.get_home_dir = lambda : "someplace" |
|
221 | path.get_home_dir = lambda : "someplace" | |
222 | path.get_xdg_dir = lambda : None |
|
222 | path.get_xdg_dir = lambda : None | |
223 | path._writable_dir = lambda path: True |
|
223 | path._writable_dir = lambda path: True | |
224 | os.name = "posix" |
|
224 | os.name = "posix" | |
225 | env.pop('IPYTHON_DIR', None) |
|
225 | env.pop('IPYTHON_DIR', None) | |
226 | env.pop('IPYTHONDIR', None) |
|
226 | env.pop('IPYTHONDIR', None) | |
227 | env.pop('XDG_CONFIG_HOME', None) |
|
227 | env.pop('XDG_CONFIG_HOME', None) | |
228 | ipdir = path.get_ipython_dir() |
|
228 | ipdir = path.get_ipython_dir() | |
229 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
229 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | |
230 |
|
230 | |||
231 | @with_environment |
|
231 | @with_environment | |
232 | def test_get_ipython_dir_3(): |
|
232 | def test_get_ipython_dir_3(): | |
233 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
233 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" | |
234 | path.get_home_dir = lambda : "someplace" |
|
234 | path.get_home_dir = lambda : "someplace" | |
235 | path._writable_dir = lambda path: True |
|
235 | path._writable_dir = lambda path: True | |
236 | os.name = "posix" |
|
236 | os.name = "posix" | |
237 | env.pop('IPYTHON_DIR', None) |
|
237 | env.pop('IPYTHON_DIR', None) | |
238 | env.pop('IPYTHONDIR', None) |
|
238 | env.pop('IPYTHONDIR', None) | |
239 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
239 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
240 | ipdir = path.get_ipython_dir() |
|
240 | ipdir = path.get_ipython_dir() | |
241 | if sys.platform == "darwin": |
|
241 | if sys.platform == "darwin": | |
242 | expected = os.path.join("someplace", ".ipython") |
|
242 | expected = os.path.join("someplace", ".ipython") | |
243 | else: |
|
243 | else: | |
244 | expected = os.path.join(XDG_TEST_DIR, "ipython") |
|
244 | expected = os.path.join(XDG_TEST_DIR, "ipython") | |
245 | nt.assert_equal(ipdir, expected) |
|
245 | nt.assert_equal(ipdir, expected) | |
246 |
|
246 | |||
247 | @with_environment |
|
247 | @with_environment | |
248 | def test_get_ipython_dir_4(): |
|
248 | def test_get_ipython_dir_4(): | |
249 | """test_get_ipython_dir_4, use XDG if both exist.""" |
|
249 | """test_get_ipython_dir_4, use XDG if both exist.""" | |
250 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
250 | path.get_home_dir = lambda : HOME_TEST_DIR | |
251 | os.name = "posix" |
|
251 | os.name = "posix" | |
252 | env.pop('IPYTHON_DIR', None) |
|
252 | env.pop('IPYTHON_DIR', None) | |
253 | env.pop('IPYTHONDIR', None) |
|
253 | env.pop('IPYTHONDIR', None) | |
254 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
254 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
255 | ipdir = path.get_ipython_dir() |
|
255 | ipdir = path.get_ipython_dir() | |
256 | if sys.platform == "darwin": |
|
256 | if sys.platform == "darwin": | |
257 | expected = os.path.join(HOME_TEST_DIR, ".ipython") |
|
257 | expected = os.path.join(HOME_TEST_DIR, ".ipython") | |
258 | else: |
|
258 | else: | |
259 | expected = os.path.join(XDG_TEST_DIR, "ipython") |
|
259 | expected = os.path.join(XDG_TEST_DIR, "ipython") | |
260 | nt.assert_equal(ipdir, expected) |
|
260 | nt.assert_equal(ipdir, expected) | |
261 |
|
261 | |||
262 | @with_environment |
|
262 | @with_environment | |
263 | def test_get_ipython_dir_5(): |
|
263 | def test_get_ipython_dir_5(): | |
264 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
264 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | |
265 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
265 | path.get_home_dir = lambda : HOME_TEST_DIR | |
266 | os.name = "posix" |
|
266 | os.name = "posix" | |
267 | env.pop('IPYTHON_DIR', None) |
|
267 | env.pop('IPYTHON_DIR', None) | |
268 | env.pop('IPYTHONDIR', None) |
|
268 | env.pop('IPYTHONDIR', None) | |
269 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR |
|
269 | env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | |
270 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
270 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
271 | ipdir = path.get_ipython_dir() |
|
271 | ipdir = path.get_ipython_dir() | |
272 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
272 | nt.assert_equal(ipdir, IP_TEST_DIR) | |
273 |
|
273 | |||
274 | @with_environment |
|
274 | @with_environment | |
275 | def test_get_ipython_dir_6(): |
|
275 | def test_get_ipython_dir_6(): | |
276 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
276 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" | |
277 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
277 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | |
278 | os.mkdir(xdg) |
|
278 | os.mkdir(xdg) | |
279 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
279 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | |
280 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
280 | path.get_home_dir = lambda : HOME_TEST_DIR | |
281 | path.get_xdg_dir = lambda : xdg |
|
281 | path.get_xdg_dir = lambda : xdg | |
282 | os.name = "posix" |
|
282 | os.name = "posix" | |
283 | env.pop('IPYTHON_DIR', None) |
|
283 | env.pop('IPYTHON_DIR', None) | |
284 | env.pop('IPYTHONDIR', None) |
|
284 | env.pop('IPYTHONDIR', None) | |
285 | env.pop('XDG_CONFIG_HOME', None) |
|
285 | env.pop('XDG_CONFIG_HOME', None) | |
286 | xdg_ipdir = os.path.join(xdg, "ipython") |
|
286 | xdg_ipdir = os.path.join(xdg, "ipython") | |
287 | ipdir = path.get_ipython_dir() |
|
287 | ipdir = path.get_ipython_dir() | |
288 | nt.assert_equal(ipdir, xdg_ipdir) |
|
288 | nt.assert_equal(ipdir, xdg_ipdir) | |
289 |
|
289 | |||
290 | @with_environment |
|
290 | @with_environment | |
291 | def test_get_ipython_dir_7(): |
|
291 | def test_get_ipython_dir_7(): | |
292 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" |
|
292 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" | |
293 | path._writable_dir = lambda path: True |
|
293 | path._writable_dir = lambda path: True | |
294 | home_dir = os.path.normpath(os.path.expanduser('~')) |
|
294 | home_dir = os.path.normpath(os.path.expanduser('~')) | |
295 | env['IPYTHONDIR'] = os.path.join('~', 'somewhere') |
|
295 | env['IPYTHONDIR'] = os.path.join('~', 'somewhere') | |
296 | ipdir = path.get_ipython_dir() |
|
296 | ipdir = path.get_ipython_dir() | |
297 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
297 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) | |
298 |
|
298 | |||
299 | @skip_win32 |
|
299 | @skip_win32 | |
300 | @with_environment |
|
300 | @with_environment | |
301 | def test_get_ipython_dir_8(): |
|
301 | def test_get_ipython_dir_8(): | |
302 | """test_get_ipython_dir_8, test / home directory""" |
|
302 | """test_get_ipython_dir_8, test / home directory""" | |
303 | old = path._writable_dir, path.get_xdg_dir |
|
303 | old = path._writable_dir, path.get_xdg_dir | |
304 | try: |
|
304 | try: | |
305 | path._writable_dir = lambda path: bool(path) |
|
305 | path._writable_dir = lambda path: bool(path) | |
306 | path.get_xdg_dir = lambda: None |
|
306 | path.get_xdg_dir = lambda: None | |
307 | env.pop('IPYTHON_DIR', None) |
|
307 | env.pop('IPYTHON_DIR', None) | |
308 | env.pop('IPYTHONDIR', None) |
|
308 | env.pop('IPYTHONDIR', None) | |
309 | env['HOME'] = '/' |
|
309 | env['HOME'] = '/' | |
310 | nt.assert_equal(path.get_ipython_dir(), '/.ipython') |
|
310 | nt.assert_equal(path.get_ipython_dir(), '/.ipython') | |
311 | finally: |
|
311 | finally: | |
312 | path._writable_dir, path.get_xdg_dir = old |
|
312 | path._writable_dir, path.get_xdg_dir = old | |
313 |
|
313 | |||
314 | @with_environment |
|
314 | @with_environment | |
315 | def test_get_xdg_dir_0(): |
|
315 | def test_get_xdg_dir_0(): | |
316 | """test_get_xdg_dir_0, check xdg_dir""" |
|
316 | """test_get_xdg_dir_0, check xdg_dir""" | |
317 | reload(path) |
|
317 | reload(path) | |
318 | path._writable_dir = lambda path: True |
|
318 | path._writable_dir = lambda path: True | |
319 | path.get_home_dir = lambda : 'somewhere' |
|
319 | path.get_home_dir = lambda : 'somewhere' | |
320 | os.name = "posix" |
|
320 | os.name = "posix" | |
321 | sys.platform = "linux2" |
|
321 | sys.platform = "linux2" | |
322 | env.pop('IPYTHON_DIR', None) |
|
322 | env.pop('IPYTHON_DIR', None) | |
323 | env.pop('IPYTHONDIR', None) |
|
323 | env.pop('IPYTHONDIR', None) | |
324 | env.pop('XDG_CONFIG_HOME', None) |
|
324 | env.pop('XDG_CONFIG_HOME', None) | |
325 |
|
325 | |||
326 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
326 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) | |
327 |
|
327 | |||
328 |
|
328 | |||
329 | @with_environment |
|
329 | @with_environment | |
330 | def test_get_xdg_dir_1(): |
|
330 | def test_get_xdg_dir_1(): | |
331 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" |
|
331 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" | |
332 | reload(path) |
|
332 | reload(path) | |
333 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
333 | path.get_home_dir = lambda : HOME_TEST_DIR | |
334 | os.name = "posix" |
|
334 | os.name = "posix" | |
335 | sys.platform = "linux2" |
|
335 | sys.platform = "linux2" | |
336 | env.pop('IPYTHON_DIR', None) |
|
336 | env.pop('IPYTHON_DIR', None) | |
337 | env.pop('IPYTHONDIR', None) |
|
337 | env.pop('IPYTHONDIR', None) | |
338 | env.pop('XDG_CONFIG_HOME', None) |
|
338 | env.pop('XDG_CONFIG_HOME', None) | |
339 | nt.assert_equal(path.get_xdg_dir(), None) |
|
339 | nt.assert_equal(path.get_xdg_dir(), None) | |
340 |
|
340 | |||
341 | @with_environment |
|
341 | @with_environment | |
342 | def test_get_xdg_dir_2(): |
|
342 | def test_get_xdg_dir_2(): | |
343 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
343 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" | |
344 | reload(path) |
|
344 | reload(path) | |
345 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
345 | path.get_home_dir = lambda : HOME_TEST_DIR | |
346 | os.name = "posix" |
|
346 | os.name = "posix" | |
347 | sys.platform = "linux2" |
|
347 | sys.platform = "linux2" | |
348 | env.pop('IPYTHON_DIR', None) |
|
348 | env.pop('IPYTHON_DIR', None) | |
349 | env.pop('IPYTHONDIR', None) |
|
349 | env.pop('IPYTHONDIR', None) | |
350 | env.pop('XDG_CONFIG_HOME', None) |
|
350 | env.pop('XDG_CONFIG_HOME', None) | |
351 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
351 | cfgdir=os.path.join(path.get_home_dir(), '.config') | |
352 | if not os.path.exists(cfgdir): |
|
352 | if not os.path.exists(cfgdir): | |
353 | os.makedirs(cfgdir) |
|
353 | os.makedirs(cfgdir) | |
354 |
|
354 | |||
355 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
355 | nt.assert_equal(path.get_xdg_dir(), cfgdir) | |
356 |
|
356 | |||
357 | @with_environment |
|
357 | @with_environment | |
358 | def test_get_xdg_dir_3(): |
|
358 | def test_get_xdg_dir_3(): | |
359 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" |
|
359 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" | |
360 | reload(path) |
|
360 | reload(path) | |
361 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
361 | path.get_home_dir = lambda : HOME_TEST_DIR | |
362 | os.name = "posix" |
|
362 | os.name = "posix" | |
363 | sys.platform = "darwin" |
|
363 | sys.platform = "darwin" | |
364 | env.pop('IPYTHON_DIR', None) |
|
364 | env.pop('IPYTHON_DIR', None) | |
365 | env.pop('IPYTHONDIR', None) |
|
365 | env.pop('IPYTHONDIR', None) | |
366 | env.pop('XDG_CONFIG_HOME', None) |
|
366 | env.pop('XDG_CONFIG_HOME', None) | |
367 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
367 | cfgdir=os.path.join(path.get_home_dir(), '.config') | |
368 | if not os.path.exists(cfgdir): |
|
368 | if not os.path.exists(cfgdir): | |
369 | os.makedirs(cfgdir) |
|
369 | os.makedirs(cfgdir) | |
370 |
|
370 | |||
371 | nt.assert_equal(path.get_xdg_dir(), None) |
|
371 | nt.assert_equal(path.get_xdg_dir(), None) | |
372 |
|
372 | |||
373 | def test_filefind(): |
|
373 | def test_filefind(): | |
374 | """Various tests for filefind""" |
|
374 | """Various tests for filefind""" | |
375 | f = tempfile.NamedTemporaryFile() |
|
375 | f = tempfile.NamedTemporaryFile() | |
376 | # print 'fname:',f.name |
|
376 | # print 'fname:',f.name | |
377 | alt_dirs = path.get_ipython_dir() |
|
377 | alt_dirs = path.get_ipython_dir() | |
378 | t = path.filefind(f.name, alt_dirs) |
|
378 | t = path.filefind(f.name, alt_dirs) | |
379 | # print 'found:',t |
|
379 | # print 'found:',t | |
380 |
|
380 | |||
381 | @with_environment |
|
381 | @with_environment | |
382 | def test_get_ipython_cache_dir(): |
|
382 | def test_get_ipython_cache_dir(): | |
383 | os.environ["HOME"] = HOME_TEST_DIR |
|
383 | os.environ["HOME"] = HOME_TEST_DIR | |
384 | if os.name == 'posix' and sys.platform != 'darwin': |
|
384 | if os.name == 'posix' and sys.platform != 'darwin': | |
385 | # test default |
|
385 | # test default | |
386 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) |
|
386 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | |
387 | os.environ.pop("XDG_CACHE_HOME", None) |
|
387 | os.environ.pop("XDG_CACHE_HOME", None) | |
388 | ipdir = path.get_ipython_cache_dir() |
|
388 | ipdir = path.get_ipython_cache_dir() | |
389 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), |
|
389 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | |
390 | ipdir) |
|
390 | ipdir) | |
391 | nt.assert_true(os.path.isdir(ipdir)) |
|
391 | nt.assert_true(os.path.isdir(ipdir)) | |
392 |
|
392 | |||
393 | # test env override |
|
393 | # test env override | |
394 | os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR |
|
394 | os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR | |
395 | ipdir = path.get_ipython_cache_dir() |
|
395 | ipdir = path.get_ipython_cache_dir() | |
396 | nt.assert_true(os.path.isdir(ipdir)) |
|
396 | nt.assert_true(os.path.isdir(ipdir)) | |
397 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) |
|
397 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | |
398 | else: |
|
398 | else: | |
399 | nt.assert_equal(path.get_ipython_cache_dir(), |
|
399 | nt.assert_equal(path.get_ipython_cache_dir(), | |
400 | path.get_ipython_dir()) |
|
400 | path.get_ipython_dir()) | |
401 |
|
401 | |||
402 | def test_get_ipython_package_dir(): |
|
402 | def test_get_ipython_package_dir(): | |
403 | ipdir = path.get_ipython_package_dir() |
|
403 | ipdir = path.get_ipython_package_dir() | |
404 | nt.assert_true(os.path.isdir(ipdir)) |
|
404 | nt.assert_true(os.path.isdir(ipdir)) | |
405 |
|
405 | |||
406 |
|
406 | |||
407 | def test_get_ipython_module_path(): |
|
407 | def test_get_ipython_module_path(): | |
408 | ipapp_path = path.get_ipython_module_path('IPython.terminal.ipapp') |
|
408 | ipapp_path = path.get_ipython_module_path('IPython.terminal.ipapp') | |
409 | nt.assert_true(os.path.isfile(ipapp_path)) |
|
409 | nt.assert_true(os.path.isfile(ipapp_path)) | |
410 |
|
410 | |||
411 |
|
411 | |||
412 | @dec.skip_if_not_win32 |
|
412 | @dec.skip_if_not_win32 | |
413 | def test_get_long_path_name_win32(): |
|
413 | def test_get_long_path_name_win32(): | |
414 | with TemporaryDirectory() as tmpdir: |
|
414 | with TemporaryDirectory() as tmpdir: | |
|
415 | ||||
|
416 | # Make a long path. | |||
415 | long_path = os.path.join(tmpdir, u'this is my long path name') |
|
417 | long_path = os.path.join(tmpdir, u'this is my long path name') | |
416 | short_path = os.path.join(tmpdir, u'THISIS~1') |
|
418 | os.makedirs(long_path) | |
417 |
|
419 | |||
418 | path = path.get_long_path_name(short_path) |
|
420 | # Test to see if the short path evaluates correctly. | |
419 | nt.assert_equal(path, long_path) |
|
421 | short_path = os.path.join(tmpdir, u'THISIS~1') | |
|
422 | evaluated_path = path.get_long_path_name(short_path) | |||
|
423 | nt.assert_equal(evaluated_path.lower(), long_path.lower()) | |||
420 |
|
424 | |||
421 |
|
425 | |||
422 | @dec.skip_win32 |
|
426 | @dec.skip_win32 | |
423 | def test_get_long_path_name(): |
|
427 | def test_get_long_path_name(): | |
424 | p = path.get_long_path_name('/usr/local') |
|
428 | p = path.get_long_path_name('/usr/local') | |
425 | nt.assert_equal(p,'/usr/local') |
|
429 | nt.assert_equal(p,'/usr/local') | |
426 |
|
430 | |||
427 | @dec.skip_win32 # can't create not-user-writable dir on win |
|
431 | @dec.skip_win32 # can't create not-user-writable dir on win | |
428 | @with_environment |
|
432 | @with_environment | |
429 | def test_not_writable_ipdir(): |
|
433 | def test_not_writable_ipdir(): | |
430 | tmpdir = tempfile.mkdtemp() |
|
434 | tmpdir = tempfile.mkdtemp() | |
431 | os.name = "posix" |
|
435 | os.name = "posix" | |
432 | env.pop('IPYTHON_DIR', None) |
|
436 | env.pop('IPYTHON_DIR', None) | |
433 | env.pop('IPYTHONDIR', None) |
|
437 | env.pop('IPYTHONDIR', None) | |
434 | env.pop('XDG_CONFIG_HOME', None) |
|
438 | env.pop('XDG_CONFIG_HOME', None) | |
435 | env['HOME'] = tmpdir |
|
439 | env['HOME'] = tmpdir | |
436 | ipdir = os.path.join(tmpdir, '.ipython') |
|
440 | ipdir = os.path.join(tmpdir, '.ipython') | |
437 | os.mkdir(ipdir) |
|
441 | os.mkdir(ipdir) | |
438 | os.chmod(ipdir, 600) |
|
442 | os.chmod(ipdir, 600) | |
439 | with AssertPrints('is not a writable location', channel='stderr'): |
|
443 | with AssertPrints('is not a writable location', channel='stderr'): | |
440 | ipdir = path.get_ipython_dir() |
|
444 | ipdir = path.get_ipython_dir() | |
441 | env.pop('IPYTHON_DIR', None) |
|
445 | env.pop('IPYTHON_DIR', None) | |
442 |
|
446 | |||
443 | def test_unquote_filename(): |
|
447 | def test_unquote_filename(): | |
444 | for win32 in (True, False): |
|
448 | for win32 in (True, False): | |
445 | nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py') |
|
449 | nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py') | |
446 | nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') |
|
450 | nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') | |
447 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py') |
|
451 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py') | |
448 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') |
|
452 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') | |
449 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py') |
|
453 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py') | |
450 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') |
|
454 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') | |
451 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') |
|
455 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') | |
452 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') |
|
456 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') | |
453 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") |
|
457 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") | |
454 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") |
|
458 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") | |
455 |
|
459 | |||
456 | @with_environment |
|
460 | @with_environment | |
457 | def test_get_py_filename(): |
|
461 | def test_get_py_filename(): | |
458 | os.chdir(TMP_TEST_DIR) |
|
462 | os.chdir(TMP_TEST_DIR) | |
459 | for win32 in (True, False): |
|
463 | for win32 in (True, False): | |
460 | with make_tempfile('foo.py'): |
|
464 | with make_tempfile('foo.py'): | |
461 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') |
|
465 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') | |
462 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') |
|
466 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') | |
463 | with make_tempfile('foo'): |
|
467 | with make_tempfile('foo'): | |
464 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') |
|
468 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') | |
465 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
469 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | |
466 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) |
|
470 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) | |
467 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
471 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | |
468 | true_fn = 'foo with spaces.py' |
|
472 | true_fn = 'foo with spaces.py' | |
469 | with make_tempfile(true_fn): |
|
473 | with make_tempfile(true_fn): | |
470 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) |
|
474 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) | |
471 | nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) |
|
475 | nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) | |
472 | if win32: |
|
476 | if win32: | |
473 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) |
|
477 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) | |
474 | nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) |
|
478 | nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) | |
475 | else: |
|
479 | else: | |
476 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) |
|
480 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) | |
477 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) |
|
481 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) | |
478 |
|
482 | |||
479 | def test_unicode_in_filename(): |
|
483 | def test_unicode_in_filename(): | |
480 | """When a file doesn't exist, the exception raised should be safe to call |
|
484 | """When a file doesn't exist, the exception raised should be safe to call | |
481 | str() on - i.e. in Python 2 it must only have ASCII characters. |
|
485 | str() on - i.e. in Python 2 it must only have ASCII characters. | |
482 |
|
486 | |||
483 | https://github.com/ipython/ipython/issues/875 |
|
487 | https://github.com/ipython/ipython/issues/875 | |
484 | """ |
|
488 | """ | |
485 | try: |
|
489 | try: | |
486 | # these calls should not throw unicode encode exceptions |
|
490 | # these calls should not throw unicode encode exceptions | |
487 | path.get_py_filename(u'fooéè.py', force_win32=False) |
|
491 | path.get_py_filename(u'fooéè.py', force_win32=False) | |
488 | except IOError as ex: |
|
492 | except IOError as ex: | |
489 | str(ex) |
|
493 | str(ex) | |
490 |
|
494 | |||
491 |
|
495 | |||
492 | class TestShellGlob(object): |
|
496 | class TestShellGlob(object): | |
493 |
|
497 | |||
494 | @classmethod |
|
498 | @classmethod | |
495 | def setUpClass(cls): |
|
499 | def setUpClass(cls): | |
496 | cls.filenames_start_with_a = map('a{0}'.format, range(3)) |
|
500 | cls.filenames_start_with_a = map('a{0}'.format, range(3)) | |
497 | cls.filenames_end_with_b = map('{0}b'.format, range(3)) |
|
501 | cls.filenames_end_with_b = map('{0}b'.format, range(3)) | |
498 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b |
|
502 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b | |
499 | cls.tempdir = TemporaryDirectory() |
|
503 | cls.tempdir = TemporaryDirectory() | |
500 | td = cls.tempdir.name |
|
504 | td = cls.tempdir.name | |
501 |
|
505 | |||
502 | with cls.in_tempdir(): |
|
506 | with cls.in_tempdir(): | |
503 | # Create empty files |
|
507 | # Create empty files | |
504 | for fname in cls.filenames: |
|
508 | for fname in cls.filenames: | |
505 | open(os.path.join(td, fname), 'w').close() |
|
509 | open(os.path.join(td, fname), 'w').close() | |
506 |
|
510 | |||
507 | @classmethod |
|
511 | @classmethod | |
508 | def tearDownClass(cls): |
|
512 | def tearDownClass(cls): | |
509 | cls.tempdir.cleanup() |
|
513 | cls.tempdir.cleanup() | |
510 |
|
514 | |||
511 | @classmethod |
|
515 | @classmethod | |
512 | @contextmanager |
|
516 | @contextmanager | |
513 | def in_tempdir(cls): |
|
517 | def in_tempdir(cls): | |
514 | save = os.getcwdu() |
|
518 | save = os.getcwdu() | |
515 | try: |
|
519 | try: | |
516 | os.chdir(cls.tempdir.name) |
|
520 | os.chdir(cls.tempdir.name) | |
517 | yield |
|
521 | yield | |
518 | finally: |
|
522 | finally: | |
519 | os.chdir(save) |
|
523 | os.chdir(save) | |
520 |
|
524 | |||
521 | def check_match(self, patterns, matches): |
|
525 | def check_match(self, patterns, matches): | |
522 | with self.in_tempdir(): |
|
526 | with self.in_tempdir(): | |
523 | # glob returns unordered list. that's why sorted is required. |
|
527 | # glob returns unordered list. that's why sorted is required. | |
524 | nt.assert_equals(sorted(path.shellglob(patterns)), |
|
528 | nt.assert_equals(sorted(path.shellglob(patterns)), | |
525 | sorted(matches)) |
|
529 | sorted(matches)) | |
526 |
|
530 | |||
527 | def common_cases(self): |
|
531 | def common_cases(self): | |
528 | return [ |
|
532 | return [ | |
529 | (['*'], self.filenames), |
|
533 | (['*'], self.filenames), | |
530 | (['a*'], self.filenames_start_with_a), |
|
534 | (['a*'], self.filenames_start_with_a), | |
531 | (['*c'], ['*c']), |
|
535 | (['*c'], ['*c']), | |
532 | (['*', 'a*', '*b', '*c'], self.filenames |
|
536 | (['*', 'a*', '*b', '*c'], self.filenames | |
533 | + self.filenames_start_with_a |
|
537 | + self.filenames_start_with_a | |
534 | + self.filenames_end_with_b |
|
538 | + self.filenames_end_with_b | |
535 | + ['*c']), |
|
539 | + ['*c']), | |
536 | (['a[012]'], self.filenames_start_with_a), |
|
540 | (['a[012]'], self.filenames_start_with_a), | |
537 | ] |
|
541 | ] | |
538 |
|
542 | |||
539 | @skip_win32 |
|
543 | @skip_win32 | |
540 | def test_match_posix(self): |
|
544 | def test_match_posix(self): | |
541 | for (patterns, matches) in self.common_cases() + [ |
|
545 | for (patterns, matches) in self.common_cases() + [ | |
542 | ([r'\*'], ['*']), |
|
546 | ([r'\*'], ['*']), | |
543 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), |
|
547 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), | |
544 | ([r'a\[012]'], ['a[012]']), |
|
548 | ([r'a\[012]'], ['a[012]']), | |
545 | ]: |
|
549 | ]: | |
546 | yield (self.check_match, patterns, matches) |
|
550 | yield (self.check_match, patterns, matches) | |
547 |
|
551 | |||
548 | @skip_if_not_win32 |
|
552 | @skip_if_not_win32 | |
549 | def test_match_windows(self): |
|
553 | def test_match_windows(self): | |
550 | for (patterns, matches) in self.common_cases() + [ |
|
554 | for (patterns, matches) in self.common_cases() + [ | |
551 | # In windows, backslash is interpreted as path |
|
555 | # In windows, backslash is interpreted as path | |
552 | # separator. Therefore, you can't escape glob |
|
556 | # separator. Therefore, you can't escape glob | |
553 | # using it. |
|
557 | # using it. | |
554 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), |
|
558 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), | |
555 | ([r'a\[012]'], [r'a\[012]']), |
|
559 | ([r'a\[012]'], [r'a\[012]']), | |
556 | ]: |
|
560 | ]: | |
557 | yield (self.check_match, patterns, matches) |
|
561 | yield (self.check_match, patterns, matches) | |
558 |
|
562 | |||
559 |
|
563 | |||
560 | def test_unescape_glob(): |
|
564 | def test_unescape_glob(): | |
561 | nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') |
|
565 | nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') | |
562 | nt.assert_equals(path.unescape_glob(r'\\*'), r'\*') |
|
566 | nt.assert_equals(path.unescape_glob(r'\\*'), r'\*') | |
563 | nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*') |
|
567 | nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*') | |
564 | nt.assert_equals(path.unescape_glob(r'\\a'), r'\a') |
|
568 | nt.assert_equals(path.unescape_glob(r'\\a'), r'\a') | |
565 | nt.assert_equals(path.unescape_glob(r'\a'), r'\a') |
|
569 | nt.assert_equals(path.unescape_glob(r'\a'), r'\a') | |
566 |
|
570 | |||
567 |
|
571 | |||
568 | class TestLinkOrCopy(object): |
|
572 | class TestLinkOrCopy(object): | |
569 | def setUp(self): |
|
573 | def setUp(self): | |
570 | self.tempdir = TemporaryDirectory() |
|
574 | self.tempdir = TemporaryDirectory() | |
571 | self.src = self.dst("src") |
|
575 | self.src = self.dst("src") | |
572 | with open(self.src, "w") as f: |
|
576 | with open(self.src, "w") as f: | |
573 | f.write("Hello, world!") |
|
577 | f.write("Hello, world!") | |
574 |
|
578 | |||
575 | def tearDown(self): |
|
579 | def tearDown(self): | |
576 | self.tempdir.cleanup() |
|
580 | self.tempdir.cleanup() | |
577 |
|
581 | |||
578 | def dst(self, *args): |
|
582 | def dst(self, *args): | |
579 | return os.path.join(self.tempdir.name, *args) |
|
583 | return os.path.join(self.tempdir.name, *args) | |
580 |
|
584 | |||
581 | def assert_inode_not_equal(self, a, b): |
|
585 | def assert_inode_not_equal(self, a, b): | |
582 | nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino, |
|
586 | nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino, | |
583 | "%r and %r do reference the same indoes" %(a, b)) |
|
587 | "%r and %r do reference the same indoes" %(a, b)) | |
584 |
|
588 | |||
585 | def assert_inode_equal(self, a, b): |
|
589 | def assert_inode_equal(self, a, b): | |
586 | nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino, |
|
590 | nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino, | |
587 | "%r and %r do not reference the same indoes" %(a, b)) |
|
591 | "%r and %r do not reference the same indoes" %(a, b)) | |
588 |
|
592 | |||
589 | def assert_content_equal(self, a, b): |
|
593 | def assert_content_equal(self, a, b): | |
590 | with open(a) as a_f: |
|
594 | with open(a) as a_f: | |
591 | with open(b) as b_f: |
|
595 | with open(b) as b_f: | |
592 | nt.assert_equals(a_f.read(), b_f.read()) |
|
596 | nt.assert_equals(a_f.read(), b_f.read()) | |
593 |
|
597 | |||
594 | @skip_win32 |
|
598 | @skip_win32 | |
595 | def test_link_successful(self): |
|
599 | def test_link_successful(self): | |
596 | dst = self.dst("target") |
|
600 | dst = self.dst("target") | |
597 | path.link_or_copy(self.src, dst) |
|
601 | path.link_or_copy(self.src, dst) | |
598 | self.assert_inode_equal(self.src, dst) |
|
602 | self.assert_inode_equal(self.src, dst) | |
599 |
|
603 | |||
600 | @skip_win32 |
|
604 | @skip_win32 | |
601 | def test_link_into_dir(self): |
|
605 | def test_link_into_dir(self): | |
602 | dst = self.dst("some_dir") |
|
606 | dst = self.dst("some_dir") | |
603 | os.mkdir(dst) |
|
607 | os.mkdir(dst) | |
604 | path.link_or_copy(self.src, dst) |
|
608 | path.link_or_copy(self.src, dst) | |
605 | expected_dst = self.dst("some_dir", os.path.basename(self.src)) |
|
609 | expected_dst = self.dst("some_dir", os.path.basename(self.src)) | |
606 | self.assert_inode_equal(self.src, expected_dst) |
|
610 | self.assert_inode_equal(self.src, expected_dst) | |
607 |
|
611 | |||
608 | @skip_win32 |
|
612 | @skip_win32 | |
609 | def test_target_exists(self): |
|
613 | def test_target_exists(self): | |
610 | dst = self.dst("target") |
|
614 | dst = self.dst("target") | |
611 | open(dst, "w").close() |
|
615 | open(dst, "w").close() | |
612 | path.link_or_copy(self.src, dst) |
|
616 | path.link_or_copy(self.src, dst) | |
613 | self.assert_inode_equal(self.src, dst) |
|
617 | self.assert_inode_equal(self.src, dst) | |
614 |
|
618 | |||
615 | @skip_win32 |
|
619 | @skip_win32 | |
616 | def test_no_link(self): |
|
620 | def test_no_link(self): | |
617 | real_link = os.link |
|
621 | real_link = os.link | |
618 | try: |
|
622 | try: | |
619 | del os.link |
|
623 | del os.link | |
620 | dst = self.dst("target") |
|
624 | dst = self.dst("target") | |
621 | path.link_or_copy(self.src, dst) |
|
625 | path.link_or_copy(self.src, dst) | |
622 | self.assert_content_equal(self.src, dst) |
|
626 | self.assert_content_equal(self.src, dst) | |
623 | self.assert_inode_not_equal(self.src, dst) |
|
627 | self.assert_inode_not_equal(self.src, dst) | |
624 | finally: |
|
628 | finally: | |
625 | os.link = real_link |
|
629 | os.link = real_link | |
626 |
|
630 | |||
627 | @skip_if_not_win32 |
|
631 | @skip_if_not_win32 | |
628 | def test_windows(self): |
|
632 | def test_windows(self): | |
629 | dst = self.dst("target") |
|
633 | dst = self.dst("target") | |
630 | path.link_or_copy(self.src, dst) |
|
634 | path.link_or_copy(self.src, dst) | |
631 | self.assert_content_equal(self.src, dst) |
|
635 | self.assert_content_equal(self.src, dst) |
General Comments 0
You need to be logged in to leave comments.
Login now