-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
69 lines (56 loc) · 1.45 KB
/
test.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
from pathlib import Path
from time import sleep
import subprocess
input = Path('testdata/input')
output = Path('testdata/output')
CMD = [
'src/build.py',
'--input' , str(input),
'--output', str(output),
]
def test_basic() -> None:
subprocess.check_call(CMD)
def wait(total_s: float, step_s: float):
for _ in range(int(total_s / step_s)):
yield
sleep(step_s)
else:
raise TimeoutError(f'Timed out after {total_s} secs')
def mtime(p: Path):
try:
return p.stat().st_mtime
except FileNotFoundError as fe:
return None
def test_watch() -> None:
i = input / 'post_1.org'
tags = input / 'tags.org'
o = output / 'post_1.html'
i.touch()
if o.exists():
o.unlink()
mi = mtime(i)
mo = None
with tmp_popen(CMD + ['--watch']) as p:
for _ in wait(5, 0.1):
mo = mtime(o)
if (mo or 0.0) > mi:
break
i.touch()
assert mtime(o) < mtime(i)
for _ in wait(5, 0.1):
if mtime(o) > mtime(i):
break
from src.compile_org import *
from contextlib import contextmanager
@contextmanager
def tmp_popen(*args, **kwargs):
import psutil # type: ignore
with psutil.Popen(*args, **kwargs) as p:
try:
yield p
finally:
for c in p.children(recursive=True):
c.kill()
p.kill()
p.wait()