File size: 1,887 Bytes
30b1610
 
 
 
43675d0
 
5491a9c
30b1610
 
 
 
 
 
 
43675d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30b1610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
from sys import exit
import subprocess
from pathlib import Path
import os
import tempfile
from .generic_eval import main as gmain


def eval_script(path: Path):
    status = None
    stdout = None
    stderr = None
    exit_code = None
    
    # Create a temporary directory for the Go module
    with tempfile.TemporaryDirectory() as temp_dir:
        # Copy the test file to the temporary directory
        test_file = Path(temp_dir) / path.name
        with open(path, 'r') as src, open(test_file, 'w') as dst:
            dst.write(src.read())
            
        # Initialize a Go module in the temporary directory
        init_result = subprocess.run(
            ["go", "mod", "init", "testmodule"],
            cwd=temp_dir,
            capture_output=True,
            text=True
        )
        
        if init_result.returncode != 0:
            return {
                "status": "SyntaxError",
                "exit_code": init_result.returncode,
                "stdout": init_result.stdout,
                "stderr": init_result.stderr,
            }
            
        # Run the test
        build = subprocess.run(
            ["go", "test", "-v"],
            cwd=temp_dir,
            timeout=30,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )

        stdout = build.stdout.decode("utf-8", errors="ignore")
        stderr = build.stderr.decode("utf-8", errors="ignore")
        exit_code = build.returncode

        if "[setup failed]" in stdout or "[build failed]" in stdout:
            status = "SyntaxError"
        elif "FAIL" in stdout:
            status = "Exception"
        else:
            status = "OK"

    return {
        "status": status,
        "exit_code": exit_code,
        "stdout": stdout,
        "stderr": stderr,
    }


if __name__ == "__main__":
    gmain(eval_script, 'Go', '.go')