Repository navigation
Expand file tree
/
Copy pathfixture_common.py
More file actions
83 lines (67 loc) · 3.14 KB
/
Copy pathfixture_common.py
File metadata and controls
83 lines (67 loc) · 3.14 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
"""Run bounded, independent MTP contracts against an unchanged NUnit specimen.
Evidence is append-only by run directory. Expected assertion failures are checked
as outcomes; missing results, process termination and token delivery are distinct.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import platform
import re
import signal
import subprocess
import time
import xml.etree.ElementTree as ET
def save(path, value):
path.write_text(json.dumps(value, indent=2, ensure_ascii=False), encoding="utf-8")
def execute(command, directory, cwd, environment, timeout=45):
directory.mkdir(parents=True, exist_ok=False)
started = time.monotonic()
with (directory / "stdout.txt").open("w", encoding="utf-8") as stdout, \
(directory / "stderr.txt").open("w", encoding="utf-8") as stderr:
child = subprocess.Popen(command, cwd=cwd, env=environment, stdout=stdout,
stderr=stderr, start_new_session=os.name != "nt")
timed_out = False
try:
child.wait(timeout=timeout)
except subprocess.TimeoutExpired:
timed_out = True
if os.name == "nt":
subprocess.run(["taskkill", "/PID", str(child.pid), "/T", "/F"],
stdout=stderr, stderr=stderr, check=False)
else:
os.killpg(child.pid, signal.SIGKILL)
child.wait()
result = {"command": command, "cwd": str(cwd), "pid": child.pid,
"exit": child.returncode, "timedOut": timed_out,
"seconds": round(time.monotonic() - started, 3)}
save(directory / "process.json", result)
return result
def discovered(directory):
value = json.loads((directory / "stdout.txt").read_text(encoding="utf-8-sig"))
if "testContainers" in value:
assert value["version"] == "1.0" and len(value["testContainers"]) == 1
value = value["testContainers"][0]
else:
assert value["schemaVersion"] == 1
return sorted([(test["uid"], test["displayName"]) for test in value["tests"]])
def results(directory):
files = list(directory.rglob("*.trx"))
assert len(files) == 1, "Expected exactly one TRX report, found " + str(len(files))
return [item for item in ET.parse(files[0]).iter()
if item.tag.split("}")[-1] == "UnitTestResult"]
def observations(directory, phase):
return [ET.parse(path).getroot() for path in
(directory / "observations").glob(phase + "-*.xml")]
def settings(directory, negative=False):
document = ET.Element("RunSettings")
nunit = ET.SubElement(document, "NUnit")
ET.SubElement(nunit, "NumberOfTestWorkers").text = "0"
parameters = ET.SubElement(document, "TestRunParameters")
values = {"evidence": str(directory / "observations"),
"text": "alpha; beta = café & λ", "integer": "41" if negative else "42",
"boolean": "true"}
for key, value in values.items():
ET.SubElement(parameters, "Parameter", {"name": key, "value": value})
return ET.tostring(document, encoding="unicode")