results.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests/metrics"
  4. import (
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "path"
  10. "time"
  11. )
  12. type results struct {
  13. resultsFile *os.File
  14. }
  15. type result struct {
  16. testName string
  17. testResult string
  18. numDiffs int
  19. }
  20. func (r *results) Init(resultsDir string) {
  21. err := os.MkdirAll(resultsDir, os.FileMode(0755))
  22. if err != nil {
  23. log.Fatalf(err.Error())
  24. }
  25. r.resultsFile, err = os.Create(path.Join(resultsDir, "TESTRESULTS.md"))
  26. if err != nil {
  27. log.Fatalf(err.Error())
  28. }
  29. r.writeString(
  30. "# Test Results\n" +
  31. fmt.Sprintf("Started: %s\n\n", time.Now().Format(time.RFC1123Z)) +
  32. "Test |Result|Num Diffs|\n" +
  33. "----------------------------------------|------|--------:|\n",
  34. )
  35. }
  36. func (r *results) Add(_ string, rslt any) {
  37. tr := rslt.(result)
  38. line := fmt.Sprintf(
  39. "%-40s|%-6s|%9d|\n",
  40. tr.testName,
  41. tr.testResult,
  42. tr.numDiffs,
  43. )
  44. r.writeString(line)
  45. }
  46. func (r *results) writeString(s string) {
  47. _, _ = io.WriteString(r.resultsFile, s)
  48. }
  49. func (r *results) Save() {
  50. err := r.resultsFile.Close()
  51. if err != nil {
  52. panic(err)
  53. }
  54. }