Skip to content

Commit

Permalink
Add SetUp method to Args for go_bazel_test (#2282)
Browse files Browse the repository at this point in the history
  • Loading branch information
achew22 authored and Jay Conrod committed Nov 18, 2019
1 parent 7a8059f commit b8db84a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions go/tools/bazel_testing/bazel_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ type Args struct {
// WorkspaceSuffix is a string that should be appended to the end
// of the default generated WORKSPACE file.
WorkspaceSuffix string

// SetUp is a function that is executed inside the context of the testing
// workspace. It is executed once and only once before the beginning of
// all tests. If SetUp returns a non-nil error, execution is halted and
// tests cases are not executed.
SetUp func() error
}

// debug may be set to make the test print the test workspace path and stop
Expand Down Expand Up @@ -139,6 +145,13 @@ func TestMain(m *testing.M, args Args) {
}
defer exec.Command("bazel", "shutdown").Run()

if args.SetUp != nil {
if err := args.SetUp(); err != nil {
fmt.Fprintf(os.Stderr, "test provided SetUp method returned error: %v\n", err)
return
}
}

code = m.Run()
}

Expand Down
9 changes: 9 additions & 0 deletions tests/core/go_bazel_test/dataargtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var (
binaryPath = flag.String("binaryPath", "", "")
setUpRan = false
)

func TestMain(m *testing.M) {
Expand All @@ -34,6 +35,10 @@ func TestMain(m *testing.M) {
Hello world!
-- nested/file.txt --
Hello world!`,
SetUp: func() error {
setUpRan = true
return nil
},
})
}

Expand All @@ -57,4 +62,8 @@ func TestGoldenPath(t *testing.T) {
t.Fatalf("unable to stat %s file (%q): %s", name, f, err)
}
}

if setUpRan == false {
t.Fatal("setUp should have been executed but was not")
}
}

0 comments on commit b8db84a

Please sign in to comment.