docker_test / src /eval_go.py
朱东升
requirements update24
43675d0
raw
history blame
1.89 kB
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')