You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

80 lines
2.4 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. package main
  15. import (
  16. "encoding/json"
  17. "os"
  18. "time"
  19. )
  20. // testOutput is a representation of Chromium's JSON test result format. See
  21. // https://www.chromium.org/developers/the-json-test-results-format
  22. type testOutput struct {
  23. Version int `json:"version"`
  24. Interrupted bool `json:"interrupted"`
  25. PathDelimiter string `json:"path_delimiter"`
  26. SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
  27. NumFailuresByType map[string]int `json:"num_failures_by_type"`
  28. Tests map[string]testResult `json:"tests"`
  29. allPassed bool
  30. }
  31. type testResult struct {
  32. Actual string `json:"actual"`
  33. Expected string `json:"expected"`
  34. IsUnexpected bool `json:"is_unexpected"`
  35. }
  36. func newTestOutput() *testOutput {
  37. return &testOutput{
  38. Version: 3,
  39. PathDelimiter: ".",
  40. SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
  41. NumFailuresByType: make(map[string]int),
  42. Tests: make(map[string]testResult),
  43. allPassed: true,
  44. }
  45. }
  46. func (t *testOutput) addResult(name, result string) {
  47. if _, found := t.Tests[name]; found {
  48. panic(name)
  49. }
  50. t.Tests[name] = testResult{
  51. Actual: result,
  52. Expected: "PASS",
  53. IsUnexpected: result != "PASS",
  54. }
  55. t.NumFailuresByType[result]++
  56. if result != "PASS" {
  57. t.allPassed = false
  58. }
  59. }
  60. func (t *testOutput) writeTo(name string) error {
  61. file, err := os.Create(name)
  62. if err != nil {
  63. return err
  64. }
  65. defer file.Close()
  66. out, err := json.MarshalIndent(t, "", " ")
  67. if err != nil {
  68. return err
  69. }
  70. _, err = file.Write(out)
  71. return err
  72. }