-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testscript: merge coverage from all test binary executions
First, consolidate how top-level commands registered via RunMain are executed. Before, they could only be run directly, such as "foo <args>". This worked because the command would run the test binary with a special TESTSCRIPT_COMMAND env variable, telling the sub-process what command to run. The unfortunate side effect was that the commands only worked when called directly. They wouldn't work when called indirectly, such as "exec foo" or "go build -toolexec=foo". To fix that inconsistency, we now set up a directory in $PATH with all the commands as copies of the test binary. The test binary sub-process knows what command it should execute thanks to os.Args[0]. This also lets us get rid of the TESTSCRIPT_COMMAND dance. Second, make all top-level command executions write coverage profiles if the -coverprofile test flag was used. Similar to the case before, this only used to work for direct command executions, not indirect ones. Now they all get merged into the final coverage profile. This is accomplished by having them all write coverage profiles under a shared directory. Once all scripts have run, the parent process walks that directory and merges all profiles found in it. Third, add a test that ensures both of the refactors above work well. It lives under gotooltest, since it runs the real "go test -coverprofile". It checks all three ways mentioned above to run a top-level command, as well as checking that all three count towards the total coverage. Note that some tests needed to be amended to keep working after the refactor. For example, some tests used a custom "echo" command as well as the system's "exec echo". Since now both of those would execute the testscript command, rename that command to fprintargs, which is also clearer and less ambiguous. Similarly, dropgofrompath had to be used in one more test, and had to be adapted to actually do the intended thing on Windows rather than just emptying the entire PATH variable. Also swap Go's 1.16beta1 for 1.16rc1 in CI, since the former is now failing due to a bug in setup-go.
- Loading branch information
Showing
14 changed files
with
286 additions
and
97 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
unquote scripts/exec.txt | ||
|
||
# The module uses testscript itself. | ||
# Use the checked out module, based on where the test binary ran. | ||
go mod edit -replace=github.com/rogpeppe/go-internal=${GOINTERNAL_MODULE} | ||
go mod tidy | ||
|
||
# First, a 'go test' run without coverage. | ||
go test -vet=off | ||
stdout 'PASS' | ||
! stdout 'total coverage' | ||
|
||
# Then, a 'go test' run with -coverprofile. | ||
# Assuming testscript works well, this results in the basic coverage being 0%, | ||
# since the test binary does not directly run any non-test code. | ||
# The total coverage after merging profiles should end up being 100%, | ||
# as long as all three sub-profiles are accounted for. | ||
# Marking all printlns as covered requires all edge cases to work well. | ||
go test -vet=off -coverprofile=cover.out -v | ||
stdout 'PASS' | ||
stdout '^coverage: 0\.0%' | ||
stdout '^total coverage: 100\.0%' | ||
! stdout 'malformed coverage' # written by "go test" if cover.out is invalid | ||
exists cover.out | ||
|
||
-- go.mod -- | ||
module test | ||
|
||
go 1.15 | ||
-- foo.go -- | ||
package foo | ||
|
||
import "os" | ||
|
||
func foo1() int { | ||
switch os.Args[1] { | ||
case "1": | ||
println("first path") | ||
case "2": | ||
println("second path") | ||
default: | ||
println("third path") | ||
} | ||
return 1 | ||
} | ||
-- foo_test.go -- | ||
package foo | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/rogpeppe/go-internal/gotooltest" | ||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
os.Exit(testscript.RunMain(m, map[string] func() int{ | ||
"foo": foo1, | ||
})) | ||
} | ||
|
||
func TestFoo(t *testing.T) { | ||
p := testscript.Params{ | ||
Dir: "scripts", | ||
} | ||
if err := gotooltest.Setup(&p); err != nil { | ||
t.Fatal(err) | ||
} | ||
testscript.Run(t, p) | ||
} | ||
-- scripts/exec.txt -- | ||
># Note that foo always fails, to prevent "go build" from doing anything. | ||
> | ||
># Running the command directly; trigger the first path. | ||
>! foo 1 | ||
> | ||
># Running the command via exec; trigger the second path. | ||
>! exec foo 2 | ||
> | ||
># Running the command indirectly, via toolexec; trigger the third path. | ||
>! go build -a -toolexec=foo runtime |
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.