##// END OF EJS Templates
Use testpath to temporarily modify environment variables
Thomas Kluyver -
Show More
@@ -13,6 +13,7 b' except NameError: # Python 3'
13 13
14 14 from nose import with_setup
15 15 import nose.tools as nt
16 from testpath import modified_env
16 17
17 18 import IPython
18 19 from IPython import paths
@@ -56,20 +57,16 b' def setup_environment():'
56 57 This needs to be done on a function level and not module level because
57 58 each testfunction needs a pristine environment.
58 59 """
59 global oldstuff, platformstuff
60 oldstuff = (env.copy(), os.name, sys.platform, paths.get_home_dir, IPython.__file__, os.getcwd())
60 global oldstuff
61 oldstuff = (os.name, sys.platform, paths.get_home_dir, IPython.__file__, os.getcwd())
61 62
62 63 def teardown_environment():
63 64 """Restore things that were remembered by the setup_environment function
64 65 """
65 (oldenv, os.name, sys.platform, paths.get_home_dir, IPython.__file__, old_wd) = oldstuff
66 (os.name, sys.platform, paths.get_home_dir, IPython.__file__, old_wd) = oldstuff
66 67 os.chdir(old_wd)
67 68 reload(paths)
68 69
69 for key in list(env):
70 if key not in oldenv:
71 del env[key]
72 env.update(oldenv)
73 70 if hasattr(sys, 'frozen'):
74 71 del sys.frozen
75 72
@@ -85,16 +82,14 b' def patch_get_home_dir(dirpath):'
85 82 finally:
86 83 paths.get_home_dir = orig_get_home_dir
87 84
88
89 @with_environment
90 85 def test_get_ipython_dir_1():
91 86 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
92 87 env_ipdir = os.path.join("someplace", ".ipython")
93 88 paths._writable_dir = lambda path: True
94 env['IPYTHONDIR'] = env_ipdir
95 ipdir = paths.get_ipython_dir()
96 nt.assert_equal(ipdir, env_ipdir)
89 with modified_env({'IPYTHONDIR': env_ipdir}):
90 ipdir = paths.get_ipython_dir()
97 91
92 nt.assert_equal(ipdir, env_ipdir)
98 93
99 94 @with_environment
100 95 def test_get_ipython_dir_2():
@@ -103,10 +98,12 b' def test_get_ipython_dir_2():'
103 98 paths.get_xdg_dir = lambda : None
104 99 paths._writable_dir = lambda path: True
105 100 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()
101 with modified_env({'IPYTHON_DIR': None,
102 'IPYTHONDIR': None,
103 'XDG_CONFIG_HOME': None
104 }):
105 ipdir = paths.get_ipython_dir()
106
110 107 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
111 108
112 109 @with_environment
@@ -116,11 +113,11 b' def test_get_ipython_dir_3():'
116 113 try:
117 114 with patch_get_home_dir(tmphome.name):
118 115 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:
116 with modified_env({
117 'IPYTHON_DIR': None,
118 'IPYTHONDIR': None,
119 'XDG_CONFIG_HOME': XDG_TEST_DIR,
120 }), warnings.catch_warnings(record=True) as w:
124 121 ipdir = paths.get_ipython_dir()
125 122
126 123 nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython"))
@@ -135,16 +132,18 b' def test_get_ipython_dir_4():'
135 132 """test_get_ipython_dir_4, warn if XDG and home both exist."""
136 133 with patch_get_home_dir(HOME_TEST_DIR):
137 134 os.name = "posix"
138 env.pop('IPYTHON_DIR', None)
139 env.pop('IPYTHONDIR', None)
140 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
141 135 try:
142 136 os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
143 137 except OSError as e:
144 138 if e.errno != errno.EEXIST:
145 139 raise
146 140
147 with warnings.catch_warnings(record=True) as w:
141
142 with modified_env({
143 'IPYTHON_DIR': None,
144 'IPYTHONDIR': None,
145 'XDG_CONFIG_HOME': XDG_TEST_DIR,
146 }), warnings.catch_warnings(record=True) as w:
148 147 ipdir = paths.get_ipython_dir()
149 148
150 149 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython"))
@@ -157,15 +156,19 b' def test_get_ipython_dir_5():'
157 156 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
158 157 with patch_get_home_dir(HOME_TEST_DIR):
159 158 os.name = "posix"
160 env.pop('IPYTHON_DIR', None)
161 env.pop('IPYTHONDIR', None)
162 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
163 159 try:
164 160 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
165 161 except OSError as e:
166 162 if e.errno != errno.ENOENT:
167 163 raise
168 ipdir = paths.get_ipython_dir()
164
165 with modified_env({
166 'IPYTHON_DIR': None,
167 'IPYTHONDIR': None,
168 'XDG_CONFIG_HOME': XDG_TEST_DIR,
169 }):
170 ipdir = paths.get_ipython_dir()
171
169 172 nt.assert_equal(ipdir, IP_TEST_DIR)
170 173
171 174 @with_environment
@@ -179,10 +182,11 b' def test_get_ipython_dir_6():'
179 182 paths.get_xdg_dir = lambda : xdg
180 183 try:
181 184 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:
185 with modified_env({
186 'IPYTHON_DIR': None,
187 'IPYTHONDIR': None,
188 'XDG_CONFIG_HOME': None,
189 }), warnings.catch_warnings(record=True) as w:
186 190 ipdir = paths.get_ipython_dir()
187 191
188 192 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython'))
@@ -190,51 +194,51 b' def test_get_ipython_dir_6():'
190 194 finally:
191 195 paths.get_xdg_dir = orig_get_xdg_dir
192 196
193 @with_environment
194 197 def test_get_ipython_dir_7():
195 198 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
196 199 paths._writable_dir = lambda path: True
197 200 home_dir = os.path.normpath(os.path.expanduser('~'))
198 env['IPYTHONDIR'] = os.path.join('~', 'somewhere')
199 ipdir = paths.get_ipython_dir()
201 with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}):
202 ipdir = paths.get_ipython_dir()
200 203 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
201 204
202 205 @skip_win32
203 @with_environment
204 206 def test_get_ipython_dir_8():
205 207 """test_get_ipython_dir_8, test / home directory"""
206 208 old = paths._writable_dir, paths.get_xdg_dir
207 209 try:
208 210 paths._writable_dir = lambda path: bool(path)
209 211 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')
212 with modified_env({
213 'IPYTHON_DIR': None,
214 'IPYTHONDIR': None,
215 'HOME': '/',
216 }):
217 nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
214 218 finally:
215 219 paths._writable_dir, paths.get_xdg_dir = old
216 220
217 221
218 222 @with_environment
219 223 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())
224 with modified_env({'HOME': HOME_TEST_DIR}):
225 if os.name == 'posix' and sys.platform != 'darwin':
226 # test default
227 os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
228 with modified_env({'XDG_CACHE_HOME': None}):
229 ipdir = paths.get_ipython_cache_dir()
230 nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
231 ipdir)
232 nt.assert_true(os.path.isdir(ipdir))
233
234 # test env override
235 with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
236 ipdir = paths.get_ipython_cache_dir()
237 nt.assert_true(os.path.isdir(ipdir))
238 nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
239 else:
240 nt.assert_equal(paths.get_ipython_cache_dir(),
241 paths.get_ipython_dir())
238 242
239 243 def test_get_ipython_package_dir():
240 244 ipdir = paths.get_ipython_package_dir()
General Comments 0
You need to be logged in to leave comments. Login now