Skip to content

Commit 5c77636

Browse files
committed
tests: add a helper to return log messages for a particular test
1 parent 403f815 commit 5c77636

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

testhelper_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func TestHelperSuite(t *testing.T) {
3838

3939
type helperResult []string
4040

41-
var testStatusLine = regexp.MustCompile(`^\s*--- ([A-Z]+): ([0-9A-Za-z/]+) \(\d+\.\d+s\)$`)
41+
var (
42+
testRunLine = regexp.MustCompile(`^=== (?:RUN|CONT)\s+([0-9A-Za-z/]+)$`)
43+
testStatusLine = regexp.MustCompile(`^\s*--- ([A-Z]+): ([0-9A-Za-z/]+) \(\d+\.\d+s\)$`)
44+
)
4245

4346
func (result helperResult) Status(test string) string {
4447
for _, line := range result {
@@ -53,6 +56,26 @@ func (result helperResult) Status(test string) string {
5356
return ""
5457
}
5558

59+
func (result helperResult) Logs(test string) string {
60+
var lines []string
61+
var inTest bool
62+
for _, line := range result {
63+
if inTest {
64+
// Log messages are all indented
65+
if strings.HasPrefix(line, " ") {
66+
lines = append(lines, line)
67+
continue
68+
}
69+
inTest = false
70+
}
71+
match := testRunLine.FindStringSubmatch(line)
72+
if match != nil && match[1] == "TestHelperSuite/" + test {
73+
inTest = true
74+
}
75+
}
76+
return strings.Join(lines, "\n")
77+
}
78+
5679
func runHelperSuite(name string, args ...string) (code int, output helperResult) {
5780
args = append([]string{"-test.v", "-test.run", "TestHelperSuite", "-helper.run", name}, args...)
5881
cmd := exec.Command(os.Args[0], args...)

0 commit comments

Comments
 (0)