##// END OF EJS Templates
Start migrating somethings to pytest's monkeypatch
Matthias Bussonnier -
Show More
@@ -113,11 +113,11 with_environment = pytest.mark.usefixtures("environment")
113
113
114 @skip_if_not_win32
114 @skip_if_not_win32
115 @with_environment
115 @with_environment
116 def test_get_home_dir_1():
116 def test_get_home_dir_1(monkeypatch):
117 """Testcase for py2exe logic, un-compressed lib
117 """Testcase for py2exe logic, un-compressed lib
118 """
118 """
119 unfrozen = path.get_home_dir()
119 unfrozen = path.get_home_dir()
120 sys.frozen = True
120 monkeypatch.setattr(sys, "frozen", True)
121
121
122 #fake filename for IPython.__init__
122 #fake filename for IPython.__init__
123 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
123 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
@@ -128,13 +128,15 def test_get_home_dir_1():
128
128
129 @skip_if_not_win32
129 @skip_if_not_win32
130 @with_environment
130 @with_environment
131 def test_get_home_dir_2():
131 def test_get_home_dir_2(monkeypatch):
132 """Testcase for py2exe logic, compressed lib
132 """Testcase for py2exe logic, compressed lib
133 """
133 """
134 unfrozen = path.get_home_dir()
134 unfrozen = path.get_home_dir()
135 sys.frozen = True
135 monkeypatch.setattr(sys, "frozen", True)
136 #fake filename for IPython.__init__
136 # fake filename for IPython.__init__
137 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
137 IPython.__file__ = abspath(
138 join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")
139 ).lower()
138
140
139 home_dir = path.get_home_dir(True)
141 home_dir = path.get_home_dir(True)
140 assert home_dir == unfrozen
142 assert home_dir == unfrozen
@@ -160,22 +162,22 def test_get_home_dir_4():
160
162
161 @skip_win32
163 @skip_win32
162 @with_environment
164 @with_environment
163 def test_get_home_dir_5():
165 def test_get_home_dir_5(monkeypatch):
164 """raise HomeDirError if $HOME is specified, but not a writable dir"""
166 """raise HomeDirError if $HOME is specified, but not a writable dir"""
165 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
167 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
166 # set os.name = posix, to prevent My Documents fallback on Windows
168 # set os.name = posix, to prevent My Documents fallback on Windows
167 os.name = 'posix'
169 monkeypatch.setattr(os, "name", "posix")
168 pytest.raises(path.HomeDirError, path.get_home_dir, True)
170 pytest.raises(path.HomeDirError, path.get_home_dir, True)
169
171
170 # Should we stub wreg fully so we can run the test on all platforms?
172 # Should we stub wreg fully so we can run the test on all platforms?
171 @skip_if_not_win32
173 @skip_if_not_win32
172 @with_environment
174 @with_environment
173 def test_get_home_dir_8():
175 def test_get_home_dir_8(monkeypatch):
174 """Using registry hack for 'My Documents', os=='nt'
176 """Using registry hack for 'My Documents', os=='nt'
175
177
176 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
178 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
177 """
179 """
178 os.name = 'nt'
180 monkeypatch.setattr(os, "name", "nt")
179 # Remove from stub environment all keys that may be set
181 # Remove from stub environment all keys that may be set
180 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
182 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
181 env.pop(key, None)
183 env.pop(key, None)
@@ -194,13 +196,12 def test_get_home_dir_8():
194 assert home_dir == abspath(HOME_TEST_DIR)
196 assert home_dir == abspath(HOME_TEST_DIR)
195
197
196 @with_environment
198 @with_environment
197 def test_get_xdg_dir_0():
199 def test_get_xdg_dir_0(monkeypatch):
198 """test_get_xdg_dir_0, check xdg_dir"""
200 """test_get_xdg_dir_0, check xdg_dir"""
199 reload(path)
201 monkeypatch.setattr(path, "_writable_dir", lambda path: True)
200 path._writable_dir = lambda path: True
202 monkeypatch.setattr(path, "get_home_dir", lambda: "somewhere")
201 path.get_home_dir = lambda : 'somewhere'
203 monkeypatch.setattr(os, "name", "posix")
202 os.name = "posix"
204 monkeypatch.setattr(sys, "platform", "linux2")
203 sys.platform = "linux2"
204 env.pop('IPYTHON_DIR', None)
205 env.pop('IPYTHON_DIR', None)
205 env.pop('IPYTHONDIR', None)
206 env.pop('IPYTHONDIR', None)
206 env.pop('XDG_CONFIG_HOME', None)
207 env.pop('XDG_CONFIG_HOME', None)
@@ -209,44 +210,41 def test_get_xdg_dir_0():
209
210
210
211
211 @with_environment
212 @with_environment
212 def test_get_xdg_dir_1():
213 def test_get_xdg_dir_1(monkeypatch):
213 """test_get_xdg_dir_1, check nonexistent xdg_dir"""
214 """test_get_xdg_dir_1, check nonexistent xdg_dir"""
214 reload(path)
215 monkeypatch.setattr(path, "get_home_dir", lambda: HOME_TEST_DIR)
215 path.get_home_dir = lambda : HOME_TEST_DIR
216 monkeypatch.setattr(os, "name", "posix")
216 os.name = "posix"
217 monkeypatch.setattr(sys, "platform", "linux2")
217 sys.platform = "linux2"
218 env.pop("IPYTHON_DIR", None)
218 env.pop('IPYTHON_DIR', None)
219 env.pop("IPYTHONDIR", None)
219 env.pop('IPYTHONDIR', None)
220 env.pop("XDG_CONFIG_HOME", None)
220 env.pop('XDG_CONFIG_HOME', None)
221 assert path.get_xdg_dir() is None
221 assert path.get_xdg_dir() is None
222
222
223 @with_environment
223 @with_environment
224 def test_get_xdg_dir_2():
224 def test_get_xdg_dir_2(monkeypatch):
225 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
225 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
226 reload(path)
226 monkeypatch.setattr(path, "get_home_dir", lambda: HOME_TEST_DIR)
227 path.get_home_dir = lambda : HOME_TEST_DIR
227 monkeypatch.setattr(os, "name", "posix")
228 os.name = "posix"
228 monkeypatch.setattr(sys, "platform", "linux2")
229 sys.platform = "linux2"
229 env.pop("IPYTHON_DIR", None)
230 env.pop('IPYTHON_DIR', None)
230 env.pop("IPYTHONDIR", None)
231 env.pop('IPYTHONDIR', None)
231 env.pop("XDG_CONFIG_HOME", None)
232 env.pop('XDG_CONFIG_HOME', None)
232 cfgdir = os.path.join(path.get_home_dir(), ".config")
233 cfgdir=os.path.join(path.get_home_dir(), '.config')
234 if not os.path.exists(cfgdir):
233 if not os.path.exists(cfgdir):
235 os.makedirs(cfgdir)
234 os.makedirs(cfgdir)
236
235
237 assert path.get_xdg_dir() == cfgdir
236 assert path.get_xdg_dir() == cfgdir
238
237
239 @with_environment
238 @with_environment
240 def test_get_xdg_dir_3():
239 def test_get_xdg_dir_3(monkeypatch):
241 """test_get_xdg_dir_3, check xdg_dir not used on non-posix systems"""
240 """test_get_xdg_dir_3, check xdg_dir not used on non-posix systems"""
242 reload(path)
241 monkeypatch.setattr(path, "get_home_dir", lambda: HOME_TEST_DIR)
243 path.get_home_dir = lambda : HOME_TEST_DIR
242 monkeypatch.setattr(os, "name", "nt")
244 os.name = "nt"
243 monkeypatch.setattr(sys, "platform", "win32")
245 sys.platform = "win32"
244 env.pop("IPYTHON_DIR", None)
246 env.pop('IPYTHON_DIR', None)
245 env.pop("IPYTHONDIR", None)
247 env.pop('IPYTHONDIR', None)
246 env.pop("XDG_CONFIG_HOME", None)
248 env.pop('XDG_CONFIG_HOME', None)
247 cfgdir = os.path.join(path.get_home_dir(), ".config")
249 cfgdir=os.path.join(path.get_home_dir(), '.config')
250 os.makedirs(cfgdir, exist_ok=True)
248 os.makedirs(cfgdir, exist_ok=True)
251
249
252 assert path.get_xdg_dir() is None
250 assert path.get_xdg_dir() is None
General Comments 0
You need to be logged in to leave comments. Login now