-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
55 lines (40 loc) · 1.34 KB
/
tests.py
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
from contextlib import contextmanager
from subprocess import PIPE
import logmux
import os.path
import pathlib
import pytest
import subprocess
import tempfile
@contextmanager
def logmux_process(paths, **kwargs):
"Creates a logmux process that is ready for new input."
command = ["logmux", "--verbose"] + paths
with subprocess.Popen(command, stdout=PIPE, stderr=PIPE) as proc:
# Wait for the process to print on stderr when it's ready
proc.stderr.readline()
try:
yield proc
finally:
proc.terminate()
@contextmanager
def named_tempfiles(names):
"Creates a set of named temporary files."
with tempfile.TemporaryDirectory() as tmpdir:
paths = [os.path.join(tmpdir, name) for name in names]
for path in paths:
pathlib.Path(path).touch()
yield paths
def appendfile(path, bytestr):
"Append bytes to a file."
with open(path, "ab") as target:
target.write(bytestr)
def test_basics():
with named_tempfiles(["log1.log", "log2.log"]) as logpaths:
print(logpaths)
with logmux_process(logpaths) as proc:
appendfile(logpaths[0], b"test line 1\n")
appendfile(logpaths[0], b"test line 2\n")
assert proc.stdout.readline() == b"log1: test line 1\n"
if __name__ == "__main__":
test_basics()