Esempio n. 1
0
def test_pass_stdin_to_subprocess(stdin):
    """
    Pass input to a normal subprocess.

    Arguments:
        stdin: Text sample generated by Hypothesis.
    """
    code, output = run_subprocess(["cat"], stdin=stdin)
    assert code == 0
    assert output == stdin
Esempio n. 2
0
def run_command(
    cmd: CmdType,
    capture: Capture = Capture.BOTH,
    ansi: bool = False,
    pty: bool = False,
    stdin: Optional[str] = None,
) -> Tuple[int, str]:
    """
    Run a command.

    Arguments:
        cmd: The command to run.
        capture: The output to capture.
        ansi: Whether to accept ANSI sequences.
        pty: Whether to run in a PTY.
        stdin: String to use as standard input.

    Returns:
        The exit code and the command output.
    """
    shell = isinstance(cmd, str)

    # if chosen format doesn't accept ansi, or on Windows, don't use pty
    if pty and (not ansi or WINDOWS):
        pty = False

    # pty can only combine, so only use pty when combining
    if pty and capture in {Capture.BOTH, Capture.NONE}:
        if shell:
            cmd = ["sh", "-c", cmd]  # type: ignore  # we know cmd is str
        return run_pty_subprocess(cmd, capture, stdin)  # type: ignore  # we made sure cmd is a list

    # we are on Windows
    if WINDOWS:
        # make sure the process can find the executable
        if not shell:
            cmd[0] = shutil.which(cmd[0]) or cmd[0]  # type: ignore  # we know cmd is a list
        return run_subprocess(cmd, capture, shell=shell, stdin=stdin)  # noqa: S604 (shell=True)

    return run_subprocess(cmd, capture, shell=shell, stdin=stdin)  # noqa: S604 (shell=True)
Esempio n. 3
0
def test_run_unknown_shell_command():
    """Run an unknown command in a shell."""
    code, output = run_subprocess("mlemlemlemlemle",
                                  shell=True)  # noqa: S604 (shell=True)
    assert code > 0
    assert output
Esempio n. 4
0
def test_run_unknown_command():
    """Run an unknown command without a shell."""
    # maybe this exception should be caught in the code?
    with pytest.raises(FileNotFoundError):
        run_subprocess("mlemlemlemlemle")
Esempio n. 5
0
def test_run_list_of_args_as_shell():
    """Test that a list of arguments is stringified."""
    code, output = run_subprocess(["python", "-V"],
                                  shell=True)  # noqa: S604 (shell=True)
    assert code == 0
    assert "Python" in output