##// END OF EJS Templates
Properly mock winreg functions for test...
Thomas Kluyver -
Show More
@@ -12,6 +12,11 b' import tempfile'
12 12 import warnings
13 13 from contextlib import contextmanager
14 14
15 try: # Python 3.3+
16 from unittest.mock import patch
17 except ImportError:
18 from mock import patch
19
15 20 from os.path import join, abspath, split
16 21
17 22 from nose import SkipTest
@@ -98,10 +103,6 b' def setup_environment():'
98 103 global oldstuff, platformstuff
99 104 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
100 105
101 if os.name == 'nt':
102 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
103
104
105 106 def teardown_environment():
106 107 """Restore things that were remembered by the setup_environment function
107 108 """
@@ -115,8 +116,6 b' def teardown_environment():'
115 116 env.update(oldenv)
116 117 if hasattr(sys, 'frozen'):
117 118 del sys.frozen
118 if os.name == 'nt':
119 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
120 119
121 120 # Build decorator that uses the setup_environment/setup_environment
122 121 with_environment = with_setup(setup_environment, teardown_environment)
@@ -184,7 +183,6 b' def test_get_home_dir_5():'
184 183 os.name = 'posix'
185 184 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
186 185
187
188 186 # Should we stub wreg fully so we can run the test on all platforms?
189 187 @skip_if_not_win32
190 188 @with_environment
@@ -198,18 +196,12 b' def test_get_home_dir_8():'
198 196 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
199 197 env.pop(key, None)
200 198
201 #Stub windows registry functions
202 def OpenKey(x, y):
203 199 class key:
204 200 def Close(self):
205 201 pass
206 return key()
207 def QueryValueEx(x, y):
208 return [abspath(HOME_TEST_DIR)]
209
210 wreg.OpenKey = OpenKey
211 wreg.QueryValueEx = QueryValueEx
212 202
203 with patch.object(wreg, 'OpenKey', return_value=key()), \
204 patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]):
213 205 home_dir = path.get_home_dir()
214 206 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
215 207
General Comments 0
You need to be logged in to leave comments. Login now