<s> """<STR_LIT>""" <EOL> import io <EOL> import json <EOL> from logging import StreamHandler <EOL> import os <EOL> from os . path import join as pjoin <EOL> from subprocess import Popen , PIPE , STDOUT <EOL> import sys <EOL> import unittest <EOL> if str is bytes : <EOL> StringIO = io . BytesIO <EOL> else : <EOL> StringIO = io . StringIO <EOL> from ipython_genutils . testing . decorators import onlyif <EOL> from ipython_genutils . tempdir import TemporaryDirectory <EOL> from jupyter_client import kernelspec <EOL> from jupyter_core import paths <EOL> from . utils import test_env <EOL> sample_kernel_json = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class KernelSpecTests ( unittest . TestCase ) : <EOL> def _install_sample_kernel ( self , kernels_dir ) : <EOL> """<STR_LIT>""" <EOL> sample_kernel_dir = pjoin ( kernels_dir , '<STR_LIT>' ) <EOL> os . makedirs ( sample_kernel_dir ) <EOL> json_file = pjoin ( sample_kernel_dir , '<STR_LIT>' ) <EOL> with open ( json_file , '<STR_LIT:w>' ) as f : <EOL> json . dump ( sample_kernel_json , f ) <EOL> return sample_kernel_dir <EOL> def setUp ( self ) : <EOL> self . env_patch = test_env ( ) <EOL> self . env_patch . start ( ) <EOL> self . sample_kernel_dir = self . _install_sample_kernel ( <EOL> pjoin ( paths . jupyter_data_dir ( ) , '<STR_LIT>' ) ) <EOL> self . ksm = kernelspec . KernelSpecManager ( ) <EOL> td2 = TemporaryDirectory ( ) <EOL> self . addCleanup ( td2 . cleanup ) <EOL> self . installable_kernel = td2 . name <EOL> with open ( pjoin ( self . installable_kernel , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> json . dump ( sample_kernel_json , f ) <EOL> def tearDown ( self ) : <EOL> self . env_patch . stop ( ) <EOL> def test_find_kernel_specs ( self ) : <EOL> kernels = self . ksm . find_kernel_specs ( ) <EOL> self . assertEqual ( kernels [ '<STR_LIT>' ] , self . sample_kernel_dir ) <EOL> def test_get_kernel_spec ( self ) : <EOL> ks = self . ksm . get_kernel_spec ( '<STR_LIT>' ) <EOL> self . assertEqual ( ks . resource_dir , self . sample_kernel_dir ) <EOL> self . assertEqual ( ks . argv , sample_kernel_json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( ks . display_name , sample_kernel_json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( ks . env , { } ) <EOL> def test_find_all_specs ( self ) : <EOL> kernels = self . ksm . get_all_specs ( ) <EOL> self . assertEqual ( kernels [ '<STR_LIT>' ] [ '<STR_LIT>' ] , self . sample_kernel_dir ) <EOL> self . assertIsNotNone ( kernels [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def test_kernel_spec_priority ( self ) : <EOL> td = TemporaryDirectory ( ) <EOL> self . addCleanup ( td . cleanup ) <EOL> sample_kernel = self . _install_sample_kernel ( td . name ) <EOL> self . ksm . kernel_dirs . append ( td . name ) <EOL> kernels = self . ksm . find_kernel_specs ( ) <EOL> self . assertEqual ( kernels [ '<STR_LIT>' ] , self . sample_kernel_dir ) <EOL> self . ksm . kernel_dirs . insert ( <NUM_LIT:0> , td . name ) <EOL> kernels = self . ksm . find_kernel_specs ( ) <EOL> self . assertEqual ( kernels [ '<STR_LIT>' ] , sample_kernel ) <EOL> def test_install_kernel_spec ( self ) : <EOL> self . ksm . install_kernel_spec ( self . installable_kernel , <EOL> kernel_name = '<STR_LIT>' , <EOL> user = True ) <EOL> self . assertIn ( '<STR_LIT>' , self . ksm . find_kernel_specs ( ) ) <EOL> self . ksm . install_kernel_spec ( self . installable_kernel , <EOL> kernel_name = '<STR_LIT>' , <EOL> user = True ) <EOL> def test_install_kernel_spec_prefix ( self ) : <EOL> td = TemporaryDirectory ( ) <EOL> self . addCleanup ( td . cleanup ) <EOL> capture = StringIO ( ) <EOL> handler = StreamHandler ( capture ) <EOL> self . ksm . log . addHandler ( handler ) <EOL> self . ksm . install_kernel_spec ( self . installable_kernel , <EOL> kernel_name = '<STR_LIT>' , <EOL> prefix = td . name ) <EOL> captured = capture . getvalue ( ) <EOL> self . ksm . log . removeHandler ( handler ) <EOL> self . assertIn ( "<STR_LIT>" , captured ) <EOL> self . assertNotIn ( '<STR_LIT>' , self . ksm . find_kernel_specs ( ) ) <EOL> self . ksm . kernel_dirs . append ( pjoin ( td . name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertIn ( '<STR_LIT>' , self . ksm . find_kernel_specs ( ) ) <EOL> capture = StringIO ( ) <EOL> handler = StreamHandler ( capture ) <EOL> self . ksm . log . addHandler ( handler ) <EOL> self . ksm . install_kernel_spec ( self . installable_kernel , <EOL> kernel_name = '<STR_LIT>' , <EOL> prefix = td . name ) <EOL> captured = capture . getvalue ( ) <EOL> self . ksm . log . removeHandler ( handler ) <EOL> self . assertNotIn ( "<STR_LIT>" , captured ) <EOL> @ onlyif ( os . name != '<STR_LIT>' and not os . access ( '<STR_LIT>' , os . W_OK ) , "<STR_LIT>" ) <EOL> def test_cant_install_kernel_spec ( self ) : <EOL> with self . assertRaises ( OSError ) : <EOL> self . ksm . install_kernel_spec ( self . installable_kernel , <EOL> kernel_name = '<STR_LIT>' , <EOL> user = False ) <EOL> def test_remove_kernel_spec ( self ) : <EOL> path = self . ksm . remove_kernel_spec ( '<STR_LIT>' ) <EOL> self . assertEqual ( path , self . sample_kernel_dir ) <EOL> def test_remove_kernel_spec_app ( self ) : <EOL> p = Popen ( <EOL> [ sys . executable , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = PIPE , stderr = STDOUT , <EOL> env = os . environ , <EOL> ) <EOL> out , _ = p . communicate ( ) <EOL> self . assertEqual ( p . returncode , <NUM_LIT:0> , out . decode ( '<STR_LIT:utf8>' , '<STR_LIT:replace>' ) ) </s>
