Repository navigation
Expand file tree
/
Copy pathannotations.go
More file actions
135 lines (118 loc) · 3.75 KB
/
Copy pathannotations.go
File metadata and controls
135 lines (118 loc) · 3.75 KB
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
package testutil
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
)
const (
envFlag = "GH_ANNOTATION"
rootPath = "/linkerd2/"
)
type level int
const (
err level = iota
warn
)
func (l level) String() string {
switch l {
case err:
return "error"
case warn:
return "warning"
}
panic(fmt.Sprintf("invalid level: %d", l))
}
func echoAnnotation(t *testing.T, l level, args ...interface{}) {
if _, ok := os.LookupEnv(envFlag); ok {
_, fileName, fileLine, ok := runtime.Caller(3)
if !ok {
panic("Couldn't recover runtime info")
}
fileName = fileName[strings.LastIndex(fileName, rootPath)+len(rootPath):]
// In case of coming from `t.Run(testName, ...)`, only take the first part
// of the name; the following parts might not be as generic
parts := strings.Split(t.Name(), "/")
testName := parts[0]
for _, arg := range args {
msg := fmt.Sprintf("%s - %s", testName, arg)
fmt.Printf("::%s file=%s,line=%d::%s\n", l, fileName, fileLine, msg)
}
}
}
func echoAnnotationErr(t *testing.T, args ...interface{}) {
echoAnnotation(t, err, args...)
}
func echoAnnotationWarn(t *testing.T, args ...interface{}) {
echoAnnotation(t, warn, args...)
}
// Error is a wrapper around t.Error()
// args are passed to t.Error(args) and each arg will be sent to stdout formatted
// as a GitHub annotation when the envFlag environment variable is set
func Error(t *testing.T, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, args...)
t.Error(args...)
}
// AnnotatedError is similar to Error() but it also admits a msg string that
// will be used as the GitHub annotation
func AnnotatedError(t *testing.T, msg string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, msg)
t.Error(args...)
}
// Errorf is a wrapper around t.Errorf()
// format and args are passed to t.Errorf(format, args) and the formatted
// message will be sent to stdout as a GitHub annotation when the envFlag
// environment variable is set
func Errorf(t *testing.T, format string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, fmt.Sprintf(format, args...))
t.Errorf(format, args...)
}
// AnnotatedErrorf is similar to Errorf() but it also admits a msg string that
// will be used as the GitHub annotation
func AnnotatedErrorf(t *testing.T, msg, format string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, msg)
t.Errorf(format, args...)
}
// Fatal is a wrapper around t.Fatal()
// args are passed to t.Fatal(args) and each arg will be sent to stdout formatted
// as a GitHub annotation when the envFlag environment variable is set
func Fatal(t *testing.T, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, args)
t.Fatal(args...)
}
// AnnotatedFatal is similar to Fatal() but it also admits a msg string that
// will be used as the GitHub annotation
func AnnotatedFatal(t *testing.T, msg string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, msg)
t.Fatal(args...)
}
// Fatalf is a wrapper around t.Errorf()
// format and args are passed to t.Fatalf(format, args) and the formatted
// message will be sent to stdout as a GitHub annotation when the envFlag
// environment variable is set
func Fatalf(t *testing.T, format string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, fmt.Sprintf(format, args...))
t.Fatalf(format, args...)
}
// AnnotatedFatalf is similar to Fatalf() but it also admits a msg string that
// will be used as the GitHub annotation
func AnnotatedFatalf(t *testing.T, msg, format string, args ...interface{}) {
t.Helper()
echoAnnotationErr(t, msg)
t.Fatalf(format, args...)
}
// AnnotatedWarn is a wrapper around t.Log() but it also admits a msg string that
// will be used as the GitHub warning annotation
func AnnotatedWarn(t *testing.T, msg string, args ...interface{}) {
t.Helper()
echoAnnotationWarn(t, msg)
t.Log(args...)
}