Debian Codegraph @

tests/test_kernelspec.py

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