run-go-check.cjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Bounded JSON test reporting: avoid flooding review logs with successful test output.
  2. const { spawn } = require('node:child_process')
  3. const [binary, ...args] = process.argv.slice(2)
  4. if (!binary) throw new Error('go executable is required')
  5. const child = spawn(binary, args, { stdio: ['ignore', 'pipe', 'pipe'] })
  6. let pending = ''
  7. const summary = { packages: { passed: [], failed: [] }, tests: { passed: 0, failed: 0, skipped: 0 }, failures: [] }
  8. const output = new Map()
  9. function line(raw) {
  10. let event
  11. try { event = JSON.parse(raw) } catch { if (raw.trim()) process.stderr.write(raw + '\n'); return }
  12. const key = `${event.Package}/${event.Test || ''}`
  13. if (event.Output && event.Test) output.set(key, ((output.get(key) || '') + event.Output).slice(-5000))
  14. if (['pass', 'fail', 'skip'].includes(event.Action)) {
  15. if (event.Test) {
  16. summary.tests[{pass:'passed', fail:'failed', skip:'skipped'}[event.Action]]++
  17. if (event.Action === 'fail') summary.failures.push({ test: key, output: output.get(key) || '' })
  18. output.delete(key)
  19. } else if (event.Action === 'pass') summary.packages.passed.push(event.Package)
  20. else if (event.Action === 'fail') summary.packages.failed.push(event.Package)
  21. }
  22. }
  23. child.stdout.on('data', data => {
  24. pending += data.toString()
  25. let index
  26. while ((index = pending.indexOf('\n')) >= 0) { line(pending.slice(0, index)); pending = pending.slice(index + 1) }
  27. })
  28. child.stderr.on('data', data => process.stderr.write(data))
  29. child.on('error', error => { process.stderr.write(error.message); process.exitCode = 1 })
  30. child.on('close', code => {
  31. if (pending) line(pending)
  32. process.stdout.write(JSON.stringify(summary, null, 2) + '\n')
  33. process.exitCode = code || 0
  34. })