Skip to content

Commit

Permalink
Fix build failure for TestMain in black box test
Browse files Browse the repository at this point in the history
Fixes #32.
  • Loading branch information
rillig committed Feb 20, 2024
1 parent ef83d56 commit c5f80fe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
9 changes: 5 additions & 4 deletions instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, dstDir string) {
"\n" +
"func GobcoCover(idx int, cond bool) bool {\n" +
"\t" + "return " + pkgName + ".GobcoCover(idx, cond)\n" +
"}\n" +
"\n" +
"func GobcoFinish(code int) int {\n" +
"\t" + "return " + pkgName + ".GobcoFinish(code)\n" +
"}\n"

writeFile(filepath.Join(dstDir, "gobco_bridge_test.go"), text)
Expand Down Expand Up @@ -700,10 +704,7 @@ func (gen codeGenerator) typeAssertExpr(x string, typ ast.Expr) ast.Expr {

func (gen codeGenerator) callFinish(arg ast.Expr) ast.Expr {
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: gen.ident("gobcoCounts"),
Sel: gen.ident("finish"),
},
Fun: gen.ident("GobcoFinish"),
Lparen: gen.pos,
Args: []ast.Expr{arg},
Ellipsis: token.NoPos,
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ func Test_gobcoMain__TestMainTest(t *testing.T) {
s := NewSuite(t)
defer s.TearDownTest()

stdout, stderr := s.RunMain(1, "gobco", "-verbose", "testdata/testmaintest")
stdout, stderr := s.RunMain(0, "gobco", "-verbose", "testdata/testmaintest")

s.CheckContains(stdout, "[build failed]")
s.CheckContains(stderr, ": undefined: gobcoCounts")
s.CheckContains(stdout, "Condition coverage: 1/2")
_ = stderr
}

func Test_gobcoMain__oddeven(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions templates/gobco_fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ func (st *gobcoStats) finish(exitCode int) int {
func GobcoCover(idx int, cond bool) bool {
return gobcoCounts.cover(idx, cond)
}

// GobcoFinish needs to be exported to black-box test packages.
func GobcoFinish(code int) int {
return gobcoCounts.finish(code)
}
6 changes: 5 additions & 1 deletion testdata/testmaintest/add.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package add

func Add(x, y int) int {
return x + y
if x > y {
return x + y
} else {
return y + x
}
}

0 comments on commit c5f80fe

Please sign in to comment.