Commits

Miki Tebeka committed 772ff2c

Fix edge case (thanks Thomas Pelletier), move gotest.out here

Comments (0)

Files changed (3)

 // parseOutput parses output of "go test -v", returns a list of tests
 func parseOutput(rd io.Reader) ([]*Test, error) {
 	tests := []*Test{}
+	var test *Test = nil
+
+	var nextTest = func() {
+		// We are switching to the next test, store the current one.
+		if test == nil {
+			return
+		}
+
+		tests = append(tests, test)
+		test = nil
+	}
 
 	reader := bufio.NewReader(rd)
-	var test *Test = nil
 	for {
-		/* FIXME: Handle isPrefix */
 		buf, _, err := reader.ReadLine()
 
 		switch err {
 			}
 			return tests, nil
 		case nil:
+			// nil is OK
 
-		default:
+		default: // Error other than io.EOF
 			return nil, err
 		}
 
 		line := string(buf)
+
 		switch {
 		case strings.HasPrefix(line, startPrefix):
-			if test != nil {
-				tests = append(tests, test)
-			}
-			test = &Test{Name: line[len(startPrefix):]}
 		case strings.HasPrefix(line, failPrefix):
-			if test == nil {
-				return nil, fmt.Errorf("fail not inside test")
-			}
-			test.Failed = true
-			name, time, err := parseEnd(failPrefix, line)
-			if err != nil {
-				return nil, err
-			}
-			if name != test.Name {
-				return nil, fmt.Errorf("wrong test end (%s!=%s)", name, test.Name)
-			}
-			test.Time = time
-		case strings.HasPrefix(line, passPrefix):
-			if test == nil {
-				return nil, fmt.Errorf("pass not inside test")
-			}
-			test.Failed = false
+			nextTest()
+
+			// Extract the test name and the duration:
 			name, time, err := parseEnd(passPrefix, line)
 			if err != nil {
 				return nil, err
 			}
-			if name != test.Name {
-				return nil, fmt.Errorf("wrong test end (%s!=%s)", name, test.Name)
+
+			test = &Test{
+				Name:   name,
+				Time:   time,
+				Failed: true,
 			}
-			test.Time = time
+
+		case strings.HasPrefix(line, passPrefix):
+			nextTest()
+			// Extract the test name and the duration:
+			name, time, err := parseEnd(passPrefix, line)
+			if err != nil {
+				return nil, err
+			}
+
+			// Create the test structure and store it.
+			tests = append(tests, &Test{
+				Name:   name,
+				Time:   time,
+				Failed: false,
+			})
+			test = nil
+		case line == "FAIL":
+			nextTest()
 		default:
 			if test != nil { // test != nil marks we're in the middle of a test
 				test.Message += line + "\n"
 		}
 	}
 
-	return tests, nil
+	// If we're here, it's an error
+	return nil, fmt.Errorf("Error parsing")
 }
 
 // numFailures count how man tests failed
 }
 
 func Test_parseOutput(t *testing.T) {
-	tests := loadTests("data/gotest.out", t)
+	tests := loadTests("gotest.out", t)
 	if len(tests) != 4 {
 		t.Fatalf("got %d tests instead of 4", len(tests))
 	}
+=== RUN TestAdd
+--- PASS: TestAdd (0.00 seconds)
+=== RUN TestSub
+--- PASS: TestSub (0.00 seconds)
+=== RUN TestSubFail
+--- FAIL: TestSubFail (0.00 seconds)
+	xunit_test.go:22: 3-1 != 3
+		Some newline goes here
+=== RUN TestSubOK
+--- PASS: TestSubOK (0.00 seconds)
+FAIL
+exit status 1
+FAIL	_/home/miki/Projects/goroot/src/xunit	0.004s