mirror of
https://github.com/golang/go.git
synced 2025-11-10 05:31:03 +00:00
[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:
parent
9a19481acb
commit
2abd24f3b7
1 changed files with 22 additions and 22 deletions
44
test/run.go
44
test/run.go
|
|
@ -489,7 +489,7 @@ func (t *test) run() {
|
|||
// Execution recipe stops at first blank line.
|
||||
pos := strings.Index(t.src, "\n\n")
|
||||
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
|
||||
}
|
||||
action := t.src[:pos]
|
||||
|
|
@ -860,9 +860,7 @@ func (t *test) run() {
|
|||
t.err = err
|
||||
return
|
||||
}
|
||||
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -902,9 +900,7 @@ func (t *test) run() {
|
|||
t.err = err
|
||||
return
|
||||
}
|
||||
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
|
||||
case "build":
|
||||
// Build Go file.
|
||||
|
|
@ -989,9 +985,7 @@ func (t *test) run() {
|
|||
t.err = err
|
||||
break
|
||||
}
|
||||
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
}
|
||||
|
||||
case "buildrun":
|
||||
|
|
@ -1017,9 +1011,7 @@ func (t *test) run() {
|
|||
return
|
||||
}
|
||||
|
||||
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
|
||||
case "run":
|
||||
// Run Go file if no special go command flags are provided;
|
||||
|
|
@ -1062,9 +1054,7 @@ func (t *test) run() {
|
|||
t.err = err
|
||||
return
|
||||
}
|
||||
if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
|
||||
case "runoutput":
|
||||
// Run Go file and write its output into temporary Go file.
|
||||
|
|
@ -1099,9 +1089,7 @@ func (t *test) run() {
|
|||
t.err = err
|
||||
return
|
||||
}
|
||||
if string(out) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
t.checkExpectedOutput(out)
|
||||
|
||||
case "errorcheckoutput":
|
||||
// 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 = filename[:len(filename)-len(".go")]
|
||||
filename += ".out"
|
||||
b, _ := ioutil.ReadFile(filename)
|
||||
return string(b)
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
// 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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue