@@ -54,18 +54,6 @@ func (e *Env) Defer(f func()) {
5454 e .ts .Defer (f )
5555}
5656
57- // Defer arranges for f to be called at the end
58- // of the test. If Defer is called multiple times, the
59- // defers are executed in reverse order (similar
60- // to Go's defer statement)
61- func (ts * TestScript ) Defer (f func ()) {
62- old := ts .deferred
63- ts .deferred = func () {
64- defer old ()
65- f ()
66- }
67- }
68-
6957// Params holds parameters for a call to Run.
7058type Params struct {
7159 // Dir holds the name of the directory holding the scripts.
@@ -76,7 +64,8 @@ type Params struct {
7664
7765 // Setup is called, if not nil, to complete any setup required
7866 // for a test. The WorkDir and Vars fields will have already
79- // been initialized, and Cd will be the same as WorkDir.
67+ // been initialized and all the files extracted into WorkDir,
68+ // and Cd will be the same as WorkDir.
8069 // The Setup function may modify Vars and Cd as it wishes.
8170 Setup func (* Env ) error
8271
@@ -161,17 +150,17 @@ func RunT(t T, p Params) {
161150 ctxt : context .Background (),
162151 deferred : func () {},
163152 }
164- ts . setup ()
165- if ! p .TestWork {
166- defer func () {
167- removeAll ( ts . workdir )
168- if atomic . AddInt32 ( & refCount , - 1 ) == 0 {
169- // This is the last subtest to finish. Remove the
170- // parent directory too.
171- os . Remove ( testTempDir )
172- }
173- }()
174- }
153+ defer func () {
154+ if p .TestWork {
155+ return
156+ }
157+ removeAll ( ts . workdir )
158+ if atomic . AddInt32 ( & refCount , - 1 ) == 0 {
159+ // This is the last subtest to finish. Remove the
160+ // parent directory too.
161+ os . Remove ( testTempDir )
162+ }
163+ }()
175164 ts .run ()
176165 })
177166 }
@@ -210,7 +199,8 @@ type backgroundCmd struct {
210199}
211200
212201// setup sets up the test execution temporary directory and environment.
213- func (ts * TestScript ) setup () {
202+ // It returns the comment section of the txtar archive.
203+ func (ts * TestScript ) setup () string {
214204 ts .workdir = filepath .Join (ts .testTempDir , "script-" + ts .name )
215205 ts .Check (os .MkdirAll (filepath .Join (ts .workdir , "tmp" ), 0777 ))
216206 env := & Env {
@@ -237,6 +227,16 @@ func (ts *TestScript) setup() {
237227 "exe=" ,
238228 )
239229 }
230+ ts .cd = env .Cd
231+ // Unpack archive.
232+ a , err := txtar .ParseFile (ts .file )
233+ ts .Check (err )
234+ for _ , f := range a .Files {
235+ name := ts .MkAbs (ts .expand (f .Name ))
236+ ts .Check (os .MkdirAll (filepath .Dir (name ), 0777 ))
237+ ts .Check (ioutil .WriteFile (name , f .Data , 0666 ))
238+ }
239+ // Run any user-defined setup.
240240 if ts .params .Setup != nil {
241241 ts .Check (ts .params .Setup (env ))
242242 }
@@ -249,6 +249,7 @@ func (ts *TestScript) setup() {
249249 ts .envMap [envvarname (kv [:i ])] = kv [i + 1 :]
250250 }
251251 }
252+ return string (a .Comment )
252253}
253254
254255// run runs the test script.
@@ -291,14 +292,7 @@ func (ts *TestScript) run() {
291292 defer func () {
292293 ts .deferred ()
293294 }()
294- // Unpack archive.
295- a , err := txtar .ParseFile (ts .file )
296- ts .Check (err )
297- for _ , f := range a .Files {
298- name := ts .MkAbs (ts .expand (f .Name ))
299- ts .Check (os .MkdirAll (filepath .Dir (name ), 0777 ))
300- ts .Check (ioutil .WriteFile (name , f .Data , 0666 ))
301- }
295+ script := ts .setup ()
302296
303297 // With -v or -testwork, start log with full environment.
304298 if * testWork || ts .t .Verbose () {
@@ -310,7 +304,6 @@ func (ts *TestScript) run() {
310304
311305 // Run script.
312306 // See testdata/script/README for documentation of script form.
313- script := string (a .Comment )
314307Script:
315308 for script != "" {
316309 // Extract next line.
@@ -461,6 +454,18 @@ func (ts *TestScript) abbrev(s string) string {
461454 return s
462455}
463456
457+ // Defer arranges for f to be called at the end
458+ // of the test. If Defer is called multiple times, the
459+ // defers are executed in reverse order (similar
460+ // to Go's defer statement)
461+ func (ts * TestScript ) Defer (f func ()) {
462+ old := ts .deferred
463+ ts .deferred = func () {
464+ defer old ()
465+ f ()
466+ }
467+ }
468+
464469// Check calls ts.Fatalf if err != nil.
465470func (ts * TestScript ) Check (err error ) {
466471 if err != nil {
0 commit comments