Debian Codegraph @

tests/__init__.py

raw
1 # Copyright (c) IPython Development Team.
2 # Distributed under the terms of the Modified BSD License.
3
4 import os
5 import shutil
6 import sys
7 import tempfile
8 from unittest.mock import patch
9
10 import pytest
11
12 from ipykernel.kernelspec import install
13
14 pjoin = os.path.join
15
16 tmp = None
17 patchers: list = []
18
19
20 @pytest.fixture(autouse=True)
21 def _global_setup():
22 """setup temporary env for tests"""
23 global tmp
24 tmp = tempfile.mkdtemp()
25 patchers[:] = [
26 patch.dict(
27 os.environ,
28 {
29 "HOME": tmp,
30 # Let tests work with --user install when HOME is changed:
31 "PYTHONPATH": os.pathsep.join(sys.path),
32 },
33 ),
34 ]
35 for p in patchers:
36 p.start()
37
38 # install IPython in the temp home:
39 install(user=True)
40 yield
41 for p in patchers:
42 p.stop()
43
44 try:
45 shutil.rmtree(tmp) # type:ignore
46 except OSError:
47 # no such file
48 pass