1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
import platform
import shutil
import sys
import tempfile
from unittest import mock
import pytest
from jupyter_core.paths import jupyter_data_dir
from ipykernel.kernelspec import (
KERNEL_NAME,
RESOURCES,
InstallIPythonKernelSpecApp,
get_kernel_dict,
install,
make_ipkernel_cmd,
write_kernel_spec,
)
pjoin = os.path.join
is_cpython = platform.python_implementation() == "CPython"
def test_make_ipkernel_cmd():
cmd = make_ipkernel_cmd()
assert cmd == [sys.executable, "-m", "ipykernel_launcher", "-f", "{connection_file}"]
def assert_kernel_dict(d):
assert d["argv"] == make_ipkernel_cmd()
assert d["display_name"] == "Python %i (ipykernel)" % sys.version_info[0]
assert d["language"] == "python"
def test_get_kernel_dict():
d = get_kernel_dict()
assert_kernel_dict(d)
def assert_kernel_dict_with_profile(d):
assert d["argv"] == make_ipkernel_cmd(extra_arguments=["--profile", "test"])
assert d["display_name"] == "Python %i (ipykernel)" % sys.version_info[0]
assert d["language"] == "python"
def test_get_kernel_dict_with_profile():
d = get_kernel_dict(["--profile", "test"])
assert_kernel_dict_with_profile(d)
def assert_is_spec(path):
for fname in os.listdir(RESOURCES):
dst = pjoin(path, fname)
assert os.path.exists(dst)
kernel_json = pjoin(path, "kernel.json")
assert os.path.exists(kernel_json)
with open(kernel_json, encoding="utf8") as f:
json.load(f)
def test_write_kernel_spec():
path = write_kernel_spec()
assert_is_spec(path)
shutil.rmtree(path)
def test_write_kernel_spec_path():
path = os.path.join(tempfile.mkdtemp(), KERNEL_NAME)
path2 = write_kernel_spec(path)
assert path == path2
assert_is_spec(path)
shutil.rmtree(path)
def test_install_kernelspec():
path = tempfile.mkdtemp()
try:
InstallIPythonKernelSpecApp.launch_instance(argv=["--prefix", path])
assert_is_spec(os.path.join(path, "share", "jupyter", "kernels", KERNEL_NAME))
finally:
shutil.rmtree(path)
def test_install_user():
tmp = tempfile.mkdtemp()
with mock.patch.dict(os.environ, {"HOME": tmp}):
install(user=True)
data_dir = jupyter_data_dir()
assert_is_spec(os.path.join(data_dir, "kernels", KERNEL_NAME))
def test_install():
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install()
assert_is_spec(os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME))
def test_install_profile():
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install(profile="Test")
spec_file = os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME, "kernel.json")
with open(spec_file) as f:
spec = json.load(f)
assert spec["display_name"].endswith(" [profile=Test]")
assert spec["argv"][-2:] == ["--profile", "Test"]
def test_install_display_name_overrides_profile():
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install(display_name="Display", profile="Test")
spec_file = os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME, "kernel.json")
with open(spec_file) as f:
spec = json.load(f)
assert spec["display_name"] == "Display"
@pytest.mark.parametrize("env", [None, dict(spam="spam"), dict(spam="spam", foo="bar")])
def test_install_env(tmp_path, env):
# python 3.5 // tmp_path must be converted to str
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [str(tmp_path)]):
install(env=env)
spec = tmp_path / "kernels" / KERNEL_NAME / "kernel.json"
with spec.open() as f:
spec = json.load(f)
if env:
assert len(env) == len(spec["env"])
for k, v in env.items():
assert spec["env"][k] == v
else:
assert "env" not in spec
@pytest.mark.skipif(sys.version_info < (3, 11) or not is_cpython, reason="requires cPython 3.11")
def test_install_frozen_modules_on():
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install(frozen_modules=True)
spec_file = os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME, "kernel.json")
with open(spec_file) as f:
spec = json.load(f)
assert spec["env"]["PYDEVD_DISABLE_FILE_VALIDATION"] == "1"
assert "-Xfrozen_modules=off" not in spec["argv"]
@pytest.mark.skipif(sys.version_info < (3, 11) or not is_cpython, reason="requires cPython 3.11")
def test_install_frozen_modules_off():
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install(frozen_modules=False)
spec_file = os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME, "kernel.json")
with open(spec_file) as f:
spec = json.load(f)
assert "env" not in spec
assert spec["argv"][1] == "-Xfrozen_modules=off"
@pytest.mark.skipif(
sys.version_info >= (3, 11) or is_cpython,
reason="checks versions older than 3.11 and other Python implementations",
)
def test_install_frozen_modules_no_op():
# ensure we do not add add Xfrozen_modules on older Python versions
# (although cPython does not error out on unknown X options as of 3.8)
system_jupyter_dir = tempfile.mkdtemp()
with mock.patch("jupyter_client.kernelspec.SYSTEM_JUPYTER_PATH", [system_jupyter_dir]):
install(frozen_modules=False)
spec_file = os.path.join(system_jupyter_dir, "kernels", KERNEL_NAME, "kernel.json")
with open(spec_file) as f:
spec = json.load(f)
assert "-Xfrozen_modules=off" not in spec["argv"]
|