Beispiel #1
0
def test_output_iteration(test):
    """Tests iteration over process' output."""

    with sh.cat(_stdin="") as process:
        stdout = [line for line in process]
    assert stdout == []

    with sh.cat(_stdin="aaa\nтест\nbbb") as process:
        stdout = [line for line in process]

    assert stdout == ["aaa\n", "тест\n", "bbb"]

    for line in stdout:
        assert type(line) == str
def test_output_iteration(test):
    """Tests iteration over process' output."""

    with sh.cat(_stdin="") as process:
        stdout = [line for line in process]
    assert stdout == []


    with sh.cat(_stdin="aaa\nтест\nbbb") as process:
        stdout = [line for line in process]

    assert stdout == ["aaa\n", "тест\n", "bbb"]

    for line in stdout:
        assert type(line) == str
Beispiel #3
0
def test_piping_errors(test):
    """Tests invalid process piping."""

    process = sh.cat()
    with pytest.raises(psh.InvalidOperation):
        process | "string"

    process = sh.cat()
    process | sh.grep()
    with pytest.raises(psh.InvalidOperation):
        process | sh.grep()

    process = sh.cat()
    process | sh.grep()
    with pytest.raises(psh.InvalidOperation):
        process.execute()
def test_output_iteration_option_delimiter(test):
    """Tests iteration over process' output with custom delimiter."""

    with sh.cat(_stdin="aa\ta\nте\tст\nbbb", _iter_delimiter="\t") as process:
        stdout = [line for line in process]

    assert stdout == ["aa\t", "a\nте\t", "ст\nbbb"]
Beispiel #5
0
def test_output_iteration_option_delimiter(test):
    """Tests iteration over process' output with custom delimiter."""

    with sh.cat(_stdin="aa\ta\nте\tст\nbbb", _iter_delimiter="\t") as process:
        stdout = [line for line in process]

    assert stdout == ["aa\t", "a\nте\t", "ст\nbbb"]
Beispiel #6
0
def test_piping_errors(test):
    """Tests invalid process piping."""

    process = sh.cat()
    with pytest.raises(psh.InvalidOperation):
        process | "string"

    process = sh.cat()
    process | sh.grep()
    with pytest.raises(psh.InvalidOperation):
        process | sh.grep()

    process = sh.cat()
    process | sh.grep()
    with pytest.raises(psh.InvalidOperation):
        process.execute()
def test_output_iteration_without_delimiter_raw(test):
    """Tests iteration over process' output without delimiter (raw)."""

    with open("/dev/urandom", "rb") as random:
        stdin = random.read(1024 * 1024)

    with sh.cat(_stdin=stdin, _iter_delimiter="", _iter_raw=True) as process:
        assert stdin == b"".join(block for block in process)
Beispiel #8
0
def test_output_iteration_without_delimiter_raw(test):
    """Tests iteration over process' output without delimiter (raw)."""

    with open("/dev/urandom", "rb") as random:
        stdin = random.read(1024 * 1024)

    with sh.cat(_stdin=stdin, _iter_delimiter="", _iter_raw=True) as process:
        assert stdin == b"".join(block for block in process)
Beispiel #9
0
def test_output_iterator_misusing(test):
    """Tests iteration outside 'with' statement."""

    with sh.cat(_stdin="aaa\nbbb\nccc") as process:
        output = iter(process)
        next(output)

    with pytest.raises(psh.InvalidOperation):
        next(output)
def test_output_iterator_misusing(test):
    """Tests iteration outside 'with' statement."""

    with sh.cat(_stdin="aaa\nbbb\nccc") as process:
        output = iter(process)
        next(output)

    with pytest.raises(psh.InvalidOperation):
        next(output)
Beispiel #11
0
def test_iterator_input(test):
    """Tests process input from string."""

    stdout = "\n".join(str(i) for i in range(0, 10))

    def func():
        for i in range(0, 10):
            yield "\n" + str(i) if i else str(i)

    assert sh.cat(_stdin=func()).execute().stdout() == stdout
def test_output_iteration_with_raw_true(test):
    """Tests iteration over process' output with _iter_raw = True."""

    with sh.cat(_stdin="aaa\nтест\nbbb", _iter_raw=True) as process:
        stdout = [line for line in process]

    assert stdout == [b"aaa\n", psys.b("тест\n"), b"bbb"]

    for line in stdout:
        assert type(line) == bytes
def test_output_iteration_with_raw_false(test):
    """Tests iteration over process' output with _iter_raw = False."""

    with sh.cat(_stdin="aaa\nтест\nbbb", _iter_raw=False) as process:
        stdout = [line for line in process]

    assert stdout == ["aaa\n", "тест\n", "bbb"]

    for line in stdout:
        assert type(line) == str
Beispiel #14
0
def test_output_iteration_with_raw_false(test):
    """Tests iteration over process' output with _iter_raw = False."""

    with sh.cat(_stdin="aaa\nтест\nbbb", _iter_raw=False) as process:
        stdout = [line for line in process]

    assert stdout == ["aaa\n", "тест\n", "bbb"]

    for line in stdout:
        assert type(line) == str
Beispiel #15
0
def test_iterator_input(test):
    """Tests process input from string."""

    stdout = "\n".join(str(i) for i in range(0, 10))

    def func():
        for i in range(0, 10):
            yield "\n" + str(i) if i else str(i)

    assert sh.cat(_stdin=func()).execute().stdout() == stdout
Beispiel #16
0
def test_output_iteration_with_raw_true(test):
    """Tests iteration over process' output with _iter_raw = True."""

    with sh.cat(_stdin="aaa\nтест\nbbb", _iter_raw=True) as process:
        stdout = [line for line in process]

    assert stdout == [b"aaa\n", psys.b("тест\n"), b"bbb"]

    for line in stdout:
        assert type(line) == bytes
def test_output_iteration_with_large_data(test):
    """
    Tests iteration over process' output with large amount of data
    (more than any buffer size).
    """

    stdin = [str(i) + "\n" for i in range(0, 100000)]

    with sh.cat(_stdin="".join(stdin)) as process:
        stdout = [line for line in process]

    assert stdout == stdin
Beispiel #18
0
def test_output_iteration_with_large_data(test):
    """
    Tests iteration over process' output with large amount of data
    (more than any buffer size).
    """

    stdin = [str(i) + "\n" for i in range(0, 100000)]

    with sh.cat(_stdin="".join(stdin)) as process:
        stdout = [line for line in process]

    assert stdout == stdin
Beispiel #19
0
def test_file_object_input(test):
    """Tests process input from file object."""

    with tempfile.NamedTemporaryFile() as temp_file:
        with open("/dev/urandom", "rb") as random:
            stdout = random.read(1024 * 1024)

        temp_file.write(stdout)
        temp_file.flush()

        with open(temp_file.name, "rb") as stdin:
            assert sh.cat(_stdin=stdin).execute().raw_stdout() == stdout
Beispiel #20
0
def test_file_object_input(test):
    """Tests process input from file object."""

    with tempfile.NamedTemporaryFile() as temp_file:
        with open("/dev/urandom", "rb") as random:
            stdout = random.read(1024 * 1024)

        temp_file.write(stdout)
        temp_file.flush()

        with open(temp_file.name, "rb") as stdin:
            assert sh.cat(_stdin=stdin).execute().raw_stdout() == stdout
Beispiel #21
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
Beispiel #22
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
Beispiel #23
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() == ""
Beispiel #24
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() == ""
Beispiel #25
0
def test_abandoned_pipe(test):
    """Tests that an abandoned pipe doesn't lead to resource leaks."""

    sh.cat("/etc/fstab") | sh.grep("/dev") | sh.wc("-l")
Beispiel #26
0
def test_pipes(test):
    """Tests process pipes."""

    process = sh.cat(_stdin="aaaa\nbbbb\n" * 1024 * 100) | sh.grep("aaaa") | sh.wc("-l")
    assert process.execute().stdout().strip() == "102400"
Beispiel #27
0
def test_abandoned_pipe(test):
    """Tests that an abandoned pipe doesn't lead to resource leaks."""

    sh.cat("/etc/fstab") | sh.grep("/dev") | sh.wc("-l")
Beispiel #28
0
def test_pipes(test):
    """Tests process pipes."""

    process = sh.cat(_stdin="aaaa\nbbbb\n" * 1024 *
                     100) | sh.grep("aaaa") | sh.wc("-l")
    assert process.execute().stdout().strip() == "102400"