Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mvdan
Copy link
Collaborator

@mvdan mvdan commented Nov 24, 2024

(see commit message)

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.
@mvdan mvdan requested review from rogpeppe and myitcv November 24, 2024 20:44
@mvdan
Copy link
Collaborator Author

mvdan commented Nov 24, 2024

This has been bugging me for some time so I did something about it. I'm not particularly set on the name Main if you have a better suggestion. Run is taken, and I thought TestMain would be too confusing with the func that the user already has to write at the top level.

Copy link
Owner

@rogpeppe rogpeppe left a 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)
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants