Skip to content

Commit

Permalink
Fix up kernelspec API tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Oct 30, 2018
1 parent 1dfc5d4 commit 76ab738
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions notebook/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(self):
@web.authenticated
def get(self, kernel_name, path, include_body=True):
kf = self.kernel_finder
# TODO: Do we actually want all kernel type names to be case-insensitive?
kernel_name = kernel_name.lower()
for name, info in kf.find_kernels():
if name == kernel_name:
self.root = info['resource_dir']
Expand Down
11 changes: 10 additions & 1 deletion notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,12 @@ def _update_mathjax_config(self, change):
(shutdown the notebook server)."""
)

kernel_providers = List(config=True,
help=_('A list of kernel provider instances. '
'If not specified, all installed kernel providers are found '
'using entry points.')
)

contents_manager_class = Type(
default_value=LargeFileManager,
klass=ContentsManager,
Expand Down Expand Up @@ -1293,7 +1299,10 @@ def parse_command_line(self, argv=None):
self.update_config(c)

def init_configurables(self):
self.kernel_finder = KernelFinder.from_entrypoints()
if self.kernel_providers:
self.kernel_finder = KernelFinder(self.kernel_providers)
else:
self.kernel_finder = KernelFinder.from_entrypoints()
self.kernel_manager = self.kernel_manager_class(
parent=self,
log=self.log,
Expand Down
2 changes: 2 additions & 0 deletions notebook/services/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class KernelSpecHandler(APIHandler):
@web.authenticated
def get(self, kernel_name):
kf = self.kernel_finder
# TODO: Do we actually want all kernel type names to be case-insensitive?
kernel_name = kernel_name.lower()
for name, info in kf.find_kernels():
if name == kernel_name:
model = kernelspec_model(self, kernel_name, info,
Expand Down
24 changes: 13 additions & 11 deletions notebook/services/kernelspecs/tests/test_kernelspecs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import requests

from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
from notebook.utils import url_path_join, url_escape
from notebook.utils import url_path_join, url_escape, quote
from notebook.tests.launchnotebook import NotebookTestBase, assert_http_error

# Copied from jupyter_client.tests.test_kernelspec so updating that doesn't
Expand Down Expand Up @@ -41,9 +41,11 @@ def list(self):
return self._req('GET', 'api/kernelspecs')

def kernel_spec_info(self, name):
name = quote(name, safe='')
return self._req('GET', url_path_join('api/kernelspecs', name))

def kernel_resource(self, name, path):
name = quote(name, safe='')
return self._req('GET', url_path_join('kernelspecs', name, path))


Expand Down Expand Up @@ -102,36 +104,36 @@ def test_list_kernelspecs(self):
self.assertGreaterEqual(len(specs), 2)

def is_sample_kernelspec(s):
return s['name'] == 'sample' and s['spec']['display_name'] == 'Test kernel'
return s['name'] == 'spec/sample' and s['spec']['display_name'] == 'Test kernel'

def is_default_kernelspec(s):
return s['name'] == NATIVE_KERNEL_NAME and s['spec']['display_name'].startswith("Python")
return s['name'] == 'pyimport/kernel' and s['spec']['display_name'].startswith("Python")

assert any(is_sample_kernelspec(s) for s in specs.values()), specs
assert any(is_default_kernelspec(s) for s in specs.values()), specs

def test_get_kernelspec(self):
model = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
self.assertEqual(model['name'].lower(), 'sample')
model = self.ks_api.kernel_spec_info('spec/Sample').json() # Case insensitive
self.assertEqual(model['name'].lower(), 'spec/sample')
self.assertIsInstance(model['spec'], dict)
self.assertEqual(model['spec']['display_name'], 'Test kernel')
self.assertIsInstance(model['resources'], dict)

def test_get_kernelspec_spaces(self):
model = self.ks_api.kernel_spec_info('sample%202').json()
self.assertEqual(model['name'].lower(), 'sample 2')
model = self.ks_api.kernel_spec_info('spec/sample 2').json()
self.assertEqual(model['name'].lower(), 'spec/sample 2')

def test_get_nonexistant_kernelspec(self):
with assert_http_error(404):
self.ks_api.kernel_spec_info('nonexistant')
self.ks_api.kernel_spec_info('spec/nonexistant')

def test_get_kernel_resource_file(self):
res = self.ks_api.kernel_resource('sAmple', 'resource.txt')
res = self.ks_api.kernel_resource('spec/sAmple', 'resource.txt')
self.assertEqual(res.text, some_resource)

def test_get_nonexistant_resource(self):
with assert_http_error(404):
self.ks_api.kernel_resource('nonexistant', 'resource.txt')
self.ks_api.kernel_resource('spec/nonexistant', 'resource.txt')

with assert_http_error(404):
self.ks_api.kernel_resource('sample', 'nonexistant.txt')
self.ks_api.kernel_resource('spec/sample', 'nonexistant.txt')
5 changes: 5 additions & 0 deletions notebook/tests/launchnotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import zmq

import jupyter_core.paths
from jupyter_kernel_mgmt.discovery import KernelSpecProvider, IPykernelProvider
from traitlets.config import Config
from ..notebookapp import NotebookApp
from ..utils import url_path_join
Expand Down Expand Up @@ -146,6 +147,10 @@ def start_thread():
data_dir=cls.data_dir,
runtime_dir=cls.runtime_dir,
notebook_dir=cls.notebook_dir,
kernel_providers=[
KernelSpecProvider(search_path=[pjoin(data_dir, 'kernels')]),
IPykernelProvider(),
],
base_url=cls.url_prefix,
config=config,
allow_root=True,
Expand Down

0 comments on commit 76ab738

Please sign in to comment.