-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathstructured.go
83 lines (65 loc) · 2.22 KB
/
structured.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package outputs
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/util"
)
// Structured is a output formatter that logs into a StructuredOutput structure
type Structured struct{}
// StructuredTestResult is an individual test result with additional human friendly summary
type StructuredTestResult struct {
resource.TestResult
SummaryLine string `json:"summary-line"`
}
// StructureTestSummary holds summary information about a test run
type StructureTestSummary struct {
TestCount int `json:"test-count"`
Failed int `json:"failed-count"`
TotalDuration time.Duration `json:"total-duration"`
}
// StructuredOutput is the full output structure for the structured output format
type StructuredOutput struct {
Results []StructuredTestResult `json:"results"`
Summary StructureTestSummary `json:"summary"`
SummaryLine string `json:"summary-line"`
}
// String represents human friendly representation of the test summary
func (s *StructureTestSummary) String() string {
return fmt.Sprintf("Count: %d, Failed: %d, Duration: %.3fs", s.TestCount, s.Failed, s.TotalDuration.Seconds())
}
// Output processes output from tests into StructuredOutput written to w as a string
func (r Structured) Output(w io.Writer, results <-chan []resource.TestResult, startTime time.Time, outConfig util.OutputConfig) (exitCode int) {
result := &StructuredOutput{
Results: []StructuredTestResult{},
Summary: StructureTestSummary{},
}
for resultGroup := range results {
for _, testResult := range resultGroup {
r := StructuredTestResult{
TestResult: testResult,
SummaryLine: humanizeResult(testResult),
}
if !testResult.Successful {
result.Summary.Failed++
}
result.Summary.TestCount++
result.Results = append(result.Results, r)
}
}
result.Summary.TotalDuration = time.Since(startTime)
result.SummaryLine = result.Summary.String()
var j []byte
if util.IsValueInList("pretty", outConfig.FormatOptions) {
j, _ = json.MarshalIndent(result, "", " ")
} else {
j, _ = json.Marshal(result)
}
fmt.Fprintln(w, string(j))
return 0
}
func init() {
RegisterOutputer("structured", &Structured{}, []string{})
}