feat(runner): compile Go and check answers per test group
This commit is contained in:
1 parent
e95c681165
commit
afee564bf4
61 files changed
+2592
-143
No files matched your search
@@ -6,7 +6,9 @@ import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -14,18 +16,23 @@ import (
|
||||
"lincodex/runner/internal/sandbox"
|
||||
)
|
||||
|
||||
// scriptedExecutor answers runs from a script keyed by the stdin of a test;
|
||||
// the compile step has no stdin.
|
||||
// scriptedExecutor answers test runs from a script keyed by the stdin of a
|
||||
// test, compile steps from compileResults in order, and custom checker runs
|
||||
// from checkerExits keyed by the actual output the checker reads.
|
||||
type scriptedExecutor struct {
|
||||
root string
|
||||
results map[string]sandbox.Result
|
||||
errs map[string]error
|
||||
runs []string
|
||||
boxes []string
|
||||
root string
|
||||
results map[string]sandbox.Result
|
||||
errs map[string]error
|
||||
compileResults []sandbox.Result
|
||||
checkerExits map[string]int
|
||||
runs []string
|
||||
compileSteps [][]string
|
||||
checkerRuns int
|
||||
boxes []string
|
||||
}
|
||||
|
||||
func (e *scriptedExecutor) NewBox(fileName string, content []byte) (*sandbox.Box, error) {
|
||||
box, err := sandbox.CreateBox(e.root, fileName, content)
|
||||
box, err := sandbox.CreateBox(e.root, fileName, content, sandbox.Owner{UID: -1, GID: -1})
|
||||
if err == nil {
|
||||
e.boxes = append(e.boxes, box.Dir())
|
||||
}
|
||||
@@ -33,6 +40,19 @@ func (e *scriptedExecutor) NewBox(fileName string, content []byte) (*sandbox.Box
|
||||
}
|
||||
|
||||
func (e *scriptedExecutor) Run(_ context.Context, spec sandbox.Spec, _ sandbox.Limits) (sandbox.Result, error) {
|
||||
for _, mount := range spec.Mounts {
|
||||
if mount.Target == sandbox.CheckPath {
|
||||
return e.runChecker(mount)
|
||||
}
|
||||
if mount.Target == sandbox.OutPath && mount.Writable {
|
||||
index := len(e.compileSteps)
|
||||
e.compileSteps = append(e.compileSteps, spec.Argv)
|
||||
if index < len(e.compileResults) {
|
||||
return e.compileResults[index], nil
|
||||
}
|
||||
return sandbox.Result{}, nil
|
||||
}
|
||||
}
|
||||
key := string(spec.Stdin)
|
||||
e.runs = append(e.runs, key)
|
||||
if err := e.errs[key]; err != nil {
|
||||
@@ -44,9 +64,22 @@ func (e *scriptedExecutor) Run(_ context.Context, spec sandbox.Spec, _ sandbox.L
|
||||
return sandbox.Result{}, nil
|
||||
}
|
||||
|
||||
func (e *scriptedExecutor) runChecker(mount sandbox.Mount) (sandbox.Result, error) {
|
||||
e.checkerRuns++
|
||||
actual, err := os.ReadFile(filepath.Join(mount.Source, "actual"))
|
||||
if err != nil {
|
||||
return sandbox.Result{}, err
|
||||
}
|
||||
exit, ok := e.checkerExits[string(actual)]
|
||||
if !ok {
|
||||
exit = 3
|
||||
}
|
||||
return sandbox.Result{ExitCode: exit}, nil
|
||||
}
|
||||
|
||||
func newScripted(t *testing.T) *scriptedExecutor {
|
||||
t.Helper()
|
||||
return &scriptedExecutor{root: t.TempDir(), results: map[string]sandbox.Result{}, errs: map[string]error{}}
|
||||
return &scriptedExecutor{root: t.TempDir(), results: map[string]sandbox.Result{}, errs: map[string]error{}, checkerExits: map[string]int{}}
|
||||
}
|
||||
|
||||
func output(text string) sandbox.Result {
|
||||
@@ -157,14 +190,14 @@ func TestRunReportsCompileErrorsWithoutRunningTests(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
executor := newScripted(t)
|
||||
executor.results[""] = sandbox.Result{ExitCode: 1, Stderr: []byte("SyntaxError: invalid syntax\n")}
|
||||
executor.compileResults = []sandbox.Result{{ExitCode: 1, Stderr: []byte("SyntaxError: invalid syntax\n")}}
|
||||
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
||||
|
||||
if result.Outcome != CompileError || result.Compile.Diagnostic != "SyntaxError: invalid syntax\n" {
|
||||
t.Fatalf("result = %+v, want compile_error with the diagnostic", result)
|
||||
}
|
||||
if len(executor.runs) != 1 {
|
||||
t.Fatalf("runs = %q, want only the compile step", executor.runs)
|
||||
if len(executor.runs) != 0 || len(executor.compileSteps) != 1 {
|
||||
t.Fatalf("runs = %q after %d compile steps, want only the compile step", executor.runs, len(executor.compileSteps))
|
||||
}
|
||||
for _, group := range result.Groups {
|
||||
if group.Outcome != CompileError || len(group.Tests) != 0 || group.Total != 2 {
|
||||
@@ -210,3 +243,94 @@ func assertBoxesRemoved(t *testing.T, executor *scriptedExecutor) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func goRequest(groups ...Group) Request {
|
||||
return Request{
|
||||
Language: "go",
|
||||
Source: "package main\nfunc main() {}\n",
|
||||
Limits: Limits{TimeLimit: time.Second, MemoryBytes: 64 << 20, OutputBytes: 64 << 10},
|
||||
Groups: groups,
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoCompilesAndLinksBeforeTheTests(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
executor := newScripted(t)
|
||||
executor.results["1"] = output("1\n")
|
||||
group := Group{ID: "public", Visibility: Public, Checker: checker.Spec{Kind: checker.Exact}, Tests: []Test{{ID: "t1", Input: "1", ExpectedOutput: "1\n"}}}
|
||||
result := New(executor, discardLogger(), Go("/usr/local/lib/lincodex-runner/go")).Run(t.Context(), goRequest(group))
|
||||
|
||||
if result.Outcome != Accepted || len(executor.compileSteps) != 2 {
|
||||
t.Fatalf("result %+v after %d compile steps, want accepted after compile and link", result, len(executor.compileSteps))
|
||||
}
|
||||
if !strings.HasSuffix(executor.compileSteps[0][0], "/compile") || !strings.HasSuffix(executor.compileSteps[1][0], "/link") {
|
||||
t.Fatalf("compile steps = %q", executor.compileSteps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoCompileErrorRunsNoTest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, failing := range []int{0, 1} {
|
||||
executor := newScripted(t)
|
||||
executor.compileResults = make([]sandbox.Result, failing+1)
|
||||
executor.compileResults[failing] = sandbox.Result{ExitCode: 2, Stderr: []byte("main.go:2:14: declared and not used: x\n")}
|
||||
group := Group{ID: "hidden-1", Visibility: Hidden, Checker: checker.Spec{Kind: checker.Exact}, Tests: []Test{{ID: "t1", Input: "1", ExpectedOutput: "1"}}}
|
||||
result := New(executor, discardLogger(), Go("/go")).Run(t.Context(), goRequest(group))
|
||||
|
||||
if result.Outcome != CompileError || !strings.Contains(result.Compile.Diagnostic, "declared and not used") {
|
||||
t.Fatalf("step %d: result %+v, want compile_error with the diagnostic", failing, result)
|
||||
}
|
||||
if len(executor.runs) != 0 || len(result.Groups[0].Tests) != 0 || result.Groups[0].Outcome != CompileError {
|
||||
t.Fatalf("step %d: tests ran after a compile error: %q", failing, executor.runs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomCheckerDecidesByExitStatusAndIsCompiledOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
executor := newScripted(t)
|
||||
executor.results["a"] = output("good\n")
|
||||
executor.results["b"] = output("bad\n")
|
||||
executor.checkerExits["good\n"] = 0
|
||||
executor.checkerExits["bad\n"] = 1
|
||||
custom := checker.Spec{Kind: checker.Custom, Program: &checker.Program{Language: "python3", Source: "import sys\n"}}
|
||||
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), Request{
|
||||
Language: "python3",
|
||||
Source: "print('x')",
|
||||
Limits: Limits{TimeLimit: time.Second, MemoryBytes: 64 << 20, OutputBytes: 64 << 10},
|
||||
Groups: []Group{
|
||||
{ID: "public", Visibility: Public, Checker: custom, Tests: []Test{{ID: "t1", Input: "a", ExpectedOutput: "e"}}},
|
||||
{ID: "hidden-1", Visibility: Hidden, Checker: custom, Tests: []Test{{ID: "t1", Input: "b", ExpectedOutput: "e"}}},
|
||||
},
|
||||
})
|
||||
|
||||
if result.Groups[0].Outcome != Accepted || result.Groups[1].Outcome != WrongAnswer {
|
||||
t.Fatalf("groups = %+v, want accepted then wrong_answer", result.Groups)
|
||||
}
|
||||
// One syntax check for the solution and one for the shared checker.
|
||||
if len(executor.compileSteps) != 2 || executor.checkerRuns != 2 {
|
||||
t.Fatalf("compile steps %d, checker runs %d; want 2 and 2", len(executor.compileSteps), executor.checkerRuns)
|
||||
}
|
||||
assertBoxesRemoved(t, executor)
|
||||
}
|
||||
|
||||
func TestFailingCustomCheckerIsAnInfrastructureFailure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
executor := newScripted(t)
|
||||
executor.results["a"] = output("unknown\n")
|
||||
custom := checker.Spec{Kind: checker.Custom, Program: &checker.Program{Language: "python3", Source: "raise SystemExit(3)\n"}}
|
||||
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), Request{
|
||||
Language: "python3",
|
||||
Source: "print('x')",
|
||||
Limits: Limits{TimeLimit: time.Second, MemoryBytes: 64 << 20, OutputBytes: 64 << 10},
|
||||
Groups: []Group{{ID: "public", Visibility: Public, Checker: custom, Tests: []Test{{ID: "t1", Input: "a", ExpectedOutput: "e"}}}},
|
||||
})
|
||||
if result.Outcome != InfrastructureFailure {
|
||||
t.Fatalf("outcome = %s, want infrastructure_failure", result.Outcome)
|
||||
}
|
||||
assertBoxesRemoved(t, executor)
|
||||
}
|
||||
Reference in new issue
Block a user