feat(runner): add the own code runner contract, client and nsjail service

This commit is contained in:
shira committed 2026-10-11 08:23:42 +03:00
1 parent 5e1db37746
commit cdf6bcbb91
109 files changed
+24084 -28

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
// Package checker compares the output of a sandboxed program with the expected
// output of a test.
//
// A checker is selected by a Spec. Kinds that need parameters, such as a float
// tolerance, add fields to Spec; kinds that need to run code, such as a custom
// checker program, implement Checker with their own sandbox and use the
// context and the error result for cancellation and infrastructure failures.
package checker
import (
"bytes"
"context"
"fmt"
)
// Kind names a comparison rule of the runner contract.
type Kind string
const (
// Exact accepts texts that are equal after line-ending and trailing
// whitespace normalization.
Exact Kind = "exact"
// Tokens accepts texts with the same sequence of whitespace-separated
// tokens.
Tokens Kind = "tokens"
)
// Spec selects and parameterizes a checker.
type Spec struct {
Kind Kind `json:"kind"`
}
// Input is everything a checker may look at for one test.
type Input struct {
TestInput []byte
Expected []byte
Actual []byte
}
// Checker decides whether the actual output of a test is accepted.
type Checker interface {
// Check reports whether the output is accepted. An error means that the
// checker itself failed, not that the output is wrong.
Check(ctx context.Context, input Input) (bool, error)
}
// New returns the checker for spec.
func New(spec Spec) (Checker, error) {
switch spec.Kind {
case Exact:
return exactChecker{}, nil
case Tokens:
return tokensChecker{}, nil
default:
return nil, fmt.Errorf("checker.New - unknown kind %q", spec.Kind)
}
}
type exactChecker struct{}
func (exactChecker) Check(_ context.Context, input Input) (bool, error) {
return bytes.Equal(normalizeLines(input.Expected), normalizeLines(input.Actual)), nil
}
type tokensChecker struct{}
func (tokensChecker) Check(_ context.Context, input Input) (bool, error) {
expected := bytes.Fields(input.Expected)
actual := bytes.Fields(input.Actual)
if len(expected) != len(actual) {
return false, nil
}
for index := range expected {
if !bytes.Equal(expected[index], actual[index]) {
return false, nil
}
}
return true, nil
}
// normalizeLines replaces CRLF with LF, removes spaces and tabs at the end of
// every line, and removes trailing empty lines.
func normalizeLines(text []byte) []byte {
lines := bytes.Split(bytes.ReplaceAll(text, []byte("\r\n"), []byte("\n")), []byte("\n"))
for index, line := range lines {
lines[index] = bytes.TrimRight(line, " \t")
}
last := len(lines)
for last > 0 && len(lines[last-1]) == 0 {
last--
}
return bytes.Join(lines[:last], []byte("\n"))
}
+53
View File
@@ -0,0 +1,53 @@
package checker
import (
"testing"
)
func TestCheckersCompareOutput(t *testing.T) {
t.Parallel()
cases := []struct {
name string
kind Kind
expected string
actual string
accepted bool
}{
{"exact equal", Exact, "6\n", "6\n", true},
{"exact missing final newline", Exact, "6\n", "6", true},
{"exact CRLF and trailing spaces", Exact, "1 2\n3\n", "1 2 \r\n3\t\r\n\r\n", true},
{"exact inner spaces matter", Exact, "1 2\n", "1 2\n", false},
{"exact leading empty line matters", Exact, "1\n", "\n1\n", false},
{"exact different value", Exact, "6\n", "7\n", false},
{"exact empty output", Exact, "", "\n\n", true},
{"tokens ignore layout", Tokens, "1 2\n3\n", "1\n2 3", true},
{"tokens different count", Tokens, "1 2 3", "1 2", false},
{"tokens different token", Tokens, "1 2 3", "1 2 4", false},
{"tokens are case sensitive", Tokens, "Yes", "yes", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
check, err := New(Spec{Kind: tc.kind})
if err != nil {
t.Fatalf("New: %v", err)
}
accepted, err := check.Check(t.Context(), Input{Expected: []byte(tc.expected), Actual: []byte(tc.actual)})
if err != nil {
t.Fatalf("Check: %v", err)
}
if accepted != tc.accepted {
t.Fatalf("Check(%q, %q) = %v, want %v", tc.expected, tc.actual, accepted, tc.accepted)
}
})
}
}
func TestNewRejectsUnknownKind(t *testing.T) {
t.Parallel()
if _, err := New(Spec{Kind: "float"}); err == nil {
t.Fatal("New accepted an unknown checker kind")
}
}