summaryrefslogtreecommitdiff
path: root/tools/debug/logfilter/main.go
blob: 25bf6feae4a1129c0097e41de29816661e34cdce (plain)
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
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

// logfilter buffer go test output and make sure to limit the output to only the error-related stuff.
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

type test struct {
	name         string
	outputBuffer string
}

func logFilter(inFile io.Reader, outFile io.Writer) int {
	scanner := bufio.NewScanner(inFile)

	tests := make(map[string]test)
	currentTestName := ""
	incomingFails := false
	// packageOutputBuffer is used to buffer messages that are package-oriented. i.e. TestMain() generated messages,
	// which are called before any test starts to run.
	packageOutputBuffer := ""
	for scanner.Scan() {
		line := scanner.Text()
		if len(line) == 0 {
			continue
		}
		if strings.HasPrefix(line, "=== RUN") {
			var testName string
			fmt.Sscanf(line, "=== RUN   %s", &testName)
			currentTestName = testName
			if _, have := tests[currentTestName]; !have {
				tests[currentTestName] = test{name: currentTestName}
			}
			continue
		}
		if strings.HasPrefix(line, "=== CONT") {
			var testName string
			fmt.Sscanf(line, "=== CONT   %s", &testName)
			currentTestName = testName
			if _, have := tests[currentTestName]; !have {
				panic(fmt.Errorf("test %s is missing", currentTestName))
			}
			continue
		}
		if strings.HasPrefix(line, "=== PAUSE") {
			var testName string
			fmt.Sscanf(line, "=== PAUSE   %s", &testName)
			currentTestName = ""
			if _, have := tests[testName]; !have {
				panic(fmt.Errorf("test %s is missing", testName))
			}
			continue
		}
		if idx := strings.Index(line, "--- PASS:"); idx >= 0 {
			var testName string
			fmt.Sscanf(line[idx:], "--- PASS: %s", &testName)
			if _, have := tests[testName]; !have {
				fmt.Fprintf(outFile, "%s\r\n%s\r\n", line, packageOutputBuffer)
				packageOutputBuffer = ""
			} else {
				fmt.Fprintf(outFile, line+"\r\n")
				delete(tests, testName)
				currentTestName = ""
			}
			continue
		}
		if idx := strings.Index(line, "--- FAIL:"); idx >= 0 {
			incomingFails = true
			var testName string
			fmt.Sscanf(line[idx:], "--- FAIL: %s", &testName)
			test, have := tests[testName]
			if !have {
				fmt.Fprintf(outFile, "%s\r\n%s\r\n", line, packageOutputBuffer)
				packageOutputBuffer = ""
			} else {
				fmt.Fprintf(outFile, test.outputBuffer+"\r\n")
				fmt.Fprintf(outFile, line+"\r\n")
				test.outputBuffer = ""
				tests[testName] = test
				currentTestName = ""
			}
			continue
		}
		// otherwise, add the line to the current test ( if there is such )
		currentTest, have := tests[currentTestName]
		if have {
			currentTest.outputBuffer += "\r\n" + line
			tests[currentTestName] = currentTest
			continue
		}
		// no current test is only legit if we're PASS, FAIL or package test line summary.
		if line == "PASS" || line == "FAIL" {
			continue
		}
		if strings.HasPrefix(line, "ok  	") {
			fmt.Fprintf(outFile, line+"\r\n")
			packageOutputBuffer = ""
			continue
		}
		if strings.HasPrefix(line, "FAIL	") {
			incomingFails = true
			if len(packageOutputBuffer) > 0 {
				fmt.Fprintf(outFile, line+"...\r\n%s\r\n", packageOutputBuffer)
			}
			packageOutputBuffer = ""
			fmt.Fprintf(outFile, line+"\r\n")
			continue
		}
		// this is package-oriented output
		packageOutputBuffer += line + "\r\n"
	}
	scannerErr := scanner.Err()
	if scannerErr != nil {
		if currentTestName != "" && tests[currentTestName].outputBuffer != "" {
			fmt.Fprint(outFile, tests[currentTestName].outputBuffer)
		}
		fmt.Fprintf(outFile, "logfilter: the following error received on the input stream : %v\r\n", scannerErr)
	}
	if incomingFails {
		return 1
	}
	return 0
}

func main() {
	retCode := logFilter(os.Stdin, os.Stdout)
	os.Exit(retCode)
}