-
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
Open
mvdan
wants to merge
1
commit into
rogpeppe:master
Choose a base branch
from
mvdan:testscript-main
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
// subcommands to be run in the testscript context. | ||
// | ||
// The commands map holds the set of command names, each | ||
// with an associated run function which should return the | ||
// code to pass to os.Exit. It's OK for a command function to | ||
// exit itself, but this may result in loss of coverage information. | ||
// with an associated run function which may call os.Exit. | ||
// | ||
// When Run is called, these commands are installed as regular commands in the shell | ||
// path, so can be invoked with "exec" or via any other command (for example a shell script). | ||
|
@@ -38,9 +36,7 @@ func IgnoreMissedCoverage() {} | |
// without "exec" - that is, "foo" will behave like "exec foo". | ||
// This can be disabled with Params.RequireExplicitExec to keep consistency | ||
// across test scripts, and to keep separate process executions explicit. | ||
// | ||
// This function returns an exit code to pass to os.Exit, after calling m.Run. | ||
func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { | ||
func Main(m TestingM, commands map[string]func()) { | ||
// Depending on os.Args[0], this is either the top-level execution of | ||
// the test binary by "go test", or the execution of one of the provided | ||
// commands via "foo" or "exec foo". | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. log.Fatalf (and elsewhere) ? |
||
} | ||
defer func() { | ||
if err := os.RemoveAll(tmpdir); err != nil { | ||
log.Printf("cannot delete temporary directory: %v", err) | ||
exitCode = 2 | ||
os.Exit(2) | ||
} | ||
}() | ||
bindir := filepath.Join(tmpdir, "bin") | ||
if err := os.MkdirAll(bindir, 0o777); err != nil { | ||
log.Printf("could not set up PATH binary directory: %v", err) | ||
return 2 | ||
os.Exit(2) | ||
} | ||
os.Setenv("PATH", bindir+string(filepath.ListSeparator)+os.Getenv("PATH")) | ||
|
||
|
@@ -86,7 +82,7 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { | |
} | ||
if err != nil { | ||
log.Printf("could not set up %s in $PATH: %v", name, err) | ||
return 2 | ||
os.Exit(2) | ||
} | ||
scriptCmds[name] = func(ts *TestScript, neg bool, args []string) { | ||
if ts.params.RequireExplicitExec { | ||
|
@@ -95,11 +91,26 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { | |
ts.cmdExec(neg, append([]string{name}, args...)) | ||
} | ||
} | ||
return m.Run() | ||
os.Exit(m.Run()) | ||
} | ||
// The command being registered is being invoked, so run it, then exit. | ||
os.Args[0] = cmdName | ||
return mainf() | ||
mainf() | ||
os.Exit(0) | ||
} | ||
|
||
// Deprecated: use [Main], as the only reason for returning exit codes | ||
// was to collect full code coverage, which Go does automatically now: | ||
// https://go.dev/blog/integration-test-coverage | ||
func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { | ||
commands2 := make(map[string]func(), len(commands)) | ||
for name, fn := range commands { | ||
commands2[name] = func() { os.Exit(fn()) } | ||
} | ||
Main(m, commands2) | ||
// Main always calls os.Exit; we assume that all users of RunMain would have simply | ||
// called os.Exit with the returned exitCode as well, following the documentation. | ||
panic("unreachable") | ||
} | ||
|
||
// copyBinary makes a copy of a binary to a new location. It is used as part of | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.