-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconftest.py
More file actions
97 lines (87 loc) · 2.74 KB
/
conftest.py
File metadata and controls
97 lines (87 loc) · 2.74 KB
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
88
89
90
91
92
93
94
95
96
import subprocess as sp
import os
import textwrap
import shutil
import functools
from pathlib import Path
import shlex
import pytest
def pytest_addoption(parser):
parser.addoption(
'--keep_files',
action='store_true',
help='Preserve SymbiYosys .sby file, log file and output directory for tests using sby_run fixture',)
def run(cmd):
if not isinstance(cmd,str):
cmd = " ".join([str(i) for i in cmd])
r = ""
try:
print("subprocess>",cmd)
r = sp.run(cmd,shell=True,capture_output=True,text=True,check=True).stdout
except sp.CalledProcessError as e:
print(e.stdout)
print(e.stderr)
raise ChildProcessError from None
return r
def get_line(opt):
opt_str = opt
if isinstance(opt,Path):
opt_str = str(opt)
elif not isinstance(opt,str):
opt_str = shlex.join([str(i) for i in opt])
return opt_str
def get_lines(name,opts):
lines = [f"[{name}]"]
if isinstance(opts,str):
opts = [opts]
if name == "files":
temp_opts = []
for file in opts:
if isinstance(file,str) or isinstance(file,Path) or len(file) == 1:
file = Path(file)
file = [file.name,file]
temp_opts.append(file)
opts = temp_opts
for opt in opts:
lines.append(get_line(opt))
return lines
def get_config_file(options,engines,script,files):
lines = []
lines.extend(get_lines("options",options))
lines.extend(get_lines("engines",engines))
lines.extend(get_lines("script",script))
lines.extend(get_lines("files",files))
return '\n'.join(lines) + '\n'
def _sby_run(sby_path,log_path,options,engines,script,files):
indent = 4
print()
sby_file = get_config_file(options,engines,script,files)
print("SBY file:")
print(textwrap.indent(sby_file," "*indent))
sby_path.write_text(sby_file)
sby_log = run(["sby","-f",sby_path,"2>&1","|","tee",log_path])
print("SBY log:")
print(textwrap.indent(sby_log," "*indent))
for line in sby_log.splitlines():
if "DONE" in line:
assert("PASS" in line)
def remove(path):
path = Path(path)
print("Removing",path.resolve())
if path.is_dir():
shutil.rmtree(path)
else:
path.unlink()
@pytest.fixture
def sby_run(request):
test_dir = Path(request.module.__file__).parent.resolve()
keep_files = request.config.getoption('--keep_files')
test_name = request.node.name
sby_path = (test_dir/test_name).with_suffix(".sby")
log_path = (test_dir/test_name).with_suffix(".log")
yield functools.partial(_sby_run,sby_path,log_path)
print()
if not keep_files:
remove(sby_path)
remove(log_path)
remove(test_dir/test_name)