init:
This commit is contained in:
119
runner/expander.go
Normal file
119
runner/expander.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Mond1c/judge/dsl"
|
||||
)
|
||||
|
||||
func expandPattern(pattern *dsl.Pattern, groupTimeout interface{ IsZero() bool }) ([]*dsl.Test, error) {
|
||||
if pattern.IsDirMode() {
|
||||
return expandDirPattern(pattern)
|
||||
}
|
||||
return expandGlobPattern(pattern)
|
||||
}
|
||||
|
||||
func expandGlobPattern(pattern *dsl.Pattern) ([]*dsl.Test, error) {
|
||||
inputFiles, err := filepath.Glob(pattern.InputGlob)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid input glob %q: %w", pattern.InputGlob, err)
|
||||
}
|
||||
if len(inputFiles) == 0 {
|
||||
return nil, fmt.Errorf("no files matched input glob %q", pattern.InputGlob)
|
||||
}
|
||||
|
||||
inputPrefix, inputSuffix := splitGlob(pattern.InputGlob)
|
||||
outputPrefix, outputSuffix := splitGlob(pattern.OutputGlob)
|
||||
|
||||
var tests []*dsl.Test
|
||||
for _, inputPath := range inputFiles {
|
||||
wildcard := extractWildcard(inputPath, inputPrefix, inputSuffix)
|
||||
outputPath := outputPrefix + wildcard + outputSuffix
|
||||
|
||||
inputContent, err := os.ReadFile(inputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read input %q: %w", inputPath, err)
|
||||
}
|
||||
outputContent, err := os.ReadFile(outputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read output %q: %w", outputPath, err)
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("pattern:%s", wildcard)
|
||||
stdin := string(inputContent)
|
||||
expected := string(outputContent)
|
||||
|
||||
tests = append(tests, &dsl.Test{
|
||||
Name: name,
|
||||
Stdin: &stdin,
|
||||
Env: map[string]string{},
|
||||
InFiles: map[string]string{},
|
||||
OutFiles: map[string]string{},
|
||||
Stdout: dsl.ExactMatcher{Value: expected},
|
||||
Stderr: dsl.NoMatcher{},
|
||||
})
|
||||
}
|
||||
return tests, nil
|
||||
}
|
||||
|
||||
func expandDirPattern(pattern *dsl.Pattern) ([]*dsl.Test, error) {
|
||||
dirs, err := filepath.Glob(pattern.DirsGlob)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid dirs glob %q: %w", pattern.DirsGlob, err)
|
||||
}
|
||||
if len(dirs) == 0 {
|
||||
return nil, fmt.Errorf("no directories matched %q", pattern.DirsGlob)
|
||||
}
|
||||
|
||||
var tests []*dsl.Test
|
||||
for _, dir := range dirs {
|
||||
info, err := os.Stat(dir)
|
||||
if err != nil || !info.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
inputPath := filepath.Join(dir, pattern.InputFile)
|
||||
outputPath := filepath.Join(dir, pattern.OutputFile)
|
||||
|
||||
inputContent, err := os.ReadFile(inputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %q: %w", inputPath, err)
|
||||
}
|
||||
outputContent, err := os.ReadFile(outputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %q: %w", outputPath, err)
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("pattern:%s", filepath.Base(dir))
|
||||
stdin := string(inputContent)
|
||||
expected := string(outputContent)
|
||||
|
||||
tests = append(tests, &dsl.Test{
|
||||
Name: name,
|
||||
Stdin: &stdin,
|
||||
Env: map[string]string{},
|
||||
InFiles: map[string]string{},
|
||||
OutFiles: map[string]string{},
|
||||
Stdout: dsl.ExactMatcher{Value: expected},
|
||||
Stderr: dsl.NoMatcher{},
|
||||
})
|
||||
}
|
||||
return tests, nil
|
||||
}
|
||||
|
||||
func splitGlob(pattern string) (prefix, suffix string) {
|
||||
idx := strings.Index(pattern, "*")
|
||||
if idx < 0 {
|
||||
return pattern, ""
|
||||
}
|
||||
return pattern[:idx], pattern[idx+1:]
|
||||
}
|
||||
|
||||
func extractWildcard(path, prefix, suffix string) string {
|
||||
s := strings.TrimPrefix(path, prefix)
|
||||
s = strings.TrimSuffix(s, suffix)
|
||||
return s
|
||||
}
|
||||
134
runner/matcher.go
Normal file
134
runner/matcher.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Mond1c/judge/dsl"
|
||||
)
|
||||
|
||||
func applyMatcher(label string, m dsl.Matcher, actual string) []string {
|
||||
switch m := m.(type) {
|
||||
case dsl.NoMatcher:
|
||||
return nil
|
||||
case dsl.ExactMatcher:
|
||||
if actual != m.Value {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s mismatch:\n expected: %q\n actual: %q",
|
||||
label, m.Value, actual,
|
||||
)}
|
||||
}
|
||||
return nil
|
||||
case dsl.ContainsMatcher:
|
||||
if !strings.Contains(actual, m.Substr) {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s: expected to contain %q, got %q",
|
||||
label, m.Substr, actual,
|
||||
)}
|
||||
}
|
||||
return nil
|
||||
case dsl.RegexMatcher:
|
||||
re, err := regexp.Compile(m.Pattern)
|
||||
if err != nil {
|
||||
return []string{fmt.Sprintf("%s: invalid regex %q: %v", label, m.Pattern, err)}
|
||||
}
|
||||
if !re.MatchString(actual) {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s: %q does not match regex %q",
|
||||
label, actual, m.Pattern,
|
||||
)}
|
||||
}
|
||||
return nil
|
||||
|
||||
case dsl.NumericEpsMatcher:
|
||||
errs := matchNumericEps(label, m, actual)
|
||||
return errs
|
||||
|
||||
case dsl.AnyOrderMatcher:
|
||||
return matchAnyOrder(label, m, actual)
|
||||
|
||||
default:
|
||||
return []string{fmt.Sprintf("unknown matcher type %T", m)}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func matchNumericEps(label string, m dsl.NumericEpsMatcher, actual string) []string {
|
||||
expectedNums, err := parseNumbers(m.Value)
|
||||
if err != nil {
|
||||
return []string{fmt.Sprintf("%s: cannot parse expected numbers %q: %v", label, m.Value, err)}
|
||||
}
|
||||
actualNums, err := parseNumbers(actual)
|
||||
if err != nil {
|
||||
return []string{fmt.Sprintf("%s: cannot parse actual numbers %q: %v", label, actual, err)}
|
||||
}
|
||||
if len(expectedNums) != len(actualNums) {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s: expected %d numbers, got %d (expected=%q, actual=%q)",
|
||||
label, len(expectedNums), len(actualNums), m.Value, actual,
|
||||
)}
|
||||
}
|
||||
var errs []string
|
||||
for i, exp := range expectedNums {
|
||||
act := actualNums[i]
|
||||
if math.Abs(exp-act) > m.Epsilon {
|
||||
errs = append(errs, fmt.Sprintf(
|
||||
"%s: number[%d] expected %.10g ± %.10g, got %.10g",
|
||||
label, i, exp, m.Epsilon, act,
|
||||
))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func parseNumbers(s string) ([]float64, error) {
|
||||
fields := strings.Fields(s)
|
||||
nums := make([]float64, 0, len(fields))
|
||||
for _, f := range fields {
|
||||
n, err := strconv.ParseFloat(f, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("not a number: %q", f)
|
||||
}
|
||||
nums = append(nums, n)
|
||||
}
|
||||
return nums, nil
|
||||
}
|
||||
|
||||
func matchAnyOrder(label string, m dsl.AnyOrderMatcher, actual string) []string {
|
||||
actualLines := splitLines(actual)
|
||||
expectedLines := make([]string, len(m.Lines))
|
||||
copy(expectedLines, m.Lines)
|
||||
|
||||
sort.Strings(actualLines)
|
||||
sort.Strings(expectedLines)
|
||||
|
||||
if len(actualLines) != len(expectedLines) {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s anyOrder: expected %d lines, got %d",
|
||||
label, len(expectedLines), len(actualLines),
|
||||
)}
|
||||
}
|
||||
|
||||
var errs []string
|
||||
for i := range expectedLines {
|
||||
if actualLines[i] != expectedLines[i] {
|
||||
errs = append(errs, fmt.Sprintf(
|
||||
"%s anyOrder: line mismatch: expected %q, got %q",
|
||||
label, expectedLines[i], actualLines[i],
|
||||
))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func splitLines(s string) []string {
|
||||
s = strings.TrimRight(s, "\n")
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
return strings.Split(s, "\n")
|
||||
}
|
||||
66
runner/result.go
Normal file
66
runner/result.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
StatusPass Status = iota
|
||||
StatusFail
|
||||
StatusTLE
|
||||
StatusBuildError
|
||||
StatusRuntimeError
|
||||
)
|
||||
|
||||
func (s Status) String() string {
|
||||
switch s {
|
||||
case StatusPass:
|
||||
return "PASS"
|
||||
case StatusFail:
|
||||
return "FAIL"
|
||||
case StatusTLE:
|
||||
return "TLE"
|
||||
case StatusBuildError:
|
||||
return "BUILD_ERROR"
|
||||
case StatusRuntimeError:
|
||||
return "RE"
|
||||
default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
type TestResult struct {
|
||||
Name string
|
||||
Status Status
|
||||
Elapsed time.Duration
|
||||
|
||||
Failures []string
|
||||
|
||||
ActualStdout string
|
||||
ActualStderr string
|
||||
ActualCode int
|
||||
}
|
||||
|
||||
func (r *TestResult) fail(msg string, args ...any) {
|
||||
r.Failures = append(r.Failures, fmt.Sprintf(msg, args...))
|
||||
r.Status = StatusFail
|
||||
}
|
||||
|
||||
type GroupResult struct {
|
||||
Name string
|
||||
Weight float64
|
||||
Score float64
|
||||
|
||||
Tests []*TestResult
|
||||
Passed int
|
||||
Total int
|
||||
}
|
||||
|
||||
type SuiteResult struct {
|
||||
Groups []*GroupResult
|
||||
TotalScore float64
|
||||
BuildLog string
|
||||
}
|
||||
405
runner/runner.go
Normal file
405
runner/runner.go
Normal file
@@ -0,0 +1,405 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Mond1c/judge/dsl"
|
||||
)
|
||||
|
||||
// MaxOutputBytes caps stdout/stderr captured from the solution process.
|
||||
// Prevents runaway student programs from OOM-ing the judge host.
|
||||
const MaxOutputBytes = 16 * 1024 * 1024 // 16 MiB
|
||||
|
||||
type Config struct {
|
||||
WorkDir string
|
||||
BinaryName string
|
||||
Wrapper string // CLI override, wins over DSL wrapper
|
||||
}
|
||||
|
||||
type Runner struct {
|
||||
cfg Config
|
||||
file *dsl.File
|
||||
binary string
|
||||
}
|
||||
|
||||
func New(f *dsl.File, cfg Config) *Runner {
|
||||
name := cfg.BinaryName
|
||||
if name == "" {
|
||||
name = f.Binary
|
||||
}
|
||||
if name == "" {
|
||||
name = "solution"
|
||||
}
|
||||
absWork, err := filepath.Abs(cfg.WorkDir)
|
||||
if err != nil {
|
||||
absWork = cfg.WorkDir
|
||||
}
|
||||
cfg.WorkDir = absWork
|
||||
return &Runner{file: f, cfg: cfg, binary: resolveBinary(absWork, name)}
|
||||
}
|
||||
|
||||
// resolveBinary picks <work>/name, falling back to <work>/name.exe on Windows.
|
||||
func resolveBinary(workDir, name string) string {
|
||||
primary := filepath.Join(workDir, name)
|
||||
if runtime.GOOS == "windows" && !strings.HasSuffix(strings.ToLower(name), ".exe") {
|
||||
if _, err := os.Stat(primary); err != nil {
|
||||
withExe := primary + ".exe"
|
||||
if _, err2 := os.Stat(withExe); err2 == nil {
|
||||
return withExe
|
||||
}
|
||||
}
|
||||
}
|
||||
return primary
|
||||
}
|
||||
|
||||
func (r *Runner) Run() *SuiteResult {
|
||||
result := &SuiteResult{}
|
||||
|
||||
buildLog, err := r.build()
|
||||
result.BuildLog = buildLog
|
||||
if err != nil {
|
||||
for _, g := range r.file.Groups {
|
||||
gr := &GroupResult{
|
||||
Name: g.Name,
|
||||
Weight: g.Weight,
|
||||
Score: 0,
|
||||
}
|
||||
|
||||
total := len(g.Tests)
|
||||
if g.Pattern != nil {
|
||||
total = -1
|
||||
}
|
||||
gr.Total = total
|
||||
for _, t := range g.Tests {
|
||||
gr.Tests = append(gr.Tests, &TestResult{
|
||||
Name: t.Name,
|
||||
Status: StatusBuildError,
|
||||
})
|
||||
}
|
||||
result.Groups = append(result.Groups, gr)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// After build, re-resolve binary (the exe may have been produced just now).
|
||||
r.binary = resolveBinary(r.cfg.WorkDir, filepath.Base(r.binary))
|
||||
|
||||
for _, g := range r.file.Groups {
|
||||
gr := r.runGroup(g)
|
||||
result.Groups = append(result.Groups, gr)
|
||||
result.TotalScore += gr.Score
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// buildCommand picks the most specific build command for this OS.
|
||||
func (r *Runner) buildCommand() string {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
if r.file.BuildWindows != "" {
|
||||
return r.file.BuildWindows
|
||||
}
|
||||
case "linux":
|
||||
if r.file.BuildLinux != "" {
|
||||
return r.file.BuildLinux
|
||||
}
|
||||
case "darwin":
|
||||
if r.file.BuildDarwin != "" {
|
||||
return r.file.BuildDarwin
|
||||
}
|
||||
}
|
||||
if r.file.Build != "" {
|
||||
return r.file.Build
|
||||
}
|
||||
return "go build -o solution ."
|
||||
}
|
||||
|
||||
func (r *Runner) build() (string, error) {
|
||||
buildCmd := r.buildCommand()
|
||||
|
||||
ctx := context.Background()
|
||||
if r.file.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.file.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
cmd := shellCommand(ctx, buildCmd)
|
||||
cmd.Dir = r.cfg.WorkDir
|
||||
cmd.Env = os.Environ()
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return out.String(), fmt.Errorf("build failed: %w\n%s", err, out.String())
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
// shellCommand runs a command string through the platform's shell so env vars
|
||||
// like $CC expand naturally from the CI matrix.
|
||||
func shellCommand(ctx context.Context, cmdline string) *exec.Cmd {
|
||||
if runtime.GOOS == "windows" {
|
||||
return exec.CommandContext(ctx, "cmd", "/C", cmdline)
|
||||
}
|
||||
return exec.CommandContext(ctx, "sh", "-c", cmdline)
|
||||
}
|
||||
|
||||
func (r *Runner) runGroup(g *dsl.Group) *GroupResult {
|
||||
gr := &GroupResult{
|
||||
Name: g.Name,
|
||||
Weight: g.Weight,
|
||||
}
|
||||
|
||||
tests := g.Tests
|
||||
|
||||
if g.Pattern != nil {
|
||||
expanded, err := expandPattern(g.Pattern, &zeroChecker{})
|
||||
if err != nil {
|
||||
gr.Tests = append(gr.Tests, &TestResult{
|
||||
Name: "pattern_expand",
|
||||
Status: StatusFail,
|
||||
Failures: []string{fmt.Sprintf("pattern expand error: %v", err)},
|
||||
})
|
||||
gr.Total = 1
|
||||
gr.Score = 0
|
||||
return gr
|
||||
}
|
||||
for _, t := range expanded {
|
||||
if t.Timeout == 0 {
|
||||
t.Timeout = g.Timeout
|
||||
}
|
||||
for k, v := range g.Env {
|
||||
if _, ok := t.Env[k]; !ok {
|
||||
t.Env[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
tests = append(tests, expanded...)
|
||||
}
|
||||
|
||||
gr.Total = len(tests)
|
||||
|
||||
for _, t := range tests {
|
||||
for k, v := range g.Env {
|
||||
if _, ok := t.Env[k]; !ok {
|
||||
t.Env[k] = v
|
||||
}
|
||||
}
|
||||
if t.Timeout == 0 {
|
||||
t.Timeout = g.Timeout
|
||||
}
|
||||
if t.Wrapper == "" {
|
||||
t.Wrapper = g.Wrapper
|
||||
}
|
||||
|
||||
tr := r.runTest(t)
|
||||
gr.Tests = append(gr.Tests, tr)
|
||||
if tr.Status == StatusPass {
|
||||
gr.Passed++
|
||||
}
|
||||
}
|
||||
|
||||
switch g.Scoring {
|
||||
case dsl.ScoringAllOrNone:
|
||||
if gr.Passed == gr.Total {
|
||||
gr.Score = g.Weight
|
||||
} else {
|
||||
gr.Score = 0
|
||||
}
|
||||
default:
|
||||
if gr.Total > 0 {
|
||||
gr.Score = g.Weight * float64(gr.Passed) / float64(gr.Total)
|
||||
}
|
||||
}
|
||||
|
||||
return gr
|
||||
}
|
||||
|
||||
func (r *Runner) runTest(t *dsl.Test) *TestResult {
|
||||
tr := &TestResult{Name: t.Name, Status: StatusPass}
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "judge-test-*")
|
||||
if err != nil {
|
||||
tr.Status = StatusRuntimeError
|
||||
tr.Failures = append(tr.Failures, fmt.Sprintf("failed to create temp dir: %v", err))
|
||||
return tr
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
for name, content := range t.InFiles {
|
||||
path := filepath.Join(tmpDir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
tr.fail("mkdir for file %q: %v", name, err)
|
||||
return tr
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
tr.fail("write input file %q: %v", name, err)
|
||||
return tr
|
||||
}
|
||||
}
|
||||
|
||||
timeout := t.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
// Wrapper precedence: CLI flag > test > group (already copied into test).
|
||||
wrapper := r.cfg.Wrapper
|
||||
if wrapper == "" {
|
||||
wrapper = t.Wrapper
|
||||
}
|
||||
|
||||
cmd := buildExecCmd(ctx, wrapper, r.binary, t.Args)
|
||||
cmd.Dir = tmpDir
|
||||
|
||||
cmd.Env = os.Environ()
|
||||
// Force C locale so numeric formatting (decimal separator, etc.) is stable
|
||||
// across runners. Student code can still override via test env.
|
||||
cmd.Env = append(cmd.Env, "LC_ALL=C", "LANG=C")
|
||||
for k, v := range t.Env {
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
if t.Stdin != nil {
|
||||
cmd.Stdin = strings.NewReader(*t.Stdin)
|
||||
}
|
||||
|
||||
stdout := &cappedBuffer{limit: MaxOutputBytes}
|
||||
stderr := &cappedBuffer{limit: MaxOutputBytes}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
|
||||
start := time.Now()
|
||||
runErr := cmd.Run()
|
||||
tr.Elapsed = time.Since(start)
|
||||
|
||||
tr.ActualStdout = normalizeOutput(stdout.String(), r.file)
|
||||
tr.ActualStderr = normalizeOutput(stderr.String(), r.file)
|
||||
|
||||
if stdout.truncated || stderr.truncated {
|
||||
tr.fail("output truncated at %d bytes (possible runaway output)", MaxOutputBytes)
|
||||
}
|
||||
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
tr.Status = StatusTLE
|
||||
tr.Failures = append(tr.Failures, fmt.Sprintf("time limit exceeded (%v)", timeout))
|
||||
return tr
|
||||
}
|
||||
|
||||
actualCode := 0
|
||||
if runErr != nil {
|
||||
if exitErr, ok := runErr.(*exec.ExitError); ok {
|
||||
actualCode = exitErr.ExitCode()
|
||||
} else {
|
||||
tr.Status = StatusRuntimeError
|
||||
tr.fail("runtime error: %v", runErr)
|
||||
return tr
|
||||
}
|
||||
}
|
||||
tr.ActualCode = actualCode
|
||||
|
||||
if t.ExitCode != nil && actualCode != *t.ExitCode {
|
||||
tr.fail("exit code: expected %d, got %d", *t.ExitCode, actualCode)
|
||||
}
|
||||
|
||||
for _, f := range applyMatcher("stdout", t.Stdout, tr.ActualStdout) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
for _, f := range applyMatcher("stderr", t.Stderr, tr.ActualStderr) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
|
||||
for name, expected := range t.OutFiles {
|
||||
path := filepath.Join(tmpDir, name)
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
tr.fail("output file %q not found: %v", name, err)
|
||||
continue
|
||||
}
|
||||
actual := normalizeOutput(string(content), r.file)
|
||||
for _, f := range applyMatcher(fmt.Sprintf("file(%s)", name), dsl.ExactMatcher{Value: expected}, actual) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
}
|
||||
|
||||
if len(tr.Failures) > 0 {
|
||||
tr.Status = StatusFail
|
||||
}
|
||||
|
||||
return tr
|
||||
}
|
||||
|
||||
// buildExecCmd creates an exec.Cmd for the solution, optionally prefixed with
|
||||
// a wrapper (gdb, valgrind, qemu, ...). Wrapper is a shell string so users can
|
||||
// pass flags like "valgrind --error-exitcode=99 --leak-check=full".
|
||||
func buildExecCmd(ctx context.Context, wrapper, binary string, args []string) *exec.Cmd {
|
||||
if wrapper == "" {
|
||||
return exec.CommandContext(ctx, binary, args...)
|
||||
}
|
||||
wrapperFields := strings.Fields(wrapper)
|
||||
full := append(wrapperFields, binary)
|
||||
full = append(full, args...)
|
||||
return exec.CommandContext(ctx, full[0], full[1:]...)
|
||||
}
|
||||
|
||||
// normalizeOutput applies the file-level CRLF / trailing-WS rules.
|
||||
func normalizeOutput(s string, f *dsl.File) string {
|
||||
if f.NormalizeCRLF {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.ReplaceAll(s, "\r", "\n")
|
||||
}
|
||||
if f.TrimTrailingWS {
|
||||
lines := strings.Split(s, "\n")
|
||||
for i := range lines {
|
||||
lines[i] = strings.TrimRight(lines[i], " \t\r")
|
||||
}
|
||||
s = strings.Join(lines, "\n")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// cappedBuffer stops writing once limit bytes are captured, but keeps draining
|
||||
// the source so the child process doesn't block on a full pipe.
|
||||
type cappedBuffer struct {
|
||||
buf bytes.Buffer
|
||||
limit int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
||||
room := c.limit - c.buf.Len()
|
||||
if room <= 0 {
|
||||
c.truncated = true
|
||||
return len(p), nil // pretend we wrote, to keep the pipe flowing
|
||||
}
|
||||
if len(p) > room {
|
||||
c.buf.Write(p[:room])
|
||||
c.truncated = true
|
||||
return len(p), nil
|
||||
}
|
||||
return c.buf.Write(p)
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) String() string { return c.buf.String() }
|
||||
|
||||
var _ io.Writer = (*cappedBuffer)(nil)
|
||||
|
||||
type zeroChecker struct{}
|
||||
|
||||
func (z *zeroChecker) IsZero() bool { return true }
|
||||
Reference in New Issue
Block a user