-
Notifications
You must be signed in to change notification settings - Fork 23
/
report.go
225 lines (198 loc) · 5.46 KB
/
report.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package report
import (
"flag"
"fmt"
"io/ioutil"
"regexp"
"github.com/fatih/color"
"github.com/padok-team/yatas/plugins/commons"
"github.com/padok-team/yatas/plugins/logger"
"gopkg.in/yaml.v3"
)
var status = map[string]string{
"OK": "✅",
"WARN": "⚠️",
"FAIL": "❌",
}
var (
details = flag.Bool("details", false, "print detailed results")
resume = flag.Bool("resume", false, "print resume results")
timeTaken = flag.Bool("time", false, "print time taken for each check")
onlyFailure = flag.Bool("only-failure", false, "print only failed checks")
)
// countResultOkOverall counts the number of OK and total results.
func countResultOkOverall(results []commons.Result) (int, int) {
var ok int
var all int
for _, result := range results {
if result.Status == "OK" {
ok++
}
all++
}
return ok, all
}
// IsIgnored checks if a result is ignored based on the configuration.
func IsIgnored(c *commons.Config, r commons.Result, check commons.Check) bool {
for _, ignored := range c.Ignore {
if ignored.ID == check.Id {
for i := range ignored.Values {
if ignored.Regex && regexp.MustCompile(ignored.Values[i]).MatchString(r.Message) {
return true
} else if !ignored.Regex && r.Message == ignored.Values[i] {
return true
}
}
}
}
return false
}
// RemoveIgnored removes ignored checks from the given tests based on the configuration.
func RemoveIgnored(c *commons.Config, tests []commons.Tests) []commons.Tests {
resultsTmp := []commons.Tests{}
for _, test := range tests {
var testTpm commons.Tests
testTpm.Account = test.Account
testTpm.Checks = []commons.Check{}
for _, check := range test.Checks {
checkTmp := check
checkTmp.Results = []commons.Result{}
checkTmp.InitCheck(check.Name, check.Description, check.Id, check.Categories)
for _, result := range check.Results {
if !IsIgnored(c, result, check) {
checkTmp.AddResult(result)
}
}
testTpm.Checks = append(testTpm.Checks, checkTmp)
}
resultsTmp = append(resultsTmp, testTpm)
}
return resultsTmp
}
// CountChecksPassedOverall counts the number of passed and total checks.
func CountChecksPassedOverall(checks []commons.Check) (int, int) {
var ok int
var all int
for _, check := range checks {
if check.Status == "OK" {
ok++
}
all++
}
return ok, all
}
// PrettyPrintChecks prints the checks in a human-readable format.
func PrettyPrintChecks(checks []commons.Tests, c *commons.Config) {
flag.Parse()
for _, tests := range checks {
ok, all := CountChecksPassedOverall(tests.Checks)
fmt.Printf("\nName: %s (%d/%d)\n", tests.Account, ok, all)
if !*resume {
for _, check := range tests.Checks {
if c.CheckExclude(check.Id) {
continue
}
ok, all := countResultOkOverall(check.Results)
count := fmt.Sprintf("%d/%d", ok, all)
duration := fmt.Sprintf("%.2fs", check.Duration.Seconds())
if *onlyFailure && check.Status == "OK" {
continue
}
if *timeTaken {
fmt.Println(status[check.Status], check.Id, check.Name, "-", duration, "-", count)
} else {
fmt.Println(status[check.Status], check.Id, check.Name, "-", count)
}
if *details {
for _, result := range check.Results {
if *onlyFailure && result.Status == "OK" {
continue
}
if result.Status == "FAIL" {
color.Red("\t" + result.Message)
} else {
color.Green("\t" + result.Message)
}
}
}
}
}
}
}
// ComparePreviousWithNew compares the previous test results with the new ones and returns the difference.
func ComparePreviousWithNew(previous []commons.Tests, new []commons.Tests) []commons.Tests {
returnedResults := []commons.Tests{}
for _, tests := range new {
var checks []commons.Check
for _, check := range tests.Checks {
found := false
for _, previousTests := range previous {
for _, previousCheck := range previousTests.Checks {
if check.Id == previousCheck.Id && tests.Account == previousTests.Account {
if check.Status != previousCheck.Status {
checks = append(checks, check)
} else {
found = true
}
}
}
}
if !found {
checks = append(checks, check)
}
}
test := tests
test.Checks = checks
returnedResults = append(returnedResults, test)
}
return returnedResults
}
// ReadPreviousResults reads the previous test results from the results.yaml file.
func ReadPreviousResults() []commons.Tests {
d, err := ioutil.ReadFile("results.yaml")
if err != nil {
return []commons.Tests{}
}
var checks []commons.Tests
err = yaml.Unmarshal(d, &checks)
if err != nil {
logger.Error(err.Error())
return nil
}
return checks
}
// WriteChecksToFile writes the test results to the results.yaml file.
func WriteChecksToFile(checks []commons.Tests, c *commons.Config) {
for _, tests := range checks {
var checksToWrite []commons.Check
for _, check := range tests.Checks {
if !c.CheckExclude(check.Id) {
checksToWrite = append(checksToWrite, check)
}
}
tests.Checks = checksToWrite
}
d, err := yaml.Marshal(checks)
if err != nil {
logger.Error(err.Error())
return
}
// Write to results.yaml
err = ioutil.WriteFile("results.yaml", d, 0644)
if err != nil {
logger.Error(err.Error())
return
}
}
// ExitCode returns the exit code for the CI based on the test results.
func ExitCode(checks []commons.Tests) int {
var exitCode int
for _, tests := range checks {
for _, check := range tests.Checks {
if check.Status == "FAIL" {
exitCode = 1
}
}
}
return exitCode
}