test_kernelspec.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) IPython Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import json
  4. import io
  5. import os
  6. import shutil
  7. import sys
  8. import tempfile
  9. try:
  10. from unittest import mock
  11. except ImportError:
  12. import mock # py2
  13. from jupyter_core.paths import jupyter_data_dir
  14. from ipykernel.kernelspec import (
  15. make_ipkernel_cmd,
  16. get_kernel_dict,
  17. write_kernel_spec,
  18. install,
  19. InstallIPythonKernelSpecApp,
  20. KERNEL_NAME,
  21. RESOURCES,
  22. )
  23. import nose.tools as nt
  24. pjoin = os.path.join
  25. def test_make_ipkernel_cmd():
  26. cmd = make_ipkernel_cmd()
  27. nt.assert_equal(cmd, [
  28. sys.executable,
  29. '-m',
  30. 'ipykernel_launcher',
  31. '-f',
  32. '{connection_file}'
  33. ])
  34. def assert_kernel_dict(d):
  35. assert d['argv'] == make_ipkernel_cmd()
  36. assert d['display_name'] == 'Python %i' % sys.version_info[0]
  37. assert d['language'] == 'python'
  38. def test_get_kernel_dict():
  39. d = get_kernel_dict()
  40. assert_kernel_dict(d)
  41. def assert_kernel_dict_with_profile(d):
  42. nt.assert_equal(d['argv'], make_ipkernel_cmd(
  43. extra_arguments=["--profile", "test"]))
  44. assert d['display_name'] == 'Python %i' % sys.version_info[0]
  45. assert d['language'] == 'python'
  46. def test_get_kernel_dict_with_profile():
  47. d = get_kernel_dict(["--profile", "test"])
  48. assert_kernel_dict_with_profile(d)
  49. def assert_is_spec(path):
  50. for fname in os.listdir(RESOURCES):
  51. dst = pjoin(path, fname)
  52. assert os.path.exists(dst)
  53. kernel_json = pjoin(path, 'kernel.json')
  54. assert os.path.exists(kernel_json)
  55. with io.open(kernel_json, encoding='utf8') as f:
  56. json.load(f)
  57. def test_write_kernel_spec():
  58. path = write_kernel_spec()
  59. assert_is_spec(path)
  60. shutil.rmtree(path)
  61. def test_write_kernel_spec_path():
  62. path = os.path.join(tempfile.mkdtemp(), KERNEL_NAME)
  63. path2 = write_kernel_spec(path)
  64. assert path == path2
  65. assert_is_spec(path)
  66. shutil.rmtree(path)
  67. def test_install_kernelspec():
  68. path = tempfile.mkdtemp()
  69. try:
  70. test = InstallIPythonKernelSpecApp.launch_instance(argv=['--prefix', path])
  71. assert_is_spec(os.path.join(
  72. path, 'share', 'jupyter', 'kernels', KERNEL_NAME))
  73. finally:
  74. shutil.rmtree(path)
  75. def test_install_user():
  76. tmp = tempfile.mkdtemp()
  77. with mock.patch.dict(os.environ, {'HOME': tmp}):
  78. install(user=True)
  79. data_dir = jupyter_data_dir()
  80. assert_is_spec(os.path.join(data_dir, 'kernels', KERNEL_NAME))
  81. def test_install():
  82. system_jupyter_dir = tempfile.mkdtemp()
  83. with mock.patch('jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH',
  84. [system_jupyter_dir]):
  85. install()
  86. assert_is_spec(os.path.join(system_jupyter_dir, 'kernels', KERNEL_NAME))
  87. def test_install_profile():
  88. system_jupyter_dir = tempfile.mkdtemp()
  89. with mock.patch('jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH',
  90. [system_jupyter_dir]):
  91. install(profile="Test")
  92. spec = os.path.join(system_jupyter_dir, 'kernels', KERNEL_NAME, "kernel.json")
  93. with open(spec) as f:
  94. spec = json.load(f)
  95. assert spec["display_name"].endswith(" [profile=Test]")
  96. nt.assert_equal(spec["argv"][-2:], ["--profile", "Test"])
  97. def test_install_display_name_overrides_profile():
  98. system_jupyter_dir = tempfile.mkdtemp()
  99. with mock.patch('jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH',
  100. [system_jupyter_dir]):
  101. install(display_name="Display", profile="Test")
  102. spec = os.path.join(system_jupyter_dir, 'kernels', KERNEL_NAME, "kernel.json")
  103. with open(spec) as f:
  104. spec = json.load(f)
  105. assert spec["display_name"] == "Display"