Example #1
0
def run(context, command):
    args = ushlex(command)[1:]
    try:
        cli.run(args or None)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #2
0
def run(context, command):
    args = _parse_args(command)
    try:
        cli.run(args or None)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #3
0
def run_with_input(context, command, inputs=""):
    # create an iterator through all inputs. These inputs will be fed one by one
    # to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()'
    if context.text:
        text = iter(context.text.split("\n"))
    else:
        text = iter([inputs])

    args = ushlex(command)[1:]

    # fmt: off
    # see: https://github.com/psf/black/issues/557
    with patch("builtins.input", side_effect=_mock_input(text)) as mock_input, \
         patch("getpass.getpass", side_effect=_mock_getpass(text)) as mock_getpass, \
         patch("sys.stdin.read", side_effect=text) as mock_read:

        try:
            cli.run(args or [])
            context.exit_status = 0
        except SystemExit as e:
            context.exit_status = e.code

        # at least one of the mocked input methods got called
        assert mock_input.called or mock_getpass.called or mock_read.called
        # all inputs were used
        try:
            next(text)
            assert False, "Not all inputs were consumed"
        except StopIteration:
            pass
Example #4
0
def run(context, command):
    args = _parse_args(command)
    try:
        cli.run(args)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #5
0
def open_editor_and_enter(context, method, text=""):
    text = text or context.text or ""

    if method == "enter":
        file_method = "w+"
    elif method == "append":
        file_method = "a"
    else:
        file_method = "r+"

    def _mock_editor_function(command):
        context.editor_command = command
        tmpfile = command[-1]
        with open(tmpfile, file_method) as f:
            f.write(text)

        return tmpfile

    # fmt: off
    # see: https://github.com/psf/black/issues/664
    with \
        patch("subprocess.call", side_effect=_mock_editor_function), \
        patch("sys.stdin.isatty", return_value=True) \
    :
        cli.run(["--edit"])
Example #6
0
def run_with_input(context, command, inputs=None):
    text = inputs or context.text
    args = _parse_args(command)
    buffer = StringIO(text.strip())
    util.STDIN = buffer
    try:
        cli.run(args or None)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #7
0
def run_with_input(context, command, inputs=None):
    text = inputs or context.text
    args = _parse_args(command)
    buffer = StringIO(text.strip())
    util.STDIN = buffer
    try:
        cli.run(args)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #8
0
File: core.py Project: zuchka/jrnl
def run(context, command, cache_dir=None):
    if cache_dir is not None:
        cache_dir = os.path.join("features", "cache", cache_dir)
        command = command.format(cache_dir=cache_dir)

    args = ushlex(command)[1:]
    try:
        cli.run(args or None)
        context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code
Example #9
0
def run_with_input(context, command, inputs1="", inputs2=""):
    # create an iterator through all inputs. These inputs will be fed one by one
    # to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()'
    text = iter((inputs1, inputs2)) if inputs1 else iter(context.text.split("\n"))
    args = ushlex(command)[1:]
    with patch("builtins.input", side_effect=_mock_input(text)) as mock_input:
        with patch("jrnl.util.getpass", side_effect=_mock_getpass(text)) as mock_getpass:
            with patch("sys.stdin.read", side_effect=text) as mock_read:
                try:
                    cli.run(args or [])
                    context.exit_status = 0
                except SystemExit as e:
                    context.exit_status = e.code

                # assert at least one of the mocked input methods got called
                assert mock_input.called or mock_getpass.called or mock_read.called
Example #10
0
def run(context, command, text="", cache_dir=None):
    text = text or context.text or ""

    if cache_dir is not None:
        cache_dir = os.path.join("features", "cache", cache_dir)
        command = command.format(cache_dir=cache_dir)

    args = ushlex(command)

    def _mock_editor(command):
        context.editor_command = command

    try:
        with patch("sys.argv", args), patch("subprocess.call",
                                            side_effect=_mock_editor), patch(
                                                "sys.stdin.read",
                                                side_effect=lambda: text):
            cli.run(args[1:])
            context.exit_status = 0
    except SystemExit as e:
        context.exit_status = e.code