Skip to content

Commit a2d2540

Browse files
authored
testscript: let Setup see unarchived txtar files (#51)
This allows a setup step to see the unarchived files (for example we can use this to start a go module proxy server in Setup that serves modules from the testscript contents).
1 parent cba92fb commit a2d2540

File tree

3 files changed

+63
-34
lines changed

3 files changed

+63
-34
lines changed

testscript/testdata/setupfiles.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# check that the Setup function saw the unarchived files,
2+
# including the temp directory that's always created.
3+
setup-filenames a b tmp
4+
5+
-- a --
6+
-- b/c --

testscript/testscript.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7058
type 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)
314307
Script:
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.
465470
func (ts *TestScript) Check(err error) {
466471
if err != nil {

testscript/testscript_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/signal"
1212
"path/filepath"
13+
"reflect"
1314
"strconv"
1415
"testing"
1516
"time"
@@ -69,6 +70,7 @@ func TestCRLFInput(t *testing.T) {
6970
func TestScripts(t *testing.T) {
7071
// TODO set temp directory.
7172
testDeferCount := 0
73+
var setupFilenames []string
7274
Run(t, Params{
7375
Dir: "testdata",
7476
Cmds: map[string]func(ts *TestScript, neg bool, args []string){
@@ -86,6 +88,22 @@ func TestScripts(t *testing.T) {
8688
testDeferCount--
8789
})
8890
},
91+
"setup-filenames": func(ts *TestScript, neg bool, args []string) {
92+
if !reflect.DeepEqual(args, setupFilenames) {
93+
ts.Fatalf("setup did not see expected files; got %q want %q", setupFilenames, args)
94+
}
95+
},
96+
},
97+
Setup: func(env *Env) error {
98+
infos, err := ioutil.ReadDir(env.WorkDir)
99+
if err != nil {
100+
return fmt.Errorf("cannot read workdir: %v", err)
101+
}
102+
setupFilenames = nil
103+
for _, info := range infos {
104+
setupFilenames = append(setupFilenames, info.Name())
105+
}
106+
return nil
89107
},
90108
})
91109
if testDeferCount != 0 {

0 commit comments

Comments
 (0)