##// END OF EJS Templates
Properly mock winreg functions for test...
Thomas Kluyver -
Show More
@@ -12,6 +12,11 b' 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+
16 from unittest.mock import patch
17 except ImportError:
18 from mock import patch
19
15 from os.path import join, abspath, split
20 from os.path import join, abspath, split
16
21
17 from nose import SkipTest
22 from nose import SkipTest
@@ -98,10 +103,6 b' def setup_environment():'
98 global oldstuff, platformstuff
103 global oldstuff, platformstuff
99 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
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 def teardown_environment():
106 def teardown_environment():
106 """Restore things that were remembered by the setup_environment function
107 """Restore things that were remembered by the setup_environment function
107 """
108 """
@@ -115,8 +116,6 b' def teardown_environment():'
115 env.update(oldenv)
116 env.update(oldenv)
116 if hasattr(sys, 'frozen'):
117 if hasattr(sys, 'frozen'):
117 del sys.frozen
118 del sys.frozen
118 if os.name == 'nt':
119 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
120
119
121 # Build decorator that uses the setup_environment/setup_environment
120 # Build decorator that uses the setup_environment/setup_environment
122 with_environment = with_setup(setup_environment, teardown_environment)
121 with_environment = with_setup(setup_environment, teardown_environment)
@@ -184,7 +183,6 b' def test_get_home_dir_5():'
184 os.name = 'posix'
183 os.name = 'posix'
185 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
184 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
186
185
187
188 # Should we stub wreg fully so we can run the test on all platforms?
186 # Should we stub wreg fully so we can run the test on all platforms?
189 @skip_if_not_win32
187 @skip_if_not_win32
190 @with_environment
188 @with_environment
@@ -198,18 +196,12 b' def test_get_home_dir_8():'
198 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
196 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
199 env.pop(key, None)
197 env.pop(key, None)
200
198
201 #Stub windows registry functions
202 def OpenKey(x, y):
203 class key:
199 class key:
204 def Close(self):
200 def Close(self):
205 pass
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 home_dir = path.get_home_dir()
205 home_dir = path.get_home_dir()
214 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
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