Run and lint solutions on the site
Test and publish the site / test (push) Successful in 1m23s
Test and publish the site / publish (push) Failing after 4m22s

A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.

Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.

C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.

CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 11:24:22 +01:00
co-authored by Claude Opus 5.5
parent 107ecab9da
commit c972fa9fd9
33 changed files with 4477 additions and 23 deletions
+200
View File
@@ -0,0 +1,200 @@
package runner
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
)
func has(ds []Diagnostic, source, rule string, line int) bool {
for _, d := range ds {
if d.Source == source && d.Rule == rule && d.Line == line {
return true
}
}
return false
}
func TestLintCpp(t *testing.T) {
need(t, "g++")
t.Parallel()
src := "#include <vector>\nint main() {\n\tint unused;\n\tstd::vector<int> v;\n\tfor (int i = 0; i < v.size(); i++) {}\n}\n"
resp, err := lint(context.Background(), LintRequest{Lang: Cpp, Source: src})
if err != nil {
t.Fatal(err)
}
if !has(resp.Diagnostics, "g++", "unused-variable", 3) || !has(resp.Diagnostics, "g++", "sign-compare", 5) {
t.Fatalf("%+v", resp.Diagnostics)
}
for _, d := range resp.Diagnostics {
// byte columns: the tab is one column, so "unused" starts at 6
if d.Rule == "unused-variable" && d.Col != 6 {
t.Errorf("column %d", d.Col)
}
}
}
func TestLintClangTidy(t *testing.T) {
need(t, "g++", "clang-tidy")
t.Parallel()
src := "#include <vector>\nint f(int a, int b, int c) { return a == b == c; }\nint main() { return f(1, 2, 3); }\n"
resp, err := lint(context.Background(), LintRequest{Lang: Cpp, Source: src})
if err != nil {
t.Fatal(err)
}
if !has(resp.Diagnostics, "clang-tidy", "bugprone-chained-comparison", 2) {
t.Errorf("%+v", resp.Diagnostics)
}
}
// The template must lint clean, or every new problem starts with warnings
// on lines its author never wrote.
func TestLintTemplateClean(t *testing.T) {
need(t, "g++")
t.Parallel()
root := filepath.Join("..", "..", "..")
tmpl, err := os.ReadFile(filepath.Join(root, "template.cpp"))
if err != nil {
t.Skip("not in the full repo: ", err) // CI exports site/ only
}
dbg, err := os.ReadFile(filepath.Join(root, "debugging.h"))
if err != nil {
t.Skip(err)
}
resp, err := lint(context.Background(), LintRequest{Lang: Cpp, Source: string(tmpl), Files: map[string]string{"debugging.h": string(dbg)}})
if err != nil {
t.Fatal(err)
}
for _, d := range resp.Diagnostics {
// The template's empty loop leaves t unused-but-set; that is the
// author's to fill in, and only g++ may say so.
if d.Source != "g++" {
t.Errorf("template.cpp:%d:%d: %s [%s %s]", d.Line, d.Col, d.Message, d.Source, d.Rule)
}
}
}
func TestClangTidyConfigMatchesRepo(t *testing.T) {
repo, err := os.ReadFile(filepath.Join("..", "..", "..", ".clang-tidy"))
if err != nil {
t.Skip("not in the full repo: ", err)
}
if !bytes.Equal(repo, []byte(tidyConfig)) {
t.Error(".clang-tidy and internal/runner/clang-tidy.yaml differ: keep them identical")
}
}
func TestParseGCC(t *testing.T) {
out := `main.cpp: In function 'int main()':
main.cpp:3:7: warning: unused variable 'x' [-Wunused-variable]
main.cpp:4:3: error: 'undeclared' was not declared in this scope
main.cpp:4:3: note: suggested alternative: 'unsigned'
In file included from /usr/include/c++/14/algorithm:61,
from main.cpp:1:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::sort(_RAIter, _RAIter) [with _RAIter = int]':
main.cpp:8:12: required from here
/usr/include/c++/14/bits/stl_algo.h:1817:9: error: no type named 'difference_type'
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__heap_select(...)':
/usr/include/c++/14/bits/stl_algo.h:4817:18: required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = int]'
main.cpp:8:12: required from here
/usr/include/c++/14/bits/stl_algo.h:1595:20: error: invalid type argument of unary '*'
debugging.h:2:1: warning: something in a header [-Wextra]
`
ds := parseGCC(out)
want := []Diagnostic{
{Line: 3, Col: 7, Severity: "warning", Message: "unused variable 'x'", Source: "g++", Rule: "unused-variable"},
{Line: 4, Col: 3, Severity: "error", Message: "'undeclared' was not declared in this scope", Source: "g++"},
{Line: 8, Col: 12, Severity: "error", Message: "no type named 'difference_type' (in stl_algo.h)", Source: "g++"},
}
if len(ds) != len(want) {
t.Fatalf("got %+v", ds)
}
for i := range want {
if ds[i] != want[i] {
t.Errorf("%d: got %+v, want %+v", i, ds[i], want[i])
}
}
}
func TestParseTidy(t *testing.T) {
out := `/tmp/w/main.cpp:2:37: warning: chained comparison 'v0 == v1 == v2' may generate unintended results [bugprone-chained-comparison]
2 | int f(int a, int b, int c) { return a == b == c; }
| ^
/tmp/w/main.cpp:5:1: error: use of undeclared identifier 'x' [clang-diagnostic-error]
/tmp/w/driver.h:1:1: warning: in a header [bugprone-foo]
/tmp/w/main.cpp:6:1: note: expanded from macro 'F'
`
ds := parseTidy(out, map[string]bool{"/tmp/w/main.cpp": true})
if len(ds) != 1 || ds[0].Rule != "bugprone-chained-comparison" || ds[0].Line != 2 || ds[0].Col != 37 {
t.Errorf("%+v", ds)
}
}
func TestLintPython(t *testing.T) {
need(t, "python3")
t.Parallel()
resp, err := lint(context.Background(), LintRequest{Lang: Python, Source: "def f(:\n pass\n"})
if err != nil {
t.Fatal(err)
}
ds := resp.Diagnostics
if len(ds) != 1 || ds[0].Source != "python" || ds[0].Line != 1 || ds[0].Severity != "error" {
t.Errorf("%+v", ds)
}
}
func TestLintRuff(t *testing.T) {
need(t, "python3", "ruff")
t.Parallel()
resp, err := lint(context.Background(), LintRequest{Lang: Python, Source: "import os\nprint(y)\n"})
if err != nil {
t.Fatal(err)
}
if !has(resp.Diagnostics, "ruff", "F401", 1) || !has(resp.Diagnostics, "ruff", "F821", 2) {
t.Errorf("%+v", resp.Diagnostics)
}
}
// A trimmed sample of ruff 0.11's --output-format=json, from Alpine 3.22.
const ruffSample = `[
{"cell": null, "code": "F401", "end_location": {"column": 10, "row": 1}, "filename": "/tmp/main.py",
"fix": {"applicability": "safe", "edits": [], "message": "Remove unused import: ` + "`os`" + `"},
"location": {"column": 8, "row": 1}, "message": "` + "`os`" + ` imported but unused", "noqa_row": 1,
"url": "https://docs.astral.sh/ruff/rules/unused-import"},
{"cell": null, "code": "F821", "end_location": {"column": 13, "row": 3}, "filename": "/tmp/main.py", "fix": null,
"location": {"column": 12, "row": 3}, "message": "Undefined name ` + "`y`" + `", "noqa_row": 3, "url": null},
{"cell": null, "code": null, "end_location": {"column": 8, "row": 5}, "filename": "/tmp/main.py", "fix": null,
"location": {"column": 7, "row": 5}, "message": "SyntaxError: Expected a parameter", "noqa_row": null, "url": null}
]`
func TestParseRuff(t *testing.T) {
ds, err := parseRuff(ruffSample)
if err != nil {
t.Fatal(err)
}
want := []Diagnostic{
{Line: 1, Col: 8, EndLine: 1, EndCol: 10, Severity: "warning", Message: "`os` imported but unused", Source: "ruff", Rule: "F401"},
{Line: 3, Col: 12, EndLine: 3, EndCol: 13, Severity: "error", Message: "Undefined name `y`", Source: "ruff", Rule: "F821"},
{Line: 5, Col: 7, EndLine: 5, EndCol: 8, Severity: "error", Message: "SyntaxError: Expected a parameter", Source: "ruff"},
}
if len(ds) != len(want) {
t.Fatalf("got %+v", ds)
}
for i := range want {
if ds[i] != want[i] {
t.Errorf("%d: got %+v, want %+v", i, ds[i], want[i])
}
}
}
func TestParsePySyntax(t *testing.T) {
ds := parsePySyntax(`{"line": 1, "col": 7, "endLine": 1, "endCol": 8, "msg": "invalid syntax", "kind": "SyntaxError"}` + "\n")
if len(ds) != 1 || ds[0].Col != 7 || ds[0].EndCol != 8 || ds[0].Rule != "SyntaxError" {
t.Errorf("%+v", ds)
}
if parsePySyntax("") != nil {
t.Error("no output is no diagnostic")
}
}