예제 #1
0
def test_stdout_append_to_file(test, capfd):
    """Tests redirection of stdout to a file with appending."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            temp_file.write(b"orig\n")
            temp_file.flush()

            process = sh.sh("-c", sh.sh("-c",
                "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                _stdout=File(temp_file.name, append=True)), _shell=True)
            process.execute()

            assert process.stdout() == ""
            assert process.stderr() == "test2\nтест4\n"

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            with open(temp_file.name, "rb") as stdout:
                assert psys.u(stdout.read()) == "orig\ntest1\nтест3\n"
    finally:
        logging.disable(logging.NOTSET)
예제 #2
0
def test_stdout_append_to_file(test, capfd):
    """Tests redirection of stdout to a file with appending."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            temp_file.write(b"orig\n")
            temp_file.flush()

            process = sh.sh(
                "-c",
                sh.sh(
                    "-c",
                    "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                    _stdout=File(temp_file.name, append=True)),
                _shell=True)
            process.execute()

            assert process.stdout() == ""
            assert process.stderr() == "test2\nтест4\n"

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            with open(temp_file.name, "rb") as stdout:
                assert psys.u(stdout.read()) == "orig\ntest1\nтест3\n"
    finally:
        logging.disable(logging.NOTSET)
예제 #3
0
def test_output(test):
    """Tests process output handling."""

    valid_stdout = "тест1\nтест3\n"
    valid_stderr = "тест2\nтест4\n"

    command = "echo тест1; echo тест2 >&2; sleep 1; echo тест3; echo тест4 >&2; "

    process = sh.sh("-c", command).execute()
    _check_output(process, valid_stdout, valid_stderr)

    error = pytest.raises(psh.ExecutionError,
        lambda: sh.sh("-c", command + " exit 1").execute()).value
    _check_output(error, valid_stdout, valid_stderr)
예제 #4
0
def test_output(test):
    """Tests process output handling."""

    valid_stdout = "тест1\nтест3\n"
    valid_stderr = "тест2\nтест4\n"

    command = "echo тест1; echo тест2 >&2; sleep 1; echo тест3; echo тест4 >&2; "

    process = sh.sh("-c", command).execute()
    _check_output(process, valid_stdout, valid_stderr)

    error = pytest.raises(
        psh.ExecutionError,
        lambda: sh.sh("-c", command + " exit 1").execute()).value
    _check_output(error, valid_stdout, valid_stderr)
예제 #5
0
def test_error_codes(test):
    """Tests error codes."""

    sh.sh("-c", sh.true(), _shell=True).execute().status() == 0

    assert pytest.raises(psh.ExecutionError,
        lambda: sh.sh("-c", sh.false(), _shell=True).execute()).value.status() == 1


    pipe = sh.cat() | sh.egrep("bbb|ccc") | sh.grep("ccc")

    assert pytest.raises(psh.ExecutionError,
        lambda: sh.sh("-c", pipe, _stdin="aaa\n", _shell=True).execute()).value.status() == 128

    assert pytest.raises(psh.ExecutionError,
        lambda: sh.sh("-c", pipe, _stdin="bbb\n", _shell=True).execute()).value.status() == 1
예제 #6
0
def test_large_output(test):
    """Tests large amount of output (more than any pipe buffer size)."""

    stdout_tempfile = None
    stderr_tempfile = None

    with open("/dev/urandom", "rb") as random:
        stdout = random.read(1024 * 1024)
        stderr = random.read(1024 * 1024 + 1)

    try:
        stdout_tempfile = tempfile.NamedTemporaryFile()
        stdout_tempfile.write(stdout)
        stdout_tempfile.flush()

        stderr_tempfile = tempfile.NamedTemporaryFile()
        stderr_tempfile.write(stderr)
        stderr_tempfile.flush()

        process = sh.sh("-c", "cat {stdout} & pid=$!; cat {stderr} >&2; wait $pid;".format(
            stdout=stdout_tempfile.name, stderr=stderr_tempfile.name)).execute()

        assert process.raw_stdout() == stdout
        assert process.raw_stderr() == stderr
    finally:
        if stdout_tempfile is not None:
            stdout_tempfile.close()

        if stderr_tempfile is not None:
            stderr_tempfile.close()
예제 #7
0
def test_large_output(test):
    """Tests large amount of output (more than any pipe buffer size)."""

    stdout_tempfile = None
    stderr_tempfile = None

    with open("/dev/urandom", "rb") as random:
        stdout = random.read(1024 * 1024)
        stderr = random.read(1024 * 1024 + 1)

    try:
        stdout_tempfile = tempfile.NamedTemporaryFile()
        stdout_tempfile.write(stdout)
        stdout_tempfile.flush()

        stderr_tempfile = tempfile.NamedTemporaryFile()
        stderr_tempfile.write(stderr)
        stderr_tempfile.flush()

        process = sh.sh(
            "-c", "cat {stdout} & pid=$!; cat {stderr} >&2; wait $pid;".format(
                stdout=stdout_tempfile.name,
                stderr=stderr_tempfile.name)).execute()

        assert process.raw_stdout() == stdout
        assert process.raw_stderr() == stderr
    finally:
        if stdout_tempfile is not None:
            stdout_tempfile.close()

        if stderr_tempfile is not None:
            stderr_tempfile.close()
예제 #8
0
def test_pid(test):
    """Tests pid()."""

    process = sh.sh("-c", "echo $$")

    with pytest.raises(psh.InvalidProcessState):
        process.pid()

    assert process.execute().stdout().strip() == str(process.pid())
예제 #9
0
def test_pid(test):
    """Tests pid()."""

    process = sh.sh("-c", "echo $$")

    with pytest.raises(psh.InvalidProcessState):
        process.pid()

    assert process.execute().stdout().strip() == unicode(process.pid())
예제 #10
0
def test_argument_passing(test):
    """Tests passing data via command line arguments."""

    value = ""
    for i in range(1, 256):
        value += chr(i)

    process = sh.sh("-c", sh.echo(value), _shell=True).execute()
    assert process.stdout() == value + "\n"
    assert process.stderr() == ""
예제 #11
0
def test_argument_passing(test):
    """Tests passing data via command line arguments."""

    value = ""
    for i in range(1, 256):
        value += chr(i)

    process = sh.sh("-c", sh.echo(value), _shell=True).execute()
    assert process.stdout() == value + "\n"
    assert process.stderr() == ""
예제 #12
0
def test_output_after_process_termination(test):
    """Tests execution of a process that terminates before its children."""

    command = ["-c", "echo aaa; ( sleep 1; echo bbb; )&"]

    process = sh.sh(*command).execute()
    assert process.status() == 0
    assert process.stdout() == "aaa\nbbb\n"
    assert process.stderr() == ""

    error = pytest.raises(psh.ProcessOutputWasTruncated,
        lambda: sh.sh(*command, _wait_for_output=False).execute()).value
    assert error.status() == 0
    assert error.stdout() == "aaa\n"
    assert error.stderr() == ""

    process = sh.sh(*command, _wait_for_output=False, _truncate_output=True).execute()
    assert process.status() == 0
    assert process.stdout() == "aaa\n"
    assert process.stderr() == ""
예제 #13
0
def test_error_codes(test):
    """Tests error codes."""

    sh.sh("-c", sh.true(), _shell=True).execute().status() == 0

    assert pytest.raises(
        psh.ExecutionError, lambda: sh.sh("-c", sh.false(), _shell=True).
        execute()).value.status() == 1

    pipe = sh.cat() | sh.egrep("bbb|ccc") | sh.grep("ccc")

    assert pytest.raises(
        psh.ExecutionError,
        lambda: sh.sh("-c", pipe, _stdin="aaa\n", _shell=True).execute(
        )).value.status() == 128

    assert pytest.raises(
        psh.ExecutionError,
        lambda: sh.sh("-c", pipe, _stdin="bbb\n", _shell=True).execute(
        )).value.status() == 1
예제 #14
0
def test_execution(test):
    """Tests command execution in the shell mode."""

    process = sh.sh("-c",
        sh.echo("aaa", _stdout=STDERR), _shell=True).execute()
    assert process.stdout() == ""
    assert process.stderr() == "aaa\n"

    process = sh.sh(
        c=sh.sh("-c", "echo aaa >&2", _stderr=STDOUT), _shell=True).execute()
    assert process.stdout() == "aaa\n"
    assert process.stderr() == ""

    process = sh.sh("-c",
        sh.echo("aaa", _stdout=DEVNULL), _shell=True).execute()
    assert process.stdout() == ""
    assert process.stderr() == ""

    pipe = sh.cat() | sh.egrep("bbb|ccc") | sh.grep("ccc")
    process = sh.sh("-c", pipe, _stdin="aaa\nbbb\nccc\n", _shell=True).execute()
    assert process.stdout() == "ccc\n"
    assert process.stderr() == ""
예제 #15
0
def test_output_after_process_termination(test):
    """Tests execution of a process that terminates before its children."""

    command = ["-c", "echo aaa; ( sleep 1; echo bbb; )&"]

    process = sh.sh(*command).execute()
    assert process.status() == 0
    assert process.stdout() == "aaa\nbbb\n"
    assert process.stderr() == ""

    error = pytest.raises(
        psh.ProcessOutputWasTruncated,
        lambda: sh.sh(*command, _wait_for_output=False).execute()).value
    assert error.status() == 0
    assert error.stdout() == "aaa\n"
    assert error.stderr() == ""

    process = sh.sh(*command, _wait_for_output=False,
                    _truncate_output=True).execute()
    assert process.status() == 0
    assert process.stdout() == "aaa\n"
    assert process.stderr() == ""
예제 #16
0
def test_execution(test):
    """Tests command execution in the shell mode."""

    process = sh.sh("-c", sh.echo("aaa", _stdout=STDERR),
                    _shell=True).execute()
    assert process.stdout() == ""
    assert process.stderr() == "aaa\n"

    process = sh.sh(c=sh.sh("-c", "echo aaa >&2", _stderr=STDOUT),
                    _shell=True).execute()
    assert process.stdout() == "aaa\n"
    assert process.stderr() == ""

    process = sh.sh("-c", sh.echo("aaa", _stdout=DEVNULL),
                    _shell=True).execute()
    assert process.stdout() == ""
    assert process.stderr() == ""

    pipe = sh.cat() | sh.egrep("bbb|ccc") | sh.grep("ccc")
    process = sh.sh("-c", pipe, _stdin="aaa\nbbb\nccc\n",
                    _shell=True).execute()
    assert process.stdout() == "ccc\n"
    assert process.stderr() == ""
예제 #17
0
def test_stderr(test, capfd):
    """Tests output to stderr."""

    logging.disable(logging.CRITICAL)

    try:
        process = sh.sh("-c", "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;", _stderr=STDERR)
        process.execute()

        assert process.stdout() == "test1\nтест3\n"
        assert process.stderr() == ""

        stdout, stderr = capfd.readouterr()
        assert stdout == ""
        assert stderr == "test2\nтест4\n"
    finally:
        logging.disable(logging.NOTSET)
예제 #18
0
def test_stderr_to_stdout_redirection(test, capfd):
    """Tests redirection of stderr to stdout."""

    logging.disable(logging.CRITICAL)

    try:
        process = sh.sh("-c", "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;", _stderr=STDOUT)
        process.execute()

        assert process.stdout() == "test1\ntest2\nтест3\nтест4\n"
        assert process.stderr() == ""

        stdout, stderr = capfd.readouterr()
        assert stdout == ""
        assert stderr == ""
    finally:
        logging.disable(logging.NOTSET)
예제 #19
0
def test_stdout(test, capfd):
    """Tests output to stdout."""

    logging.disable(logging.CRITICAL)

    try:
        process = sh.sh(
            "-c",
            "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
            _stdout=STDOUT)
        process.execute()

        assert process.stdout() == ""
        assert process.stderr() == "test2\nтест4\n"

        stdout, stderr = capfd.readouterr()
        assert stdout == "test1\nтест3\n"
        assert stderr == ""
    finally:
        logging.disable(logging.NOTSET)
예제 #20
0
def test_stdout_to_dev_null_and_stderr_to_file(test, capfd):
    """Tests redirection of stdout to /dev/null and stderr to a file."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            process = sh.sh("-c", "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                _stdout=DEVNULL, _stderr=File(temp_file.name))
            process.execute()

            assert process.stdout() == ""
            assert process.stderr() == ""

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            assert temp_file.read() == psys.b("test2\nтест4\n")
    finally:
        logging.disable(logging.NOTSET)
예제 #21
0
def test_stdout_to_dev_null_and_stderr_to_file(test, capfd):
    """Tests redirection of stdout to /dev/null and stderr to a file."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            process = sh.sh(
                "-c",
                "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                _stdout=DEVNULL,
                _stderr=File(temp_file.name))
            process.execute()

            assert process.stdout() == ""
            assert process.stderr() == ""

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            assert temp_file.read() == psys.b("test2\nтест4\n")
    finally:
        logging.disable(logging.NOTSET)