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