File size: 2,341 Bytes
4bc63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Node: gate_checker. Independent re-verification of the applied pair.

It does not read the executor receipt as evidence. It re-reads the tree, checks the shape
is gone, checks the lock test exists and asserts something, and runs the suite itself.
"""

import ast
import json
import os
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)

from lingologos_thinkingpython import session, shapes  # noqa: E402


def main() -> int:
    task = json.load(sys.stdin)
    workspace, plan = task["workspace"], task["plan"]
    problems = []
    path = os.path.join(workspace, plan["file"])
    try:
        with open(path, "r", encoding="utf-8") as handle:
            source = handle.read()
    except OSError:
        print(json.dumps({"node": "gate_checker", "status": "fail",
                          "reasons": ["SEARCH_NOT_FOUND"]}))
        return 1
    for edit in plan["edits"]:
        if edit["replace"] and edit["replace"] not in source:
            problems.append("PAIR_DISHONEST")
            break
    hits, _ = shapes.match(plan["frame"], source)
    if any(h["symbol"] == plan["symbol"] and h["line"] == plan["line"] for h in hits):
        problems.append("SHAPE_STILL_PRESENT")
    lock_name = "test_lock_%s.py" % plan["symbol"]
    lock_path = os.path.join(workspace, "tests", lock_name)
    if not os.path.isfile(lock_path):
        problems.append("PAIR_DISHONEST")
    else:
        with open(lock_path, "r", encoding="utf-8") as handle:
            tree = ast.parse(handle.read())
        tests = [n for n in ast.walk(tree)
                 if isinstance(n, ast.FunctionDef) and n.name.startswith("test_")]
        asserts = [n for n in ast.walk(tree) if isinstance(n, ast.Assert)]
        if not tests or not asserts:
            problems.append("PAIR_DISHONEST")
    cwd = os.getcwd()
    os.chdir(workspace)
    try:
        suite = session.run_suite(workspace)
    finally:
        os.chdir(cwd)
    if suite["status"] != "GREEN":
        problems.append(suite["status"])
    status = "pass" if not problems else "fail"
    print(json.dumps({"node": "gate_checker", "status": status,
                      "reasons": sorted(set(problems)), "suite": suite["status"]}))
    return 0 if status == "pass" else 1


if __name__ == "__main__":
    raise SystemExit(main())