Commits

Anonymous committed 3f9bea7

Ensuring that an error message in the last test in a suite is correctly parsed.

Comments (0)

Files changed (3)

data/gotest-multierror.out

+=== RUN TestError1
+--- FAIL: TestError1 (0.00 seconds)
+	main_test.go:10: something went wrong
+=== RUN TestError2
+--- FAIL: TestError2 (0.00 seconds)
+	main_test.go:14: something new went wrong
+FAIL
+exit status 1
+FAIL	skeleton	0.047s
 	var curSuite *Suite
 	var out []string
 
+	AddRemainingOutputToLastTest := func() error {
+		if len(out) > 0 {
+			message := strings.Join(out, "\n")
+			if curSuite == nil {
+				return fmt.Errorf("orphan output: %s", message)
+			}
+			curSuite.Tests[len(curSuite.Tests)-1].Message = message
+		}
+		out = []string{}
+		return nil
+	}
+
 	scanner := bufio.NewScanner(rd)
 	for lnum := 1; scanner.Scan(); lnum++ {
 		line := scanner.Text()
 			curTest = &Test{
 				Name: tokens[1],
 			}
-			if len(out) > 0 {
-				message := strings.Join(out, "\n")
-				if curSuite == nil {
-					return nil, fmt.Errorf("orphan output: %s", message)
-				}
-				curSuite.Tests[len(curSuite.Tests)-1].Message = message
+
+			if e := AddRemainingOutputToLastTest(); e != nil {
+				return nil, e
 			}
-			out = []string{}
 			continue
 		}
 
 			}
 			curSuite.Name = tokens[2]
 			curSuite.Time = tokens[3]
+			if e := AddRemainingOutputToLastTest(); e != nil {
+				return nil, e
+			}
 			suites = append(suites, curSuite)
 			curSuite = nil
 
 		t.Fatalf("bad number of suites. expected %d got %d", count, len(suites))
 	}
 }
+
+func Test_ThatMessageIsParsedCorrectly_WhenThereIsAnErrorWithinTheLastTestInSuite(t *testing.T) {
+	filename := "data/gotest-multierror.out"
+	suites, err := loadGotest(filename, t)
+	if err != nil {
+		t.Fatalf("error loading %s - %s", filename, err)
+	}
+
+	count := 1
+	if len(suites) != count {
+		t.Fatalf("bad number of suites. expected %d got %d", count, len(suites))
+	}
+
+	suite := suites[0]
+
+	if len(suite.Tests) != 2 {
+		t.Fatalf("bad number of tests. expected %d got %d", 2, len(suite.Tests))
+	}
+
+	expectedMessages := []string{
+		"\tmain_test.go:10: something went wrong",
+		"\tmain_test.go:14: something new went wrong",
+	}
+
+	for i, test := range suite.Tests {
+		if test.Message != expectedMessages[i] {
+			t.Errorf(
+				"test %v message does not match expected result:\n\tGot: \"%v\"\n\tExpected: \"%v\"\n",
+				i,
+				test.Message,
+				expectedMessages[i])
+		}
+	}
+}