新功能(#5): 新增 acceptance 测试脚手架 (#8)

This commit is contained in:
2026-06-03 20:07:06 -07:00
committed by GitHub
parent 53fb541846
commit 5e4cc33ff6
10 changed files with 717 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
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)
}