Esempio n. 1
0
def test_string_input(test):
    """Tests process input from string."""

    assert sh.grep("тест",
                   _stdin="aaa\nтест\nbbb\n").execute().stdout() == "тест\n"
    assert sh.grep(
        "тест",
        _stdin=psys.b("aaa\nтест\nbbb\n")).execute().stdout() == "тест\n"
Esempio n. 2
0
def test_invalid_input(test):
    """Tests invalid input."""

    with pytest.raises(psh.InvalidArgument):
        sh.grep(_stdin=3)

    with pytest.raises(psh.InvalidArgument):
        sh.grep("1", _stdin=iter([1])).execute()
Esempio n. 3
0
def test_invalid_input(test):
    """Tests invalid input."""

    with pytest.raises(psh.InvalidArgument):
        sh.grep(_stdin=3)

    with pytest.raises(psh.InvalidArgument):
        sh.grep("1", _stdin=iter([1])).execute()
Esempio n. 4
0
def test_pipe_errors(test):
    """Tests errors in the middle of the pipe."""

    process = sh.echo("aaa") | sh.grep("bbb") | sh.wc("-l")
    assert pytest.raises(psh.ExecutionError,
        lambda: process.execute()).value.status() == 1

    process = sh.echo("aaa") | sh.grep("bbb", _ok_statuses=(0, 1)) | sh.wc("-l")
    process.execute().stdout().strip() == "0"
Esempio n. 5
0
def test_pipe_errors(test):
    """Tests errors in the middle of the pipe."""

    process = sh.echo("aaa") | sh.grep("bbb") | sh.wc("-l")
    assert pytest.raises(psh.ExecutionError,
                         lambda: process.execute()).value.status() == 1

    process = sh.echo("aaa") | sh.grep("bbb",
                                       _ok_statuses=(0, 1)) | sh.wc("-l")
    process.execute().stdout().strip() == "0"
Esempio n. 6
0
def test_output_iteration_error(test):
    """Tests iteration over process which returns an error."""

    with sh.grep("aaa", _stdin="bbb") as process:
        with pytest.raises(psh.ExecutionError):
            for line in process:
                pass
Esempio n. 7
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_error(test):
    """Tests iteration over process which returns an error."""

    with sh.grep("aaa", _stdin="bbb") as process:
        with pytest.raises(psh.ExecutionError):
            for line in process:
                pass
Esempio n. 9
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()
Esempio n. 10
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
Esempio n. 11
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
Esempio n. 12
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() == ""
Esempio n. 13
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() == ""
Esempio n. 14
0
def test_string_input(test):
    """Tests process input from string."""

    assert sh.grep("тест", _stdin="aaa\nтест\nbbb\n").execute().stdout() == "тест\n"
    assert sh.grep("тест", _stdin=psys.b("aaa\nтест\nbbb\n")).execute().stdout() == "тест\n"
Esempio n. 15
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")
Esempio n. 16
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"
Esempio n. 17
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")
Esempio n. 18
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"