Files
hi-server/tests/acceptance/reporter.go
T

65 lines
1.4 KiB
Go

package acceptance
import (
"encoding/json"
"os"
"sync"
"testing"
"time"
)
type Reporter struct {
mu sync.Mutex
started time.Time
cases []ReportCase
}
type ReportCase struct {
Name string `json:"name"`
Domain string `json:"domain"`
Method string `json:"method"`
Path string `json:"path"`
Expected string `json:"expected"`
}
type Report struct {
RunID string `json:"run_id"`
BaseURL string `json:"base_url"`
Duration string `json:"duration"`
Cases []ReportCase `json:"cases"`
}
var acceptanceReporter = &Reporter{started: time.Now()}
func recordCase(t *testing.T, domain string, method string, path string, expected string) {
t.Helper()
acceptanceReporter.mu.Lock()
defer acceptanceReporter.mu.Unlock()
acceptanceReporter.cases = append(acceptanceReporter.cases, ReportCase{
Name: t.Name(),
Domain: domain,
Method: method,
Path: path,
Expected: expected,
})
}
func writeReport(c Config) error {
if c.ReportPath == "" {
return nil
}
acceptanceReporter.mu.Lock()
defer acceptanceReporter.mu.Unlock()
report := Report{
RunID: c.RunID,
BaseURL: c.StagingURL,
Duration: time.Since(acceptanceReporter.started).String(),
Cases: append([]ReportCase(nil), acceptanceReporter.cases...),
}
out, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
return os.WriteFile(c.ReportPath, out, 0o600)
}