Show More
@@ -1,39 +1,65 b'' | |||||
1 | import json |
|
1 | import json | |
2 | import os |
|
2 | import os | |
3 | from os.path import join as pjoin |
|
3 | from os.path import join as pjoin | |
4 | import unittest |
|
4 | import unittest | |
5 |
|
5 | |||
|
6 | from IPython.testing.decorators import onlyif | |||
6 | from IPython.utils.tempdir import TemporaryDirectory |
|
7 | from IPython.utils.tempdir import TemporaryDirectory | |
7 | from IPython.kernel import kernelspec |
|
8 | from IPython.kernel import kernelspec | |
8 |
|
9 | |||
9 | sample_kernel_json = {'argv':['cat', '{connection_file}'], |
|
10 | sample_kernel_json = {'argv':['cat', '{connection_file}'], | |
10 | 'display_name':'Test kernel', |
|
11 | 'display_name':'Test kernel', | |
11 | 'language':'bash', |
|
12 | 'language':'bash', | |
12 | } |
|
13 | } | |
13 |
|
14 | |||
14 | class KernelSpecTests(unittest.TestCase): |
|
15 | class KernelSpecTests(unittest.TestCase): | |
15 | def setUp(self): |
|
16 | def setUp(self): | |
16 |
|
|
17 | td = TemporaryDirectory() | |
|
18 | self.addCleanup(td.cleanup) | |||
17 | self.sample_kernel_dir = pjoin(td.name, 'kernels', 'Sample') |
|
19 | self.sample_kernel_dir = pjoin(td.name, 'kernels', 'Sample') | |
18 | os.makedirs(self.sample_kernel_dir) |
|
20 | os.makedirs(self.sample_kernel_dir) | |
19 | json_file = pjoin(self.sample_kernel_dir, 'kernel.json') |
|
21 | json_file = pjoin(self.sample_kernel_dir, 'kernel.json') | |
20 | with open(json_file, 'w') as f: |
|
22 | with open(json_file, 'w') as f: | |
21 | json.dump(sample_kernel_json, f) |
|
23 | json.dump(sample_kernel_json, f) | |
22 |
|
24 | |||
23 | self.ksm = kernelspec.KernelSpecManager(ipython_dir=td.name) |
|
25 | self.ksm = kernelspec.KernelSpecManager(ipython_dir=td.name) | |
24 |
|
26 | |||
25 | def tearDown(self): |
|
27 | td2 = TemporaryDirectory() | |
26 |
self. |
|
28 | self.addCleanup(td2.cleanup) | |
|
29 | self.installable_kernel = td2.name | |||
|
30 | with open(pjoin(self.installable_kernel, 'kernel.json'), 'w') as f: | |||
|
31 | json.dump(sample_kernel_json, f) | |||
27 |
|
32 | |||
28 | def test_find_kernel_specs(self): |
|
33 | def test_find_kernel_specs(self): | |
29 | kernels = self.ksm.find_kernel_specs() |
|
34 | kernels = self.ksm.find_kernel_specs() | |
30 | self.assertEqual(kernels['sample'], self.sample_kernel_dir) |
|
35 | self.assertEqual(kernels['sample'], self.sample_kernel_dir) | |
31 |
|
36 | |||
32 | def test_get_kernel_spec(self): |
|
37 | def test_get_kernel_spec(self): | |
33 | ks = self.ksm.get_kernel_spec('SAMPLE') # Case insensitive |
|
38 | ks = self.ksm.get_kernel_spec('SAMPLE') # Case insensitive | |
34 | self.assertEqual(ks.resource_dir, self.sample_kernel_dir) |
|
39 | self.assertEqual(ks.resource_dir, self.sample_kernel_dir) | |
35 | self.assertEqual(ks.argv, sample_kernel_json['argv']) |
|
40 | self.assertEqual(ks.argv, sample_kernel_json['argv']) | |
36 | self.assertEqual(ks.display_name, sample_kernel_json['display_name']) |
|
41 | self.assertEqual(ks.display_name, sample_kernel_json['display_name']) | |
37 | self.assertEqual(ks.language, sample_kernel_json['language']) |
|
42 | self.assertEqual(ks.language, sample_kernel_json['language']) | |
38 | self.assertEqual(ks.codemirror_mode, sample_kernel_json['language']) |
|
43 | self.assertEqual(ks.codemirror_mode, sample_kernel_json['language']) | |
39 | self.assertEqual(ks.env, {}) |
|
44 | self.assertEqual(ks.env, {}) | |
|
45 | ||||
|
46 | def test_install_kernel_spec(self): | |||
|
47 | self.ksm.install_kernel_spec(self.installable_kernel, | |||
|
48 | kernel_name='tstinstalled') | |||
|
49 | self.assertIn('tstinstalled', self.ksm.find_kernel_specs()) | |||
|
50 | ||||
|
51 | with self.assertRaises(OSError): | |||
|
52 | self.ksm.install_kernel_spec(self.installable_kernel, | |||
|
53 | kernel_name='tstinstalled') | |||
|
54 | ||||
|
55 | # Smoketest that this succeeds | |||
|
56 | self.ksm.install_kernel_spec(self.installable_kernel, | |||
|
57 | kernel_name='tstinstalled', | |||
|
58 | replace=True) | |||
|
59 | ||||
|
60 | @onlyif(os.name != 'nt' and os.getuid() != 0, "needs Unix system and non-root uid") | |||
|
61 | def test_cant_install_kernel_spec(self): | |||
|
62 | with self.assertRaises(OSError): | |||
|
63 | self.ksm.install_kernel_spec(self.installable_kernel, | |||
|
64 | kernel_name='tstinstalled', | |||
|
65 | system=True) |
General Comments 0
You need to be logged in to leave comments.
Login now