--- /dev/null
+# For issue golang.org/issue/41084
+[short] skip
+
+! go test -v examplegoexit
+stdout '(?s)--- PASS.*--- FAIL.*'
+stdout 'panic: test executed panic\(nil\) or runtime\.Goexit'
+
+-- examplegoexit/example_test.go --
+package main
+
+import (
+ "fmt"
+ "runtime"
+)
+
+func ExamplePass() {
+ fmt.Println("pass")
+ // Output:
+ // pass
+}
+
+func ExampleGoexit() {
+ runtime.Goexit()
+ // Output:
+}
// If stdout doesn't match the expected output or if recovered is non-nil, it'll print the cause of failure to stdout.
// If the test is chatty/verbose, it'll print a success message to stdout.
// If recovered is non-nil, it'll panic with that value.
-func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Duration, recovered interface{}) (passed bool) {
+// If the test panicked with nil, or invoked runtime.Goexit, it'll be
+// made to fail and panic with errNilPanicOrGoexit
+func (eg *InternalExample) processRunResult(stdout string, timeSpent time.Duration, finished bool, recovered interface{}) (passed bool) {
passed = true
-
dstr := fmtDuration(timeSpent)
var fail string
got := strings.TrimSpace(stdout)
fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", got, want)
}
}
- if fail != "" || recovered != nil {
+ if fail != "" || !finished || recovered != nil {
fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)
passed = false
} else if *chatty {
fmt.Printf("--- PASS: %s (%s)\n", eg.Name, dstr)
}
+
if recovered != nil {
// Propagate the previously recovered result, by panicking.
panic(recovered)
}
+ if !finished && recovered == nil {
+ panic(errNilPanicOrGoexit)
+ }
return
}
outC <- buf.String()
}()
+ finished := false
start := time.Now()
// Clean up in a deferred call so we can recover if the example panics.
out := <-outC
err := recover()
- ok = eg.processRunResult(out, timeSpent, err)
+ ok = eg.processRunResult(out, timeSpent, finished, err)
}()
// Run example.
eg.F()
+ finished = true
return
}
stdout := os.Stdout
f := createTempFile(eg.Name)
os.Stdout = f
+ finished := false
start := time.Now()
// Clean up in a deferred call so we can recover if the example panics.
}
err := recover()
- ok = eg.processRunResult(out, timeSpent, err)
+ ok = eg.processRunResult(out, timeSpent, finished, err)
}()
// Run example.
eg.F()
+ finished = true
return
}