Skip to content

Commit 1e247e4

Browse files
authored
testscript: rename testscript-update meta command to testscript (#127)
This allows us to use testscript the meta command in non-update mode. Also allow the commmand to take an -update flag.
1 parent a32363a commit 1e247e4

6 files changed

+28
-16
lines changed

testscript/testdata/testscript_update_script.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unquote scripts/testscript.txt
22
unquote testscript-new.txt
3-
testscript-update scripts
3+
testscript -update scripts
44
cmp scripts/testscript.txt testscript-new.txt
55

66
-- scripts/testscript.txt --

testscript/testdata/testscript_update_script_actual_is_file.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unquote scripts/testscript.txt
22
unquote testscript-new.txt
3-
testscript-update scripts
3+
testscript -update scripts
44
cmp scripts/testscript.txt testscript-new.txt
55

66
-- scripts/testscript.txt --

testscript/testdata/testscript_update_script_expected_not_in_archive.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
unquote scripts/testscript.txt
44
cp scripts/testscript.txt unchanged
5-
! testscript-update scripts
5+
! testscript -update scripts
66
cmp scripts/testscript.txt unchanged
77

88
-- scripts/testscript.txt --

testscript/testdata/testscript_update_script_quote.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unquote scripts/testscript.txt
22
unquote testscript-new.txt
3-
testscript-update scripts
3+
testscript -update scripts
44
cmp scripts/testscript.txt testscript-new.txt
55

66
-- scripts/testscript.txt --

testscript/testdata/testscript_update_script_stderr.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unquote scripts/testscript.txt
22
unquote testscript-new.txt
3-
testscript-update scripts
3+
testscript -update scripts
44
cmp scripts/testscript.txt testscript-new.txt
55

66
-- scripts/testscript.txt --

testscript/testscript_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package testscript
77
import (
88
"bytes"
99
"errors"
10+
"flag"
1011
"fmt"
1112
"io/ioutil"
1213
"os"
@@ -133,7 +134,8 @@ func TestScripts(t *testing.T) {
133134
// TODO set temp directory.
134135
testDeferCount := 0
135136
Run(t, Params{
136-
Dir: "testdata",
137+
UpdateScripts: os.Getenv("TESTSCRIPT_UPDATE") != "",
138+
Dir: "testdata",
137139
Cmds: map[string]func(ts *TestScript, neg bool, args []string){
138140
"setSpecialVal": setSpecialVal,
139141
"ensureSpecialVal": ensureSpecialVal,
@@ -176,12 +178,19 @@ func TestScripts(t *testing.T) {
176178
ts.Fatalf("reading %q; got %q want %q", args[0], got, want)
177179
}
178180
},
179-
"testscript-update": func(ts *TestScript, neg bool, args []string) {
181+
"testscript": func(ts *TestScript, neg bool, args []string) {
180182
// Run testscript in testscript. Oooh! Meta!
181-
if len(args) != 1 {
182-
ts.Fatalf("testscript <dir>")
183+
fset := flag.NewFlagSet("testscript", flag.ContinueOnError)
184+
fUpdate := fset.Bool("update", false, "update scripts when cmp fails")
185+
fVerbose := fset.Bool("verbose", false, "be verbose with output")
186+
if err := fset.Parse(args); err != nil {
187+
ts.Fatalf("failed to parse args for testscript: %v", err)
188+
}
189+
if fset.NArg() != 1 {
190+
ts.Fatalf("testscript [-verbose] [-update] <dir>")
183191
}
184-
t := &fakeT{ts: ts}
192+
dir := fset.Arg(0)
193+
t := &fakeT{ts: ts, verbose: *fVerbose}
185194
func() {
186195
defer func() {
187196
if err := recover(); err != nil {
@@ -191,18 +200,19 @@ func TestScripts(t *testing.T) {
191200
}
192201
}()
193202
RunT(t, Params{
194-
Dir: ts.MkAbs(args[0]),
195-
UpdateScripts: true,
203+
Dir: ts.MkAbs(dir),
204+
UpdateScripts: *fUpdate,
196205
})
197206
}()
207+
ts.stdout = strings.Replace(t.log.String(), ts.workdir, "$WORK", -1)
198208
if neg {
199209
if len(t.failMsgs) == 0 {
200-
ts.Fatalf("testscript-update unexpectedly succeeded")
210+
ts.Fatalf("testscript unexpectedly succeeded")
201211
}
202212
return
203213
}
204214
if len(t.failMsgs) > 0 {
205-
ts.Fatalf("testscript-update unexpectedly failed with errors: %q", t.failMsgs)
215+
ts.Fatalf("testscript unexpectedly failed with errors: %q", t.failMsgs)
206216
}
207217
},
208218
},
@@ -376,7 +386,9 @@ func waitFile(ts *TestScript, neg bool, args []string) {
376386

377387
type fakeT struct {
378388
ts *TestScript
389+
log bytes.Buffer
379390
failMsgs []string
391+
verbose bool
380392
}
381393

382394
var errAbort = errors.New("abort test")
@@ -393,7 +405,7 @@ func (t *fakeT) Fatal(args ...interface{}) {
393405
func (t *fakeT) Parallel() {}
394406

395407
func (t *fakeT) Log(args ...interface{}) {
396-
t.ts.Logf("testscript: %v", fmt.Sprint(args...))
408+
fmt.Fprint(&t.log, args...)
397409
}
398410

399411
func (t *fakeT) FailNow() {
@@ -405,5 +417,5 @@ func (t *fakeT) Run(name string, f func(T)) {
405417
}
406418

407419
func (t *fakeT) Verbose() bool {
408-
return false
420+
return t.verbose
409421
}

0 commit comments

Comments
 (0)