示例#1
0
def test_kill():
    p = subby.Processes([["echo", "hi"]])
    p.run()
    p.block()
    assert not p.kill()
    assert p.returncode == 0

    p = subby.Processes([["sleep", "5"]])
    p.run()
    assert p.kill()
    assert p.returncode != 0

    with subby.Processes([["sleep", "5"]]) as p:
        pass
    assert p.closed
    assert p.returncode != 0
示例#2
0
def test_rc():
    with pytest.raises(RuntimeError):
        subby.Processes([]).returncode

    p = subby.Processes([["echo", "hi"], ["cat", "foo"]])
    p.run()
    while p.returncode is None:
        time.sleep(1)
    assert p.returncode == 1

    p = subby.Processes([["cat", "foo"], ["echo", "hi"]])
    p.run()
    while p.returncode is None:
        time.sleep(1)
    assert p.returncode == 1

    p = subby.Processes([["echo", "hi"], ["cat", "foo"]])
    p.run()
    try:
        p.block()
        raise AssertionError("Expected a CalledProcessError")
    except subprocess.CalledProcessError as err:
        assert err.returncode == 1
示例#3
0
def test_stderr_stdout(mode, expected_stdout, expected_stderr):
    p = subby.Processes([["echo", "hi"]],
                        stdout=subby.StdType.BUFFER,
                        stderr=subby.StdType.BUFFER,
                        mode=mode)
    with pytest.raises(RuntimeError):
        p.stdin_type
    with pytest.raises(RuntimeError):
        p.stdout_type
    with pytest.raises(RuntimeError):
        p.stderr_type
    p.run(echo=True)
    p.block(close=False)
    assert p._stdout_type is subby.StdType.BUFFER
    assert p._stderr_type is subby.StdType.BUFFER
    assert p.stdout_stream is not None
    assert p.stderr_stream is not None
    with pytest.raises(RuntimeError):
        p.output
    with pytest.raises(RuntimeError):
        p.error
    p.close()
    assert p.output == expected_stdout
    assert p.error == expected_stderr

    p = subby.Processes([["echo", "hi"]],
                        stdout=subby.StdType.BUFFER,
                        stderr=subby.StdType.BUFFER,
                        mode=mode)
    p.run(echo=True)
    p.block()
    assert p._stdout_type is subby.StdType.BUFFER
    assert p._stderr_type is subby.StdType.BUFFER
    assert p.stdout_stream is not None
    assert p.stderr_stream is not None
    assert p.output == expected_stdout
    assert p.error == expected_stderr
示例#4
0
def test_stdin_stream():
    p = subby.Processes([["cat"]],
                        stdin=subby.StdType.PIPE,
                        stdout=subby.StdType.PIPE)

    try:
        p.run()
        assert p.stdin_type == subby.StdType.PIPE
        stdin = p.stdin_stream
        stdout = p.stdout_stream
        stdin.write("hello\n")
        stdin.flush()
        assert stdout.readline() == "hello\n"
    finally:
        p.block()
示例#5
0
def test_stdin_sys(mode, expected):
    # We have to use a tempfile to mock stdin - an io.BytesIO doesn't work
    # because the fileno method is called
    mode_str = "t" if mode is str else "b"
    with isolated_dir() as d:
        mock_stdin = d / "stdin"
        with open(mock_stdin, "w" + mode_str) as out:
            out.write(expected)
        cur_stdin = sys.stdin
        try:
            with open(mock_stdin, "r" + mode_str) as inp:
                sys.stdin = inp
                p = subby.Processes([["grep", "hi"]],
                                    stdin=subby.StdType.SYS,
                                    mode=mode)
                p.run()
                p.block()
                assert p.output == expected
        finally:
            sys.stdin = cur_stdin
示例#6
0
def test_files(mode, expected_stdout, expected_stderr):
    with isolated_dir() as d:
        stdout = d / "stdout"
        stderr = d / "stderr"
        p = subby.Processes([["echo", "hi"]],
                            stdout=stdout,
                            stderr=stderr,
                            mode=mode)
        p.run(echo=True)
        p.block()
        assert (str(p)) == f"echo hi > {stdout}"
        assert p._stdout_type is subby.StdType.FILE
        assert p._stderr_type is subby.StdType.FILE
        assert p.stdout_stream is not None
        assert p.stderr_stream is not None
        with pytest.raises(RuntimeError):
            p.output
        with pytest.raises(RuntimeError):
            p.error
        mode_str = "t" if mode is str else "b"
        with open(stdout, "r" + mode_str) as inp:
            assert inp.read() == expected_stdout
        with open(stderr, "r" + mode_str) as inp:
            assert inp.read() == expected_stderr
示例#7
0
def test_timeout():
    p = subby.Processes([["sleep", "10"]])
    p.run()
    with pytest.raises(subprocess.TimeoutExpired):
        p.block(timeout=1)
示例#8
0
def test_stdin_bytes(mode, expected):
    p = subby.Processes([["grep", "hi"]], stdin="hi", mode=mode)
    p.run()
    p.block()
    assert expected == p.output
示例#9
0
def test_state_errors():
    p = subby.Processes([["echo", "hi"]], stdout=None, stderr=None)
    with pytest.raises(RuntimeError):
        p.stdout_stream
    with pytest.raises(RuntimeError):
        p.stderr_stream
    with pytest.raises(RuntimeError):
        p.get_all_stderr()
    with pytest.raises(RuntimeError):
        p.block()
    with pytest.raises(RuntimeError):
        p.close()
    p.run(echo=True)
    with pytest.raises(RuntimeError):
        p.run()
    p.block()
    with pytest.raises(RuntimeError):
        p.block()

    p = subby.Processes([["echo", "hi"]], stdout=None, stderr=None)
    p.run(echo=True)
    p.block()
    assert p._stdout_type is subby.StdType.OTHER
    assert p._stderr_type is subby.StdType.OTHER
    assert p.stdout_stream is None
    assert p.stderr_stream is None
    with pytest.raises(RuntimeError):
        p.output
    with pytest.raises(RuntimeError):
        p.error

    with pytest.raises(ValueError):
        p = subby.Processes([["echo", "hi"]], stdout=subby.StdType.FILE)
        p.run()

    subby.Processes([["echo", "hi"]], mode=str, universal_newlines=True)
    subby.Processes([["echo", "hi"]], mode=str, text=True)
    subby.Processes([["echo", "hi"]], mode=bytes, universal_newlines=False)
    subby.Processes([["echo", "hi"]], mode=bytes, text=False)

    with pytest.raises(ValueError):
        subby.Processes([["echo", "hi"]], mode=str, universal_newlines=False)
    with pytest.raises(ValueError):
        subby.Processes([["echo", "hi"]], mode=str, text=False)
    with pytest.raises(ValueError):
        subby.Processes([["echo", "hi"]], mode=bytes, universal_newlines=True)
    with pytest.raises(ValueError):
        subby.Processes([["echo", "hi"]], mode=bytes, text=True)