Files

91 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Acceptance check for task fixbug-ledger.
Usage: python3 accept.py WORKSPACE
Exit 0 when the task is done; any other exit code means not done. The workspace is never modified: everything runs in a temporary copy.
"""
import hashlib
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
PRISTINE = HERE / "hidden" / "pristine_tests"
HIDDEN_TESTS = HERE / "hidden" / "test_hidden.py"
IGNORE = shutil.ignore_patterns("__pycache__", "*.pyc", ".git")
failed = []
def check(name, ok, detail=""):
if ok:
print("PASS " + name)
else:
print("FAIL %s :: %s" % (name, detail.strip()[-1500:]))
failed.append(name)
def digest(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def run_unittest(cwd, start_dir, timeout=100):
env = dict(os.environ, PYTHONDONTWRITEBYTECODE="1", PYTHONHASHSEED="0")
try:
proc = subprocess.run(
[sys.executable, "-m", "unittest", "discover", "-s", start_dir],
cwd=str(cwd), env=env, capture_output=True, text=True, timeout=timeout,
)
except subprocess.TimeoutExpired:
return False, "timeout"
return proc.returncode == 0, proc.stdout + proc.stderr
def main():
if len(sys.argv) != 2:
print("usage: accept.py WORKSPACE")
return 2
workspace = Path(sys.argv[1]).resolve()
if not workspace.is_dir():
print("FAIL workspace :: not a directory")
return 2
# 0. nothing may shadow the standard library or hook the interpreter start-up
shadow = {"sitecustomize", "usercustomize", "unittest", "decimal", "datetime", "csv", "json", "argparse", "calendar"}
clashes = sorted(p.name for p in workspace.iterdir() if p.stem in shadow)
check("no-shadowing", not clashes, "top-level files shadow standard modules: %s" % ", ".join(clashes))
# 1. the pristine tests must be untouched
for src in sorted(PRISTINE.rglob("*")):
if not src.is_file() or "__pycache__" in src.parts:
continue
rel = src.relative_to(PRISTINE)
target = workspace / "tests" / rel
same = target.is_file() and digest(target) == digest(src)
check("tests-untouched:" + rel.as_posix(), same, "tests/%s is missing or was modified" % rel.as_posix())
with tempfile.TemporaryDirectory() as tmp:
work = Path(tmp) / "ws"
shutil.copytree(workspace, work, ignore=IGNORE)
# run the pristine tests, not whatever is in tests/ now
shutil.rmtree(work / "tests", ignore_errors=True)
shutil.copytree(PRISTINE, work / "tests", ignore=IGNORE)
ok, out = run_unittest(work, "tests")
check("visible-tests", ok, out)
hidden_dir = work / "_hidden"
hidden_dir.mkdir()
shutil.copy(HIDDEN_TESTS, hidden_dir / "test_hidden.py")
ok, out = run_unittest(work, "_hidden")
check("hidden-tests", ok, out)
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())