forked from GoogleContainerTools/container-structure-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
101 lines (89 loc) · 3.14 KB
/
output.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
// Copyright 2018 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"bytes"
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/pkg/errors"
types "github.com/GoogleContainerTools/container-structure-test/pkg/types/unversioned"
)
var bannerLength = 27 // default banner length
func OutputResult(out io.Writer, result *types.TestResult) error {
var strBuffer bytes.Buffer
strBuffer.WriteString(fmt.Sprintf("=== RUN: %s\n", result.Name))
if result.Pass {
strBuffer.WriteString(green("--- PASS\n"))
} else {
strBuffer.WriteString(red("--- FAIL\n"))
}
if result.Stdout != "" {
strBuffer.WriteString(blue(fmt.Sprintf("stdout: %s", result.Stdout)))
}
if result.Stderr != "" {
strBuffer.WriteString(blue(fmt.Sprintf("stderr: %s", result.Stderr)))
}
for _, s := range result.Errors {
strBuffer.WriteString(orange(fmt.Sprintf("Error: %s\n", s)))
}
strBuffer.WriteString("\n")
_, err := out.Write(strBuffer.Bytes())
return err
}
func Banner(out io.Writer, filename string) error {
var strBuffer bytes.Buffer
fileStr := fmt.Sprintf("====== Test file: %s ======", filepath.Base(filename))
bannerLength = len(fileStr)
strBuffer.WriteString("\n" + strings.Repeat("=", bannerLength) + "\n")
strBuffer.WriteString(fileStr + "\n")
strBuffer.WriteString(strings.Repeat("=", bannerLength) + "\n")
_, err := out.Write([]byte(purple(strBuffer.String())))
return err
}
func FinalResults(out io.Writer, jsonOut bool, result types.SummaryObject) error {
if jsonOut {
res, err := json.Marshal(result)
if err != nil {
return errors.Wrap(err, "marshalling json")
}
res = append(res, []byte("\n")...)
_, err = out.Write(res)
return err
}
if bannerLength%2 == 0 {
bannerLength++
}
var strBuffer bytes.Buffer
if result.Total == 0 {
strBuffer.WriteString(red("No tests run! Check config file format."))
_, err := out.Write(strBuffer.Bytes())
return err
}
strBuffer.WriteString("\n" + strings.Repeat("=", bannerLength) + "\n")
strBuffer.WriteString(strings.Repeat("=", (bannerLength-9)/2))
strBuffer.WriteString(" RESULTS ")
strBuffer.WriteString(strings.Repeat("=", (bannerLength-9)/2))
strBuffer.WriteString("\n" + strings.Repeat("=", bannerLength) + "\n")
strBuffer.WriteString(lightGreen(fmt.Sprintf("Passes: %d\n", result.Pass)))
strBuffer.WriteString(lightRed(fmt.Sprintf("Failures: %d\n", result.Fail)))
strBuffer.WriteString(cyan(fmt.Sprintf("Total tests: %d\n", result.Total)))
if result.Fail == 0 {
strBuffer.WriteString(green("\nPASS"))
} else {
strBuffer.WriteString(red("\nFAIL"))
}
strBuffer.WriteString("\n")
_, err := out.Write(strBuffer.Bytes())
return err
}