Skip to content

Commit

Permalink
testscript: expose current subtest in Env (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankban authored Apr 30, 2020
1 parent bc89b17 commit f1b9519
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func (e *Env) Defer(f func()) {
e.ts.Defer(f)
}

// T returns the t argument passed to the current test by the T.Run method.
// Note that if the tests were started by calling Run,
// the returned value will implement testing.TB.
// Note that, despite that, the underlying value will not be of type
// *testing.T because *testing.T does not implement T.
//
// If Cleanup is called on the returned value, the function will run
// after any functions passed to Env.Defer.
func (e *Env) T() T {
return e.ts.t
}

// Params holds parameters for a call to Run.
type Params struct {
// Dir holds the name of the directory holding the scripts.
Expand Down
7 changes: 7 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func TestScripts(t *testing.T) {
if ts.Value("somekey") != 1234 {
ts.Fatalf("test-values did not see expected value")
}
if ts.Value("t").(T) != ts.t {
ts.Fatalf("test-values did not see expected t")
}
if _, ok := ts.Value("t").(testing.TB); !ok {
ts.Fatalf("test-values t does not implement testing.TB")
}
},
"testreadfile": func(ts *TestScript, neg bool, args []string) {
if len(args) != 1 {
Expand Down Expand Up @@ -165,6 +171,7 @@ func TestScripts(t *testing.T) {
}
env.Values["setupFilenames"] = setupFilenames
env.Values["somekey"] = 1234
env.Values["t"] = env.T()
env.Vars = append(env.Vars,
"GONOSUMDB=*",
)
Expand Down

0 comments on commit f1b9519

Please sign in to comment.