Skip to content

Commit f1b9519

Browse files
authored
testscript: expose current subtest in Env (#95)
1 parent bc89b17 commit f1b9519

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

testscript/testscript.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ func (e *Env) Defer(f func()) {
6969
e.ts.Defer(f)
7070
}
7171

72+
// T returns the t argument passed to the current test by the T.Run method.
73+
// Note that if the tests were started by calling Run,
74+
// the returned value will implement testing.TB.
75+
// Note that, despite that, the underlying value will not be of type
76+
// *testing.T because *testing.T does not implement T.
77+
//
78+
// If Cleanup is called on the returned value, the function will run
79+
// after any functions passed to Env.Defer.
80+
func (e *Env) T() T {
81+
return e.ts.t
82+
}
83+
7284
// Params holds parameters for a call to Run.
7385
type Params struct {
7486
// Dir holds the name of the directory holding the scripts.

testscript/testscript_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ func TestScripts(t *testing.T) {
113113
if ts.Value("somekey") != 1234 {
114114
ts.Fatalf("test-values did not see expected value")
115115
}
116+
if ts.Value("t").(T) != ts.t {
117+
ts.Fatalf("test-values did not see expected t")
118+
}
119+
if _, ok := ts.Value("t").(testing.TB); !ok {
120+
ts.Fatalf("test-values t does not implement testing.TB")
121+
}
116122
},
117123
"testreadfile": func(ts *TestScript, neg bool, args []string) {
118124
if len(args) != 1 {
@@ -165,6 +171,7 @@ func TestScripts(t *testing.T) {
165171
}
166172
env.Values["setupFilenames"] = setupFilenames
167173
env.Values["somekey"] = 1234
174+
env.Values["t"] = env.T()
168175
env.Vars = append(env.Vars,
169176
"GONOSUMDB=*",
170177
)

0 commit comments

Comments
 (0)