-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testscript: phase out func() int
in RunMain
#281
base: master
Are you sure you want to change the base?
Conversation
We wanted the user's command functions to return an exit code as an int rather than calling os.Exit directly like a main function so that we could collect coverage profiles from subprocesses. This way, Go tests using testscript would still report the full code coverage information even when using nested processes. This all thankfully went away with Go 1.20, which introduced the same feature but built right into the toolchain for both `go test` and `go build`. As such, we were able to drop all of that code, including the bit that we ran before os.Exit. For more information, see: https://go.dev/blog/integration-test-coverage At this point, testscript users continue to use the `func() int` signature, via e.g. `func main1() int` out of inertia, but there's actually no good reason to keep doing that. It causes extra boilerplate and confuses new testscript users. Moreover, avoiding the use of os.Exit was rather tricky, for example see the former use of flag.ContinueOnExit in our tests. Add a new API, Main, which uses a `func()` signature just like `func main()`, meaning that no second function declaration is needed. Deprecate RunMain in favor of Main as well.
This has been bugging me for some time so I did something about it. I'm not particularly set on the name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but we should have at least one test that still exercises the old functionality, I think.
@@ -58,18 +54,18 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { | |||
tmpdir, err := os.MkdirTemp("", "testscript-main") | |||
if err != nil { | |||
log.Printf("could not set up temporary directory: %v", err) | |||
return 2 | |||
os.Exit(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Fatalf (and elsewhere) ?
@@ -23,13 +23,11 @@ type TestingM interface { | |||
// Deprecated: this option is no longer used. | |||
func IgnoreMissedCoverage() {} | |||
|
|||
// RunMain should be called within a TestMain function to allow | |||
// Main should be called within a TestMain function to allow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc comment should point out that Main always invokes os.Exit and never returns.
(see commit message)