1 package testutil
2
3 import (
4 "fmt"
5 "os"
6 "runtime"
7 "strings"
8 "testing"
9 )
10
11 const (
12 envFlag = "GH_ANNOTATION"
13 rootPath = "/linkerd2/"
14 )
15
16 type level int
17
18 const (
19 err level = iota
20 warn
21 )
22
23 func (l level) String() string {
24 switch l {
25 case err:
26 return "error"
27 case warn:
28 return "warning"
29 }
30 panic(fmt.Sprintf("invalid level: %d", l))
31 }
32
33 func echoAnnotation(t *testing.T, l level, args ...interface{}) {
34 if _, ok := os.LookupEnv(envFlag); ok {
35 _, fileName, fileLine, ok := runtime.Caller(3)
36 if !ok {
37 panic("Couldn't recover runtime info")
38 }
39 fileName = fileName[strings.LastIndex(fileName, rootPath)+len(rootPath):]
40
41
42 parts := strings.Split(t.Name(), "/")
43 testName := parts[0]
44 for _, arg := range args {
45 msg := fmt.Sprintf("%s - %s", testName, arg)
46 fmt.Printf("::%s file=%s,line=%d::%s\n", l, fileName, fileLine, msg)
47 }
48 }
49 }
50
51 func echoAnnotationErr(t *testing.T, args ...interface{}) {
52 echoAnnotation(t, err, args...)
53 }
54
55 func echoAnnotationWarn(t *testing.T, args ...interface{}) {
56 echoAnnotation(t, warn, args...)
57 }
58
59
60
61
62 func Error(t *testing.T, args ...interface{}) {
63 t.Helper()
64 echoAnnotationErr(t, args...)
65 t.Error(args...)
66 }
67
68
69
70 func AnnotatedError(t *testing.T, msg string, args ...interface{}) {
71 t.Helper()
72 echoAnnotationErr(t, msg)
73 t.Error(args...)
74 }
75
76
77
78
79
80 func Errorf(t *testing.T, format string, args ...interface{}) {
81 t.Helper()
82 echoAnnotationErr(t, fmt.Sprintf(format, args...))
83 t.Errorf(format, args...)
84 }
85
86
87
88 func AnnotatedErrorf(t *testing.T, msg, format string, args ...interface{}) {
89 t.Helper()
90 echoAnnotationErr(t, msg)
91 t.Errorf(format, args...)
92 }
93
94
95
96
97 func Fatal(t *testing.T, args ...interface{}) {
98 t.Helper()
99 echoAnnotationErr(t, args)
100 t.Fatal(args...)
101 }
102
103
104
105 func AnnotatedFatal(t *testing.T, msg string, args ...interface{}) {
106 t.Helper()
107 echoAnnotationErr(t, msg)
108 t.Fatal(args...)
109 }
110
111
112
113
114
115 func Fatalf(t *testing.T, format string, args ...interface{}) {
116 t.Helper()
117 echoAnnotationErr(t, fmt.Sprintf(format, args...))
118 t.Fatalf(format, args...)
119 }
120
121
122
123 func AnnotatedFatalf(t *testing.T, msg, format string, args ...interface{}) {
124 t.Helper()
125 echoAnnotationErr(t, msg)
126 t.Fatalf(format, args...)
127 }
128
129
130
131 func AnnotatedWarn(t *testing.T, msg string, args ...interface{}) {
132 t.Helper()
133 echoAnnotationWarn(t, msg)
134 t.Log(args...)
135 }
136
View as plain text