From 46f66ffe8ed8de24da5dfa89946acfb1db13ee47 2014-10-01 19:17:20 From: MinRK Date: 2014-10-01 19:17:20 Subject: [PATCH] Backport PR #6581: Properly mock winreg functions for test Fixes a lot of test failures on Python 3.4 on Windows, caused by importlib trying to use our mocked out winreg functions when reload(path) was called. --- diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index 4fe0b32..d681498 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -22,6 +22,11 @@ import tempfile import warnings from contextlib import contextmanager +try: # Python 3.3+ + from unittest.mock import patch +except ImportError: + from mock import patch + from os.path import join, abspath, split import nose.tools as nt @@ -107,10 +112,6 @@ def setup_environment(): global oldstuff, platformstuff oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) - if os.name == 'nt': - platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) - - def teardown_environment(): """Restore things that were remembered by the setup_environment function """ @@ -124,8 +125,6 @@ def teardown_environment(): env.update(oldenv) if hasattr(sys, 'frozen'): del sys.frozen - if os.name == 'nt': - (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff # Build decorator that uses the setup_environment/setup_environment with_environment = with_setup(setup_environment, teardown_environment) @@ -193,7 +192,6 @@ def test_get_home_dir_5(): os.name = 'posix' nt.assert_raises(path.HomeDirError, path.get_home_dir, True) - # Should we stub wreg fully so we can run the test on all platforms? @skip_if_not_win32 @with_environment @@ -207,19 +205,13 @@ def test_get_home_dir_8(): for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: env.pop(key, None) - #Stub windows registry functions - def OpenKey(x, y): - class key: - def Close(self): - pass - return key() - def QueryValueEx(x, y): - return [abspath(HOME_TEST_DIR)] - - wreg.OpenKey = OpenKey - wreg.QueryValueEx = QueryValueEx + class key: + def Close(self): + pass - home_dir = path.get_home_dir() + with patch.object(wreg, 'OpenKey', return_value=key()), \ + patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]): + home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))