예제 #1
0
    def test_magic__doc__(self, capsys):
        """
        Test the ``.__doc__`` property of a created ``%magic``.

        The ``--help`` output of the ``%magic`` should be printed

        And test that a :exc:`moreshell.MagicExit` with code ``0`` is raised
        """
        @IPython_magic(with_arguments('value')('-f', '--flag'))
        def magic(shell, args):  # pragma: no cover
            magic.was_not_called = False

        magic.was_not_called = True

        with pytest.raises(IPythonMagicExit, match=r'^0$') as exc:
            assert magic.__doc__ is None
        assert exc.value.code == 0

        # Also, due to the exception, the decorated function should not get
        # called
        assert magic.was_not_called

        std = capsys.readouterr()
        assert std.out == dedent("""
        usage: %magic [-h] [-f FLAG] value

        positional arguments:
          value

        optional arguments:
          -h, --help            show this help message and exit
          -f FLAG, --flag FLAG
        """).lstrip()
        assert std.err == ""
예제 #2
0
    def test_magic__help(self, capsys):
        """
        Test the ``--help`` output of a created cell ``%%magic``.

        And that a :exc:`moreshell.MagicExit` with code ``0`` is raised
        """
        @IPython_cell_magic(with_arguments('-f', '--flag'))
        def magic(shell, args, block):  # pragma: no cover
            magic.was_not_called = False

        magic.was_not_called = True

        with pytest.raises(IPythonMagicExit, match=r'^0$') as exc:
            magic('--help', block="")  # pylint: disable=no-value-for-parameter
        assert exc.value.code == 0

        # Also, due to the exception, the decorated function should not get
        # called
        assert magic.was_not_called

        std = capsys.readouterr()
        assert std.out == dedent("""
        usage: %%magic [-h] [-f FLAG]

        optional arguments:
          -h, --help            show this help message and exit
          -f FLAG, --flag FLAG
        """).lstrip()
        assert std.err == ""
예제 #3
0
    def test__call__with_invalid_kind_of_magic(self):
        """
        Test that an ``AssertionError`` is raised.

        When the internal ``kind_of_magic`` argument is used improperly
        """
        def magic(shell, args):  # pragma: no cover
            pass

        with pytest.raises(AssertionError,
                           match=r" 'line' or 'cell', not: 'invalid'$"):

            magic_deco = IPython_magic(with_arguments('-f', '--flag'))
            magic_deco(magic, kind_of_magic='invalid')
예제 #4
0
import sys

from path import Path
from zetup import call

import moreshell
from moreshell import IPython_magic_module, IPython_magic, with_arguments

IPython_magic_module(__name__, [
    'test_moreshell',
])


@IPython_magic(
    with_arguments('-c',
                   '--coverage', action='store_true')('-v',
                                                      '--verbose',
                                                      action='store_true'))
def test_moreshell(shell, args):  # pragma: no cover
    """Run the :mod:`moreshell` unit tests with ``pytest``."""
    moreshell_dir = Path(  # pylint: disable=no-value-for-parameter
        moreshell.__file__).dirname().realpath()

    call_args = [
        sys.executable, '-m', 'pytest', '--doctest-modules', moreshell_dir
    ]
    if args.coverage:
        call_args.extend(
            ['--cov', moreshell_dir, '--cov-report', 'term-missing'])
    if args.verbose:
        call_args.append('-vv')
예제 #5
0
import os
import sys
from inspect import getsourcefile, getsourcelines

import zetup
from moreshell import IPython_magic, IPython_magic_module, with_arguments
from moretools import getfunc
from path import Path
from six import reraise
from zetup import call

IPython_magic_module(__name__, ['dev'])


@IPython_magic(with_arguments('object')('-e', '--editor')('-f', '--format'))
def dev(shell, args):
    """
    Open the file containing the code for the given object.

    And jump to the location of its definition

    By taking the command from the ``--editor`` option or from the ``EDITOR``
    environment variable, and running it with an argument created from the
    format string ``{file}:{line}``, which can be customized using the
    ``--format`` option

    The subprocess is started with ``shell=True``

    If no line number can be determined for the given ``object``, then the
    format string is ignored, and only the file path is used