import json
import os
from os.path import join as pjoin
import unittest

from IPython.utils.tempdir import TemporaryDirectory
from IPython.kernel import kernelspec

sample_kernel_json = {'argv':['cat', '{connection_file}'],
                      'display_name':'Test kernel',
                      'language':'bash',
                     }

class KernelSpecTests(unittest.TestCase):
    def setUp(self):
        self.tempdir = td = TemporaryDirectory()
        self.sample_kernel_dir = pjoin(td.name, 'kernels', 'Sample')
        os.makedirs(self.sample_kernel_dir)
        json_file = pjoin(self.sample_kernel_dir, 'kernel.json')
        with open(json_file, 'w') as f:
            json.dump(sample_kernel_json, f)

        self.ksm = kernelspec.KernelSpecManager(ipython_dir=td.name)

    def tearDown(self):
        self.tempdir.cleanup()

    def test_find_kernel_specs(self):
        kernels = self.ksm.find_kernel_specs()
        self.assertEqual(kernels['sample'], self.sample_kernel_dir)

    def test_get_kernel_spec(self):
        ks = self.ksm.get_kernel_spec('SAMPLE')  # Case insensitive
        self.assertEqual(ks.resource_dir, self.sample_kernel_dir)
        self.assertEqual(ks.argv, sample_kernel_json['argv'])
        self.assertEqual(ks.display_name, sample_kernel_json['display_name'])
        self.assertEqual(ks.language, sample_kernel_json['language'])
        self.assertEqual(ks.codemirror_mode, sample_kernel_json['language'])
        self.assertEqual(ks.env, {})
@@ -66,16 +66,62 @@
 ittest%0A%0A
+from IPython.testing.decorators import onlyif%0A
 from IPy
@@ -165,16 +165,16 @@
 rectory%0A
-
 from IPy
@@ -454,23 +454,8 @@
     
- self.tempdir =
  td 
@@ -477,16 +477,52 @@
 ctory()%0A
+        self.addCleanup(td.cleanup)%0A
         
@@ -850,62 +850,251 @@
 me)%0A
-%0A    def tearDown(self):%0A        self.tempdir.cleanup(
+        %0A        td2 = TemporaryDirectory()%0A        self.addCleanup(td2.cleanup)%0A        self.installable_kernel = td2.name%0A        with open(pjoin(self.installable_kernel, 'kernel.json'), 'w') as f:%0A            json.dump(sample_kernel_json, f
 )%0A%0A 
@@ -1695,32 +1695,32 @@
 on%5B'language'%5D)%0A
-
         self.ass
@@ -1739,8 +1739,1028 @@
 env, %7B%7D)
+%0A    %0A    def test_install_kernel_spec(self):%0A        self.ksm.install_kernel_spec(self.installable_kernel,%0A                                     kernel_name='tstinstalled')%0A        self.assertIn('tstinstalled', self.ksm.find_kernel_specs())%0A        %0A        with self.assertRaises(OSError):%0A            self.ksm.install_kernel_spec(self.installable_kernel,%0A                                         kernel_name='tstinstalled')%0A        %0A        # Smoketest that this succeeds%0A        self.ksm.install_kernel_spec(self.installable_kernel,%0A                                     kernel_name='tstinstalled',%0A                                     replace=True)%0A    %0A    @onlyif(os.name != 'nt' and os.getuid() != 0, %22needs Unix system and non-root uid%22)%0A    def test_cant_install_kernel_spec(self):%0A        with self.assertRaises(OSError):%0A            self.ksm.install_kernel_spec(self.installable_kernel,%0A                                         kernel_name='tstinstalled',%0A                                         system=True)%0A

Add test for installing kernel spec