// 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]
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)
}
}
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.
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":
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;
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.
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.
}
}
-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 {