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"
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()
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_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
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_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
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
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() == ""
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() == ""
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"
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")
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"