-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
157 lines (137 loc) · 3.68 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
)
const (
TEAMCITY_TIMESTAMP_FORMAT = "2006-01-02T15:04:05.000"
)
type Test struct {
Start string
Name string
Output string
Details []string
Duration time.Duration
Status string
Race bool
}
var (
input = os.Stdin
output = os.Stdout
additionalTestName = ""
run = regexp.MustCompile("^=== RUN\\s+([a-zA-Z_]\\S*)")
end = regexp.MustCompile("^(\\s*)--- (PASS|SKIP|FAIL):\\s+([a-zA-Z_]\\S*) \\((-?[\\.\\ds]+)\\)")
suite = regexp.MustCompile("^(ok|PASS|FAIL|exit status|Found)")
race = regexp.MustCompile("^WARNING: DATA RACE")
)
func init() {
flag.StringVar(&additionalTestName, "name", "", "Add prefix to test name")
}
func escapeLines(lines []string) string {
return escape(strings.Join(lines, "\n"))
}
func escape(s string) string {
s = strings.Replace(s, "|", "||", -1)
s = strings.Replace(s, "\n", "|n", -1)
s = strings.Replace(s, "\r", "|n", -1)
s = strings.Replace(s, "'", "|'", -1)
s = strings.Replace(s, "]", "|]", -1)
s = strings.Replace(s, "[", "|[", -1)
return s
}
func getNow() string {
return time.Now().Format(TEAMCITY_TIMESTAMP_FORMAT)
}
func outputTest(w io.Writer, test *Test) {
now := getNow()
testName := escape(additionalTestName + test.Name)
fmt.Fprintf(w, "##teamcity[testStarted timestamp='%s' name='%s' captureStandardOutput='true']\n", test.Start, testName)
fmt.Fprint(w, test.Output)
if test.Status == "SKIP" {
fmt.Fprintf(w, "##teamcity[testIgnored timestamp='%s' name='%s']\n", now, testName)
} else {
if test.Race {
fmt.Fprintf(w, "##teamcity[testFailed timestamp='%s' name='%s' message='Race detected!' details='%s']\n",
now, testName, escapeLines(test.Details))
} else {
switch test.Status {
case "FAIL":
fmt.Fprintf(w, "##teamcity[testFailed timestamp='%s' name='%s' details='%s']\n",
now, testName, escapeLines(test.Details))
case "PASS":
// ignore
default:
fmt.Fprintf(w, "##teamcity[testFailed timestamp='%s' name='%s' message='Test ended in panic.' details='%s']\n",
now, testName, escapeLines(test.Details))
}
}
fmt.Fprintf(w, "##teamcity[testFinished timestamp='%s' name='%s' duration='%d']\n",
now, testName, test.Duration/time.Millisecond)
}
}
func processReader(r *bufio.Reader, w io.Writer) {
tests := make(map[string]*Test)
var test *Test
var final string
prefix := "\t"
for {
line, err := r.ReadString('\n')
if err != nil {
break
}
runOut := run.FindStringSubmatch(line)
endOut := end.FindStringSubmatch(line)
suiteOut := suite.FindStringSubmatch(line)
if test != nil && test.Status != "" && (runOut != nil || endOut != nil || suiteOut != nil) {
outputTest(w, test)
delete(tests, test.Name)
test = nil
}
if runOut != nil {
test = &Test{
Name: runOut[1],
Start: getNow(),
}
tests[test.Name] = test
} else if endOut != nil {
test = tests[endOut[3]]
prefix = endOut[1] + "\t"
test.Status = endOut[2]
test.Duration, _ = time.ParseDuration(endOut[4])
} else if suiteOut != nil {
final += line
} else if race.MatchString(line) {
test.Race = true
} else if test != nil && test.Status != "" && strings.HasPrefix(line, prefix) {
line = line[:len(line)-1]
line = strings.TrimPrefix(line, prefix)
test.Details = append(test.Details, line)
} else if test != nil {
test.Output += line
} else {
fmt.Fprint(w, line)
}
}
if test != nil {
outputTest(w, test)
delete(tests, test.Name)
}
for _, t := range tests {
outputTest(w, t)
}
fmt.Fprint(w, final)
}
func main() {
flag.Parse()
if len(additionalTestName) > 0 {
additionalTestName += " "
}
reader := bufio.NewReader(input)
processReader(reader, output)
}