| 1 |
"""Tests for KernelManager""" |
| 2 |
|
| 3 |
# Copyright (c) Jupyter Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
import os |
| 6 |
import tempfile |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
from jupyter_client.kernelspec import KernelSpec |
| 10 |
from jupyter_client.manager import KernelManager |
| 11 |
|
| 12 |
|
| 13 |
def test_connection_file_real_path(): |
| 14 |
"""Verify realpath is used when formatting connection file""" |
| 15 |
with mock.patch("os.path.realpath") as patched_realpath: |
| 16 |
patched_realpath.return_value = "foobar" |
| 17 |
km = KernelManager( |
| 18 |
connection_file=os.path.join(tempfile.gettempdir(), "kernel-test.json"), |
| 19 |
kernel_name="test_kernel", |
| 20 |
) |
| 21 |
|
| 22 |
# KernelSpec and launch args have to be mocked as we don't have an actual kernel on disk |
| 23 |
km._kernel_spec = KernelSpec( |
| 24 |
resource_dir="test", |
| 25 |
**{ |
| 26 |
"argv": ["python.exe", "-m", "test_kernel", "-f", "{connection_file}"], |
| 27 |
"env": {}, |
| 28 |
"display_name": "test_kernel", |
| 29 |
"language": "python", |
| 30 |
"metadata": {}, |
| 31 |
}, |
| 32 |
) |
| 33 |
km._launch_args = {} |
| 34 |
cmds = km.format_kernel_cmd() |
| 35 |
assert cmds[4] == "foobar" |
| 36 |
|
| 37 |
|
| 38 |
def test_env_update_launch_args_not_set(): |
| 39 |
km = KernelManager() |
| 40 |
km.update_env(env={"A": "A"}) |
| 41 |
|
| 42 |
|
| 43 |
def test_env_update_launch_args_not_dict(): |
| 44 |
km = KernelManager() |
| 45 |
km._launch_args = None |
| 46 |
km.update_env(env={"B": "B"}) |
| 47 |
|
| 48 |
|
| 49 |
def test_env_update_launch_args_no_env(): |
| 50 |
km = KernelManager() |
| 51 |
km._launch_args = {} |
| 52 |
km.update_env(env={"C": "C"}) |
| 53 |
|
| 54 |
|
| 55 |
def test_env_update_launch_args_env_not_dict(): |
| 56 |
km = KernelManager() |
| 57 |
km._launch_args = {"env": None} |
| 58 |
km.update_env(env={"D": "D"}) |
| 59 |
|
| 60 |
|
| 61 |
def test_env_update_launch_args_env_dic(): |
| 62 |
km = KernelManager() |
| 63 |
km._launch_args = {"env": {}} |
| 64 |
km.update_env(env={"E": "E"}) |
| 65 |
assert km._launch_args["env"]["E"] == "E" |