##// END OF EJS Templates
more pytest cleanup
M Bussonnier -
Show More
@@ -75,20 +75,29 b' def teardown_module():'
75 75 shutil.rmtree(TMP_TEST_DIR)
76 76
77 77
78 def setup_environment():
79 """Setup testenvironment for some functions that are tested
80 in this module. In particular this functions stores attributes
81 and other things that we need to stub in some test functions.
82 This needs to be done on a function level and not module level because
83 each testfunction needs a pristine environment.
84 """
78 # Build decorator that uses the setup_environment/setup_environment
79 @pytest.fixture
80 def environment():
85 81 global oldstuff, platformstuff
86 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
82 oldstuff = (
83 env.copy(),
84 os.name,
85 sys.platform,
86 path.get_home_dir,
87 IPython.__file__,
88 os.getcwd(),
89 )
87 90
88 def teardown_environment():
89 """Restore things that were remembered by the setup_environment function
90 """
91 (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
91 yield
92
93 (
94 oldenv,
95 os.name,
96 sys.platform,
97 path.get_home_dir,
98 IPython.__file__,
99 old_wd,
100 ) = oldstuff
92 101 os.chdir(old_wd)
93 102 reload(path)
94 103
@@ -96,16 +105,7 b' def teardown_environment():'
96 105 if key not in oldenv:
97 106 del env[key]
98 107 env.update(oldenv)
99 if hasattr(sys, 'frozen'):
100 del sys.frozen
101
102
103 # Build decorator that uses the setup_environment/setup_environment
104 @pytest.fixture
105 def environment():
106 setup_environment()
107 yield
108 teardown_environment()
108 assert not hasattr(sys, "frozen")
109 109
110 110
111 111 with_environment = pytest.mark.usefixtures("environment")
@@ -117,7 +117,7 b' def test_get_home_dir_1(monkeypatch):'
117 117 """Testcase for py2exe logic, un-compressed lib
118 118 """
119 119 unfrozen = path.get_home_dir()
120 monkeypatch.setattr(sys, "frozen", True)
120 monkeypatch.setattr(sys, "frozen", True, raising=False)
121 121
122 122 #fake filename for IPython.__init__
123 123 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
@@ -132,7 +132,7 b' def test_get_home_dir_2(monkeypatch):'
132 132 """Testcase for py2exe logic, compressed lib
133 133 """
134 134 unfrozen = path.get_home_dir()
135 monkeypatch.setattr(sys, "frozen", True)
135 monkeypatch.setattr(sys, "frozen", True, raising=False)
136 136 # fake filename for IPython.__init__
137 137 IPython.__file__ = abspath(
138 138 join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")
@@ -279,31 +279,30 b' def test_get_long_path_name():'
279 279 assert p == "/usr/local"
280 280
281 281
282 class TestRaiseDeprecation(unittest.TestCase):
282 @dec.skip_win32 # can't create not-user-writable dir on win
283 @with_environment
284 def test_not_writable_ipdir():
285 tmpdir = tempfile.mkdtemp()
286 os.name = "posix"
287 env.pop("IPYTHON_DIR", None)
288 env.pop("IPYTHONDIR", None)
289 env.pop("XDG_CONFIG_HOME", None)
290 env["HOME"] = tmpdir
291 ipdir = os.path.join(tmpdir, ".ipython")
292 os.mkdir(ipdir, 0o555)
293 try:
294 open(os.path.join(ipdir, "_foo_"), "w", encoding="utf-8").close()
295 except IOError:
296 pass
297 else:
298 # I can still write to an unwritable dir,
299 # assume I'm root and skip the test
300 pytest.skip("I can't create directories that I can't write to")
301
302 with pytest.warns(UserWarning, match="is not a writable location"):
303 ipdir = paths.get_ipython_dir()
304 env.pop("IPYTHON_DIR", None)
283 305
284 @dec.skip_win32 # can't create not-user-writable dir on win
285 @with_environment
286 def test_not_writable_ipdir(self):
287 tmpdir = tempfile.mkdtemp()
288 os.name = "posix"
289 env.pop('IPYTHON_DIR', None)
290 env.pop('IPYTHONDIR', None)
291 env.pop('XDG_CONFIG_HOME', None)
292 env['HOME'] = tmpdir
293 ipdir = os.path.join(tmpdir, '.ipython')
294 os.mkdir(ipdir, 0o555)
295 try:
296 open(os.path.join(ipdir, "_foo_"), "w", encoding="utf-8").close()
297 except IOError:
298 pass
299 else:
300 # I can still write to an unwritable dir,
301 # assume I'm root and skip the test
302 pytest.skip("I can't create directories that I can't write to")
303
304 with self.assertWarnsRegex(UserWarning, 'is not a writable location'):
305 ipdir = paths.get_ipython_dir()
306 env.pop('IPYTHON_DIR', None)
307 306
308 307 @with_environment
309 308 def test_get_py_filename():
General Comments 0
You need to be logged in to leave comments. Login now