File: test_proc.py

package info (click to toggle)
easyprocess 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: python: 774; sh: 25; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 2,178 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from __future__ import with_statement

import sys
import time

import pytest

from easyprocess import EasyProcess

python = sys.executable


def test_call():
    assert EasyProcess("ls -la").call().return_code == 0
    assert EasyProcess(["ls", "-la"]).call().return_code == 0


def test_start():
    p = EasyProcess("ls -la").start()
    time.sleep(0.2)
    assert p.stop().return_code == 0


def test_start2():
    p = EasyProcess("echo hi").start()
    time.sleep(0.2)
    # no wait() -> no results
    assert p.return_code is None
    assert p.stdout is None


@pytest.mark.timeout(1)
def test_start3():
    p = EasyProcess("sleep 10").start()
    assert p.return_code is None


def test_alive():
    assert EasyProcess("ping 127.0.0.1 -c 2").is_alive() is False
    assert EasyProcess("ping 127.0.0.1 -c 2").start().is_alive()
    assert EasyProcess("ping 127.0.0.1 -c 2").start().stop().is_alive() is False
    assert EasyProcess("ping 127.0.0.1 -c 2").call().is_alive() is False


def test_std():
    assert EasyProcess("echo hello").call().stdout == "hello"
    assert EasyProcess([python, "-c", "print(42)"]).call().stdout == "42"


def test_wait():
    assert EasyProcess("echo hello").wait().return_code is None
    assert EasyProcess("echo hello").wait().stdout is None

    assert EasyProcess("echo hello").start().wait().return_code == 0
    assert EasyProcess("echo hello").start().wait().stdout == "hello"


#     def test_xephyr():
#         EasyProcess('Xephyr -help').check(return_code=1)


def test_wrap():
    def f():
        return EasyProcess("echo hi").call().stdout

    assert EasyProcess("ping 127.0.0.1").wrap(f)() == "hi"


def test_with():
    with EasyProcess("ping 127.0.0.1") as x:
        assert x.is_alive()
    assert x.return_code != 0
    assert not x.is_alive()


def test_parse():
    assert EasyProcess("ls -la").cmd == ["ls", "-la"]
    assert EasyProcess('ls "abc"').cmd == ["ls", "abc"]
    assert EasyProcess('ls "ab c"').cmd == ["ls", "ab c"]


def test_stop():
    p = EasyProcess("ls -la").start()
    time.sleep(0.2)
    assert p.stop().return_code == 0
    assert p.stop().return_code == 0
    assert p.stop().return_code == 0