# Path: metakernel/tests/utils.py
# def get_kernel(kernel_class=MetaKernel):
#     context = zmq.Context.instance()
#     iopub_socket = context.socket(zmq.PUB)
# 
#     kernel = kernel_class(session=ss.Session(), iopub_socket=iopub_socket,
#                           log=get_log())
#     return kernel
# 
# def get_log_text(obj):
#     """Get the log text from a kernel or a log object."""
#     if isinstance(obj, MetaKernel):
#         log = obj.log
#     else:
#         log = obj
#     return log.handlers[0].stream.getvalue()


from metakernel.tests.utils import get_kernel, get_log_text
import os
# force locale to C to get consistent error messages
os.environ["LC_ALL"] = "C"
os.environ["LANG"] = "C"
os.environ["LANGUAGE"] = "C"

def test_shell_magic():
    kernel = get_kernel()

    text = '%shell ech'
    comp = kernel.do_complete(text, len(text))

    assert 'echo' in comp['matches']

    helpstr = kernel.get_help_on('!cat')
    assert not 'Sorry, no help' in helpstr, helpstr

    helpstr = kernel.get_help_on('%%shell cat', level=1)
    assert not 'Sorry, no help' in helpstr

    helpstr = kernel.get_help_on('!lkjalskdfj')
    assert 'Sorry, no help' in helpstr


def test_shell_magic2():
    kernel = get_kernel()
    kernel.do_execute("!cat \"%s\"" % __file__, False)
    log_text = get_log_text(kernel)
    assert 'metakernel.py' in log_text

    kernel.do_execute('!!\necho "hello"\necho "goodbye"', None)

# Path: metakernel/tests/utils.py
# def get_kernel(kernel_class=MetaKernel):
#     context = zmq.Context.instance()
#     iopub_socket = context.socket(zmq.PUB)
# 
#     kernel = kernel_class(session=ss.Session(), iopub_socket=iopub_socket,
#                           log=get_log())
#     return kernel
# 
# def get_log_text(obj):
#     """Get the log text from a kernel or a log object."""
#     if isinstance(obj, MetaKernel):
#         log = obj.log
#     else:
#         log = obj
#     return log.handlers[0].stream.getvalue()


# Path: metakernel/magics/tests/test_shell_magic.py
from metakernel.tests.utils import get_kernel, get_log_text
import os

# force locale to C to get consistent error messages
os.environ["LC_ALL"] = "C"
os.environ["LANG"] = "C"
os.environ["LANGUAGE"] = "C"

def test_shell_magic():
    kernel = get_kernel()

    text = '%shell ech'
    comp = kernel.do_complete(text, len(text))

    assert 'echo' in comp['matches']

    helpstr = kernel.get_help_on('!cat')
    assert not 'Sorry, no help' in helpstr, helpstr

    helpstr = kernel.get_help_on('%%shell cat', level=1)
    assert not 'Sorry, no help' in helpstr

    helpstr = kernel.get_help_on('!lkjalskdfj')
    assert 'Sorry, no help' in helpstr


def test_shell_magic2():
    kernel = get_kernel()
    kernel.do_execute("!cat \"%s\"" % __file__, False)
    log_text = get_log_text(kernel)
    assert 'metakernel.py' in log_text

    kernel.do_execute('!!\necho "hello"\necho "goodbye"', None)

    log_text = get_log_text(kernel)