__init__.py 998 B

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