[dev.regabi] test: make run.go error messages slightly more informative

This is intended to make it easier to write/change a test
without referring to the source code to figure out what the
error messages actually mean, or how to correct them.

Change-Id: Ie79ff7cd9f2d1fa605257fe97eace68adc8a6716
Reviewed-on: https://go-review.googlesource.com/c/go/+/281452
Trust: David Chase <drchase@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
David Chase 2021-01-04 14:05:17 -05:00
parent 9a19481acb
commit 2abd24f3b7

View file

@ -489,7 +489,7 @@ func (t *test) run() {
// Execution recipe stops at first blank line. // Execution recipe stops at first blank line.
pos := strings.Index(t.src, "\n\n") pos := strings.Index(t.src, "\n\n")
if pos == -1 { if pos == -1 {
t.err = errors.New("double newline not found") t.err = fmt.Errorf("double newline ending execution recipe not found in %s", t.goFileName())
return return
} }
action := t.src[:pos] action := t.src[:pos]
@ -860,9 +860,7 @@ func (t *test) run() {
t.err = err t.err = err
return return
} }
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
} }
} }
@ -902,9 +900,7 @@ func (t *test) run() {
t.err = err t.err = err
return return
} }
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
case "build": case "build":
// Build Go file. // Build Go file.
@ -989,9 +985,7 @@ func (t *test) run() {
t.err = err t.err = err
break break
} }
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
} }
case "buildrun": case "buildrun":
@ -1017,9 +1011,7 @@ func (t *test) run() {
return return
} }
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
case "run": case "run":
// Run Go file if no special go command flags are provided; // Run Go file if no special go command flags are provided;
@ -1062,9 +1054,7 @@ func (t *test) run() {
t.err = err t.err = err
return return
} }
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
case "runoutput": case "runoutput":
// Run Go file and write its output into temporary Go file. // Run Go file and write its output into temporary Go file.
@ -1099,9 +1089,7 @@ func (t *test) run() {
t.err = err t.err = err
return return
} }
if string(out) != t.expectedOutput() { t.checkExpectedOutput(out)
t.err = fmt.Errorf("incorrect output\n%s", out)
}
case "errorcheckoutput": case "errorcheckoutput":
// Run Go file and write its output into temporary Go file. // Run Go file and write its output into temporary Go file.
@ -1175,12 +1163,24 @@ func (t *test) makeTempDir() {
} }
} }
func (t *test) expectedOutput() string { // checkExpectedOutput compares the output from compiling and/or running with the contents
// of the corresponding reference output file, if any (replace ".go" with ".out").
// If they don't match, fail with an informative message.
func (t *test) checkExpectedOutput(gotBytes []byte) {
got := string(gotBytes)
filename := filepath.Join(t.dir, t.gofile) filename := filepath.Join(t.dir, t.gofile)
filename = filename[:len(filename)-len(".go")] filename = filename[:len(filename)-len(".go")]
filename += ".out" filename += ".out"
b, _ := ioutil.ReadFile(filename) b, err := ioutil.ReadFile(filename)
return string(b) // File is allowed to be missing (err != nil) in which case output should be empty.
got = strings.Replace(got, "\r\n", "\n", -1)
if got != string(b) {
if err == nil {
t.err = fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
} else {
t.err = fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
}
}
} }
func splitOutput(out string, wantAuto bool) []string { func splitOutput(out string, wantAuto bool) []string {