Example #1
0
def test_cancel_pty():
    """Test that using PTY is canceled if the format does not support it."""
    with patch("failprint.runners.run_subprocess", new=MagicMock(return_value=(0, ""))) as run_sub:
        with patch("failprint.runners.run_pty_subprocess", new=MagicMock(return_value=(0, ""))) as run_pty_sub:
            run("true", pty=True, fmt="tap")
            assert not run_pty_sub.called
            assert run_sub.called
Example #2
0
def test_run_failing_silent_command_verbosely(capsys):
    """
    Run a failing silent command, verbosely.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    run(["false"])
    outerr = capsys.readouterr()
    assert "false" in outerr.out
    assert not outerr.err
Example #3
0
def test_run_silent_command_silently(capsys):
    """
    Run a silent command, silently.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    run(["true"], silent=True)
    outerr = capsys.readouterr()
    assert not outerr.out
    assert not outerr.err  # noqa: WPS204 (overuse)
Example #4
0
def test_run_verbose_command_silently(capsys):
    """
    Run a verbose command, silently.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    run("echo VERBS", silent=True)
    outerr = capsys.readouterr()
    assert not outerr.out
    assert not outerr.err
Example #5
0
def test_tap_format(capsys):
    """
    Check the tap output format.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    run(["true"], fmt="tap")
    outerr = capsys.readouterr()
    assert "ok" in outerr.out
    run(["false"], fmt="tap")
    outerr = capsys.readouterr()
    assert "not ok" in outerr.out
Example #6
0
def test_callable_exit_codes(code):
    """
    Check the return codes of Python callables.

    Arguments:
        code: Hypothesis fixture to provide various integers.
    """
    assert run(lambda: code).code == code
Example #7
0
def test_fails_with_falsy_object():
    """Check the return code when a callable returns a falsy object."""

    class Meh:  # noqa: C0115,WPS431 (missing docstring, nested class)
        def __bool__(self):
            return False

    assert run(Meh).code == 1
Example #8
0
def test_callable_capture_stderr(capsys):
    """
    Check that stderr is captured while running a callable.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    msg_stdout = "out"
    msg_stderr = "err"
    run(
        lambda: sys.stdout.write(msg_stdout) and sys.stderr.write(msg_stderr),
        capture="stderr",
        fmt="custom={{output}}",
    )
    outerr = capsys.readouterr()
    assert msg_stdout not in outerr.out
    assert msg_stderr in outerr.out
    assert not outerr.err
Example #9
0
def test_process_capture_stderr(capsys):
    """
    Check that stderr is captured while running a process.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    msg_stdout = "out"
    msg_stderr = "err"
    run(
        ["bash", "-c", f"echo {msg_stdout}; echo {msg_stderr} >&2"],
        capture="stderr",
        fmt="custom={{output}}",
    )
    outerr = capsys.readouterr()
    assert msg_stdout not in outerr.out
    assert msg_stderr in outerr.out
    assert not outerr.err
Example #10
0
def test_process_capture_none(capfd):
    """
    Check that nothing is captured while running a process.

    Arguments:
        capfd: Pytest fixture to capture output.
    """
    assert run([sys.executable, "-V"], capture=False, silent=True).code == 0
    outerr = capfd.readouterr()
    assert "Python" in outerr.out
Example #11
0
def test_run_callable_raising_exception(capsys):
    """
    Test running a callable raising an exception.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    assert run(lambda: 1 / 0).code == 1  # noqa: WPS344 (zero division)
    outerr = capsys.readouterr()
    assert "ZeroDivisionError:" in outerr.out
Example #12
0
def test_callable_capture_none(capsys):
    """
    Check that nothing is captured while running a callable.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    msg = "out"
    assert run(lambda: print(msg), capture=False, silent=True).code == 0  # noqa: WPS421 (print)
    outerr = capsys.readouterr()
    assert msg in outerr.out
Example #13
0
def test_run_verbose_command_verbosely(capsys):
    """
    Run a verbose command, verbosely.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    assert run("echo VERBS").code == 0
    outerr = capsys.readouterr()
    assert "VERBS" in outerr.out
    assert not outerr.err
Example #14
0
def run_linux_shell_command(capsys):
    """
    Run a Linux shell command.

    Arguments:
        capsys: Pytest fixture to capture output.
    """
    assert run("echo herbert | grep -o er", capture=False, silent=True).code == 0
    outerr = capsys.readouterr()
    assert outerr.out.split("\n") == ["er", "er"]
    assert not outerr.err
Example #15
0
def main(args: Optional[List[str]] = None) -> int:
    """
    Run the main program.

    This function is executed when you type `failprint` or `python -m failprint`.

    Arguments:
        args: Arguments passed from the command line.

    Returns:
        An exit code.
    """
    parser = get_parser()
    opts = parser.parse_args(args).__dict__.items()  # noqa: WPS609
    return run(**{_: value for _, value in opts if value is not None}).code
Example #16
0
def test_run_linux_program():
    """Run a GNU/Linux program."""
    marker = "THIS VERY LINE"
    assert run(["grep", "-q", marker, __file__]).code == 0
    assert run(["grep", "-q", r"NOT\s*THIS\s*LINE", __file__]).code == 1
Example #17
0
def test_succeed_with_truthy_object():
    """Check the return code when a callable returns a truthy object."""
    assert run(object).code == 0
Example #18
0
def test_succeed_with_none_result():
    """Check the return code when a callable returns `None`."""
    assert run(lambda: None).code == 0
Example #19
0
def return_shell_custom_code():
    """Check the return code of a shell exit."""
    assert run("exit 15").code == 15  # noqa: WPS432 (magic number)
Example #20
0
def return_failure_code():
    """Check the return code of a failing command."""
    assert run(["false"]).code == 1
Example #21
0
def return_success_code():
    """Check the return code of a successful command."""
    assert run(["true"]).code == 0
Example #22
0
def test_run_callable_return_boolean():
    """Check the return code when a callable returns a boolean."""
    assert run(lambda: True).code == 0
    assert run(lambda: False).code == 1  # noqa: WPS522 (implicit primitive/lambda)
Example #23
0
def test_run_pty_shell():
    """Test running a shell command in a PTY."""
    with patch("failprint.runners.run_pty_subprocess", new=MagicMock(return_value=(0, ""))) as run_pty_sub:
        run("true", pty=True)
        assert run_pty_sub.called