朱东升 commited on
Commit
43675d0
·
1 Parent(s): 82b524a

requirements update24

Browse files
Files changed (1) hide show
  1. src/eval_go.py +34 -8
src/eval_go.py CHANGED
@@ -2,6 +2,8 @@ import argparse
2
  from sys import exit
3
  import subprocess
4
  from pathlib import Path
 
 
5
  from .generic_eval import main as gmain
6
 
7
 
@@ -10,16 +12,42 @@ def eval_script(path: Path):
10
  stdout = None
11
  stderr = None
12
  exit_code = None
13
- try:
14
- build = subprocess.run(["go", "test", path],
15
- timeout=30,
16
- stdout=subprocess.PIPE,
17
- stderr=subprocess.PIPE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  stdout = build.stdout.decode("utf-8", errors="ignore")
20
  stderr = build.stderr.decode("utf-8", errors="ignore")
21
  exit_code = build.returncode
22
- # write to stderr just so that we can redirect stdout to a csv
23
 
24
  if "[setup failed]" in stdout or "[build failed]" in stdout:
25
  status = "SyntaxError"
@@ -27,8 +55,6 @@ def eval_script(path: Path):
27
  status = "Exception"
28
  else:
29
  status = "OK"
30
- except subprocess.TimeoutExpired:
31
- status = "Timeout"
32
 
33
  return {
34
  "status": status,
 
2
  from sys import exit
3
  import subprocess
4
  from pathlib import Path
5
+ import os
6
+ import tempfile
7
  from .generic_eval import main as gmain
8
 
9
 
 
12
  stdout = None
13
  stderr = None
14
  exit_code = None
15
+
16
+ # Create a temporary directory for the Go module
17
+ with tempfile.TemporaryDirectory() as temp_dir:
18
+ # Copy the test file to the temporary directory
19
+ test_file = Path(temp_dir) / path.name
20
+ with open(path, 'r') as src, open(test_file, 'w') as dst:
21
+ dst.write(src.read())
22
+
23
+ # Initialize a Go module in the temporary directory
24
+ init_result = subprocess.run(
25
+ ["go", "mod", "init", "testmodule"],
26
+ cwd=temp_dir,
27
+ capture_output=True,
28
+ text=True
29
+ )
30
+
31
+ if init_result.returncode != 0:
32
+ return {
33
+ "status": "SyntaxError",
34
+ "exit_code": init_result.returncode,
35
+ "stdout": init_result.stdout,
36
+ "stderr": init_result.stderr,
37
+ }
38
+
39
+ # Run the test
40
+ build = subprocess.run(
41
+ ["go", "test", "-v"],
42
+ cwd=temp_dir,
43
+ timeout=30,
44
+ stdout=subprocess.PIPE,
45
+ stderr=subprocess.PIPE
46
+ )
47
 
48
  stdout = build.stdout.decode("utf-8", errors="ignore")
49
  stderr = build.stderr.decode("utf-8", errors="ignore")
50
  exit_code = build.returncode
 
51
 
52
  if "[setup failed]" in stdout or "[build failed]" in stdout:
53
  status = "SyntaxError"
 
55
  status = "Exception"
56
  else:
57
  status = "OK"
 
 
58
 
59
  return {
60
  "status": status,