##// END OF EJS Templates
Use mock for monkeypatching instead of handcoding it
Thomas Kluyver -
Show More
@@ -1,4 +1,3 b''
1 from contextlib import contextmanager
2 import errno
1 import errno
3 import os
2 import os
4 import shutil
3 import shutil
@@ -6,21 +5,18 b' import sys'
6 import tempfile
5 import tempfile
7 import warnings
6 import warnings
8
7
9 try:
8 try: # Python 3
10 reload
9 from unittest.mock import patch
11 except NameError: # Python 3
10 except ImportError: # Python 2
12 from imp import reload
11 from mock import patch
13
12
14 from nose import with_setup
15 import nose.tools as nt
13 import nose.tools as nt
16 from testpath import modified_env
14 from testpath import modified_env
17
15
18 import IPython
19 from IPython import paths
16 from IPython import paths
20 from IPython.testing.decorators import skip_win32
17 from IPython.testing.decorators import skip_win32
21 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.utils.tempdir import TemporaryDirectory
22
19
23 env = os.environ
24 TMP_TEST_DIR = tempfile.mkdtemp()
20 TMP_TEST_DIR = tempfile.mkdtemp()
25 HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir")
21 HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir")
26 XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir")
22 XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir")
@@ -49,89 +45,57 b' def teardown():'
49 # that non-empty directories are all recursively removed.
45 # that non-empty directories are all recursively removed.
50 shutil.rmtree(TMP_TEST_DIR)
46 shutil.rmtree(TMP_TEST_DIR)
51
47
52
53 def setup_environment():
54 """Setup testenvironment for some functions that are tested
55 in this module. In particular this functions stores attributes
56 and other things that we need to stub in some test functions.
57 This needs to be done on a function level and not module level because
58 each testfunction needs a pristine environment.
59 """
60 global oldstuff
61 oldstuff = (os.name, sys.platform, paths.get_home_dir, IPython.__file__, os.getcwd())
62
63 def teardown_environment():
64 """Restore things that were remembered by the setup_environment function
65 """
66 (os.name, sys.platform, paths.get_home_dir, IPython.__file__, old_wd) = oldstuff
67 os.chdir(old_wd)
68 reload(paths)
69
70 if hasattr(sys, 'frozen'):
71 del sys.frozen
72
73 # Build decorator that uses the setup_environment/setup_environment
74 with_environment = with_setup(setup_environment, teardown_environment)
75
76 @contextmanager
77 def patch_get_home_dir(dirpath):
48 def patch_get_home_dir(dirpath):
78 orig_get_home_dir = paths.get_home_dir
49 return patch.object(paths, 'get_home_dir', return_value=dirpath)
79 paths.get_home_dir = lambda : dirpath
50
80 try:
81 yield
82 finally:
83 paths.get_home_dir = orig_get_home_dir
84
51
85 def test_get_ipython_dir_1():
52 def test_get_ipython_dir_1():
86 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
53 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
87 env_ipdir = os.path.join("someplace", ".ipython")
54 env_ipdir = os.path.join("someplace", ".ipython")
88 paths._writable_dir = lambda path: True
55 with patch.object(paths, '_writable_dir', return_value=True), \
89 with modified_env({'IPYTHONDIR': env_ipdir}):
56 modified_env({'IPYTHONDIR': env_ipdir}):
90 ipdir = paths.get_ipython_dir()
57 ipdir = paths.get_ipython_dir()
91
58
92 nt.assert_equal(ipdir, env_ipdir)
59 nt.assert_equal(ipdir, env_ipdir)
93
60
94 @with_environment
95 def test_get_ipython_dir_2():
61 def test_get_ipython_dir_2():
96 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
62 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
97 with patch_get_home_dir('someplace'):
63 with patch_get_home_dir('someplace'), \
98 paths.get_xdg_dir = lambda : None
64 patch.object(paths, 'get_xdg_dir', return_value=None), \
99 paths._writable_dir = lambda path: True
65 patch.object(paths, '_writable_dir', return_value=True), \
100 os.name = "posix"
66 patch('os.name', "posix"), \
101 with modified_env({'IPYTHON_DIR': None,
67 modified_env({'IPYTHON_DIR': None,
102 'IPYTHONDIR': None,
68 'IPYTHONDIR': None,
103 'XDG_CONFIG_HOME': None
69 'XDG_CONFIG_HOME': None
104 }):
70 }):
105 ipdir = paths.get_ipython_dir()
71 ipdir = paths.get_ipython_dir()
106
72
107 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
73 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
108
74
109 @with_environment
110 def test_get_ipython_dir_3():
75 def test_get_ipython_dir_3():
111 """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist."""
76 """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist."""
112 tmphome = TemporaryDirectory()
77 tmphome = TemporaryDirectory()
113 try:
78 try:
114 with patch_get_home_dir(tmphome.name):
79 with patch_get_home_dir(tmphome.name), \
115 os.name = "posix"
80 patch('os.name', 'posix'), \
116 with modified_env({
81 modified_env({
117 'IPYTHON_DIR': None,
82 'IPYTHON_DIR': None,
118 'IPYTHONDIR': None,
83 'IPYTHONDIR': None,
119 'XDG_CONFIG_HOME': XDG_TEST_DIR,
84 'XDG_CONFIG_HOME': XDG_TEST_DIR,
120 }), warnings.catch_warnings(record=True) as w:
85 }), warnings.catch_warnings(record=True) as w:
121 ipdir = paths.get_ipython_dir()
86 ipdir = paths.get_ipython_dir()
122
87
123 nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython"))
88 nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython"))
124 if sys.platform != 'darwin':
89 if sys.platform != 'darwin':
125 nt.assert_equal(len(w), 1)
90 nt.assert_equal(len(w), 1)
126 nt.assert_in('Moving', str(w[0]))
91 nt.assert_in('Moving', str(w[0]))
127 finally:
92 finally:
128 tmphome.cleanup()
93 tmphome.cleanup()
129
94
130 @with_environment
131 def test_get_ipython_dir_4():
95 def test_get_ipython_dir_4():
132 """test_get_ipython_dir_4, warn if XDG and home both exist."""
96 """test_get_ipython_dir_4, warn if XDG and home both exist."""
133 with patch_get_home_dir(HOME_TEST_DIR):
97 with patch_get_home_dir(HOME_TEST_DIR), \
134 os.name = "posix"
98 patch('os.name', 'posix'):
135 try:
99 try:
136 os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
100 os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
137 except OSError as e:
101 except OSError as e:
@@ -151,11 +115,10 b' def test_get_ipython_dir_4():'
151 nt.assert_equal(len(w), 1)
115 nt.assert_equal(len(w), 1)
152 nt.assert_in('Ignoring', str(w[0]))
116 nt.assert_in('Ignoring', str(w[0]))
153
117
154 @with_environment
155 def test_get_ipython_dir_5():
118 def test_get_ipython_dir_5():
156 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
119 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
157 with patch_get_home_dir(HOME_TEST_DIR):
120 with patch_get_home_dir(HOME_TEST_DIR), \
158 os.name = "posix"
121 patch('os.name', 'posix'):
159 try:
122 try:
160 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
123 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
161 except OSError as e:
124 except OSError as e:
@@ -171,55 +134,46 b' def test_get_ipython_dir_5():'
171
134
172 nt.assert_equal(ipdir, IP_TEST_DIR)
135 nt.assert_equal(ipdir, IP_TEST_DIR)
173
136
174 @with_environment
175 def test_get_ipython_dir_6():
137 def test_get_ipython_dir_6():
176 """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
138 """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
177 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
139 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
178 os.mkdir(xdg)
140 os.mkdir(xdg)
179 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
141 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
180 with patch_get_home_dir(HOME_TEST_DIR):
142 print(paths._writable_dir)
181 orig_get_xdg_dir = paths.get_xdg_dir
143 with patch_get_home_dir(HOME_TEST_DIR), \
182 paths.get_xdg_dir = lambda : xdg
144 patch.object(paths, 'get_xdg_dir', return_value=xdg), \
183 try:
145 patch('os.name', 'posix'), \
184 os.name = "posix"
146 modified_env({
185 with modified_env({
186 'IPYTHON_DIR': None,
147 'IPYTHON_DIR': None,
187 'IPYTHONDIR': None,
148 'IPYTHONDIR': None,
188 'XDG_CONFIG_HOME': None,
149 'XDG_CONFIG_HOME': None,
189 }), warnings.catch_warnings(record=True) as w:
150 }), warnings.catch_warnings(record=True) as w:
190 ipdir = paths.get_ipython_dir()
151 ipdir = paths.get_ipython_dir()
191
152
192 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython'))
153 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython'))
193 nt.assert_equal(len(w), 0)
154 nt.assert_equal(len(w), 0)
194 finally:
195 paths.get_xdg_dir = orig_get_xdg_dir
196
155
197 def test_get_ipython_dir_7():
156 def test_get_ipython_dir_7():
198 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
157 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
199 paths._writable_dir = lambda path: True
200 home_dir = os.path.normpath(os.path.expanduser('~'))
158 home_dir = os.path.normpath(os.path.expanduser('~'))
201 with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}):
159 with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \
160 patch.object(paths, '_writable_dir', return_value=True):
202 ipdir = paths.get_ipython_dir()
161 ipdir = paths.get_ipython_dir()
203 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
162 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
204
163
205 @skip_win32
164 @skip_win32
206 def test_get_ipython_dir_8():
165 def test_get_ipython_dir_8():
207 """test_get_ipython_dir_8, test / home directory"""
166 """test_get_ipython_dir_8, test / home directory"""
208 old = paths._writable_dir, paths.get_xdg_dir
167 with patch.object(paths, '_writable_dir', lambda path: bool(path)), \
209 try:
168 patch.object(paths, 'get_xdg_dir', return_value=None), \
210 paths._writable_dir = lambda path: bool(path)
169 modified_env({
211 paths.get_xdg_dir = lambda: None
170 'IPYTHON_DIR': None,
212 with modified_env({
171 'IPYTHONDIR': None,
213 'IPYTHON_DIR': None,
172 'HOME': '/',
214 'IPYTHONDIR': None,
173 }):
215 'HOME': '/',
174 nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
216 }):
217 nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
218 finally:
219 paths._writable_dir, paths.get_xdg_dir = old
220
175
221
176
222 @with_environment
223 def test_get_ipython_cache_dir():
177 def test_get_ipython_cache_dir():
224 with modified_env({'HOME': HOME_TEST_DIR}):
178 with modified_env({'HOME': HOME_TEST_DIR}):
225 if os.name == 'posix' and sys.platform != 'darwin':
179 if os.name == 'posix' and sys.platform != 'darwin':
General Comments 0
You need to be logged in to leave comments. Login now