Exemple #1
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"
Exemple #2
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"
def test_command_with_redirection(test):
    """Tests command arguments representation."""

    process = sh.echo("test", _stdout=STDERR)
    assert str(process) == "echo test >&2"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=File("/tmp/test.path"))
    assert str(process) == "echo test > /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=File("/tmp/test.path", append=True))
    assert str(process) == "echo test >> /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stderr=File("/tmp/test.path"))
    assert str(process) == "echo test 2> /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stderr=File("/tmp/test.path", append=True))
    assert str(process) == "echo test 2>> /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stderr=STDOUT)
    assert str(process) == "echo test 2>&1"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=DEVNULL, _stderr=DEVNULL)
    assert str(process) == "echo test > /dev/null 2> /dev/null"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=File("/\rtmp\t/test.\rpath"))
    assert str(process) == "echo test > '/\\rtmp\\t/test.\\rpath'"
    assert bytes(process) == psys.b(str(process))
def test_output_iteration_without_delimiter_unicode(test):
    """Tests iteration over process' output without delimiter (unicode)."""

    with pytest.raises(psh.InvalidOperation):
        with sh.echo(_iter_delimiter="") as process:
            for block in process:
                pass
Exemple #5
0
def test_output_iteration_without_delimiter_unicode(test):
    """Tests iteration over process' output without delimiter (unicode)."""

    with pytest.raises(psh.InvalidOperation):
        with sh.echo(_iter_delimiter="") as process:
            for block in process:
                pass
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() == ""
Exemple #7
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() == ""
Exemple #8
0
def test_command_with_redirection(test):
    """Tests command arguments representation."""

    process = sh.echo("test", _stdout=STDERR)
    assert str(process) == "echo test >&2"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=File("/tmp/test.path"))
    assert str(process) == "echo test > /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stderr=File("/tmp/test.path"))
    assert str(process) == "echo test 2> /tmp/test.path"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stderr=STDOUT)
    assert str(process) == "echo test 2>&1"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=DEVNULL, _stderr=DEVNULL)
    assert str(process) == "echo test > /dev/null 2> /dev/null"
    assert bytes(process) == psys.b(str(process))

    process = sh.echo("test", _stdout=File("/\rtmp\t/test.\rpath"))
    assert str(process) == "echo test > '/\\rtmp\\t/test.\\rpath'"
    assert bytes(process) == psys.b(str(process))
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() == ""
Exemple #10
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() == ""