package checks import ( "bytes" "crypto/sha256" "encoding/hex" "encoding/json" "hash" "io" "strings" "amadan.net/oficina/equipo/internal/runner/provider" ) const logLimit = 64 * 1024 type boundedLog struct { bytes.Buffer hash hash.Hash } func (l *boundedLog) Write(p []byte) (int, error) { if l.hash == nil { l.hash = sha256.New() } _, _ = l.hash.Write(p) n := len(p) if left := logLimit - l.Len(); left > 0 { _, _ = l.Buffer.Write(p[:min(left, n)]) } return n, nil } func (l *boundedLog) digest() string { if l.hash == nil { return Digest(nil) } return hex.EncodeToString(l.hash.Sum(nil)) } type testState struct { done, failed, matched bool tail string } type goResults struct { pending []byte log boundedLog bad bool packages map[string]string noTests map[string]bool testEvents map[string]bool tests map[TestIdentity]*testState names []TestIdentity run, failed int symptom string matched bool } func newGoResults() *goResults { return &goResults{packages: map[string]string{}, noTests: map[string]bool{}, testEvents: map[string]bool{}, tests: map[TestIdentity]*testState{}} } func (g *goResults) Write(p []byte) (int, error) { n := len(p) for len(p) > 0 { i := bytes.IndexByte(p, '\n') if i < 0 { i = len(p) - 1 } chunk := p[:i+1] p = p[i+1:] if len(g.pending)+len(chunk) > 1024*1024 { g.bad = true g.pending = nil } else { g.pending = append(g.pending, chunk...) if chunk[len(chunk)-1] == '\n' { g.event(g.pending) g.pending = nil } } } // Parsing always precedes bounded retention. // The caller's full chunk is hashed separately by the command collector. return n, nil } func (g *goResults) event(line []byte) { defer g.log.Write(line) var e struct{ Action, Package, Test, Output string } if json.Unmarshal(line, &e) != nil || e.Package == "" { g.bad = true return } id := TestIdentity{Package: e.Package, Name: e.Test} if e.Test == "" { switch e.Action { case "start": if _, ok := g.packages[e.Package]; ok { g.bad = true } g.packages[e.Package] = "running" case "pass", "fail": if g.packages[e.Package] != "running" { g.bad = true } g.packages[e.Package] = e.Action case "output": fields := strings.Fields(e.Output) if len(fields) == 5 && fields[0] == "?" && fields[1] == e.Package && strings.Join(fields[2:], " ") == "[no test files]" && g.packages[e.Package] == "running" { g.noTests[e.Package] = true } case "skip": if g.packages[e.Package] != "running" || !g.noTests[e.Package] { g.bad = true } if g.testEvents[e.Package] { g.bad = true } g.packages[e.Package] = "no-tests" default: g.bad = true } return } g.testEvents[e.Package] = true switch e.Action { case "run": if g.packages[e.Package] != "running" || g.tests[id] != nil { g.bad = true return } g.tests[id] = &testState{} g.run++ g.names = append(g.names, id) case "pass", "fail": s := g.tests[id] if s == nil || s.done { g.bad = true return } s.done = true s.failed = e.Action == "fail" if s.failed { g.failed++ g.matched = g.matched || s.matched } case "output": if s := g.tests[id]; s != nil && g.symptom != "" { text := s.tail + e.Output s.matched = s.matched || strings.Contains(text, g.symptom) if len(text) > len(g.symptom) { text = text[len(text)-len(g.symptom):] } s.tail = text } case "pause", "cont": if g.tests[id] == nil { g.bad = true } default: g.bad = true } } func (g *goResults) finish(exit int, err error) error { if err != nil { return err } if g.bad || len(g.pending) != 0 || g.run == 0 || len(g.packages) == 0 || exit < 0 { return provider.ErrContract } for _, s := range g.tests { if !s.done { return provider.ErrContract } } for p, status := range g.packages { if status == "no-tests" { continue } if status != "pass" && status != "fail" { return provider.ErrContract } found := false failed := false for id, s := range g.tests { if id.Package == p { found = true failed = failed || s.failed } } if !found || status == "fail" && !failed || status == "pass" && failed { return provider.ErrContract } } if exit == 0 && g.failed > 0 || exit != 0 && g.failed == 0 { return provider.ErrContract } return nil } var _ io.Writer = (*goResults)(nil)