@@ -81,6 +81,40 @@ type Params struct {
8181// RunDir runs the tests in the given directory. All files in dir with a ".txt"
8282// are considered to be test files.
8383func Run (t * testing.T , p Params ) {
84+ RunT (tshim {t }, p )
85+ }
86+
87+ // T holds all the methods of the *testing.T type that
88+ // are used by testscript.
89+ type T interface {
90+ Skip (... interface {})
91+ Fatal (... interface {})
92+ Parallel ()
93+ Log (... interface {})
94+ FailNow ()
95+ Run (string , func (T ))
96+ // Verbose is usually implemented by the testing package
97+ // directly rather than on the *testing.T type.
98+ Verbose () bool
99+ }
100+
101+ type tshim struct {
102+ * testing.T
103+ }
104+
105+ func (t tshim ) Run (name string , f func (T )) {
106+ t .T .Run (name , func (t * testing.T ) {
107+ f (tshim {t })
108+ })
109+ }
110+
111+ func (t tshim ) Verbose () bool {
112+ return testing .Verbose ()
113+ }
114+
115+ // RunT is like Run but uses an interface type instead of the concrete *testing.T
116+ // type to make it possible to use testscript functionality outside of go test.
117+ func RunT (t T , p Params ) {
84118 files , err := filepath .Glob (filepath .Join (p .Dir , "*.txt" ))
85119 if err != nil {
86120 t .Fatal (err )
@@ -93,7 +127,7 @@ func Run(t *testing.T, p Params) {
93127 for _ , file := range files {
94128 file := file
95129 name := strings .TrimSuffix (filepath .Base (file ), ".txt" )
96- t .Run (name , func (t * testing. T ) {
130+ t .Run (name , func (t T ) {
97131 t .Parallel ()
98132 ts := & TestScript {
99133 t : t ,
@@ -122,7 +156,7 @@ func Run(t *testing.T, p Params) {
122156// A TestScript holds execution state for a single test script.
123157type TestScript struct {
124158 params Params
125- t * testing. T
159+ t T
126160 testTempDir string
127161 workdir string // temporary work dir ($WORK)
128162 log bytes.Buffer // test execution log (printed at end of test)
@@ -207,7 +241,7 @@ func (ts *TestScript) run() {
207241 // Truncate log at end of last phase marker,
208242 // discarding details of successful phase.
209243 rewind := func () {
210- if ! testing .Verbose () {
244+ if ! ts . t .Verbose () {
211245 ts .log .Truncate (ts .mark )
212246 }
213247 }
@@ -250,7 +284,7 @@ func (ts *TestScript) run() {
250284 }
251285
252286 // With -v or -testwork, start log with full environment.
253- if * testWork || testing .Verbose () {
287+ if * testWork || ts . t .Verbose () {
254288 // Display environment.
255289 ts .cmdEnv (false , nil )
256290 fmt .Fprintf (& ts .log , "\n " )
0 commit comments