Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testscript: rename testscript-update meta command to testscript #127

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion testscript/testdata/testscript_update_script.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unquote scripts/testscript.txt
unquote testscript-new.txt
testscript-update scripts
testscript -update scripts
cmp scripts/testscript.txt testscript-new.txt

-- scripts/testscript.txt --
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unquote scripts/testscript.txt
unquote testscript-new.txt
testscript-update scripts
testscript -update scripts
cmp scripts/testscript.txt testscript-new.txt

-- scripts/testscript.txt --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

unquote scripts/testscript.txt
cp scripts/testscript.txt unchanged
! testscript-update scripts
! testscript -update scripts
cmp scripts/testscript.txt unchanged

-- scripts/testscript.txt --
Expand Down
2 changes: 1 addition & 1 deletion testscript/testdata/testscript_update_script_quote.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unquote scripts/testscript.txt
unquote testscript-new.txt
testscript-update scripts
testscript -update scripts
cmp scripts/testscript.txt testscript-new.txt

-- scripts/testscript.txt --
Expand Down
2 changes: 1 addition & 1 deletion testscript/testdata/testscript_update_script_stderr.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unquote scripts/testscript.txt
unquote testscript-new.txt
testscript-update scripts
testscript -update scripts
cmp scripts/testscript.txt testscript-new.txt

-- scripts/testscript.txt --
Expand Down
34 changes: 23 additions & 11 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package testscript
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -133,7 +134,8 @@ func TestScripts(t *testing.T) {
// TODO set temp directory.
testDeferCount := 0
Run(t, Params{
Dir: "testdata",
UpdateScripts: os.Getenv("TESTSCRIPT_UPDATE") != "",
Dir: "testdata",
Cmds: map[string]func(ts *TestScript, neg bool, args []string){
"setSpecialVal": setSpecialVal,
"ensureSpecialVal": ensureSpecialVal,
Expand Down Expand Up @@ -176,12 +178,19 @@ func TestScripts(t *testing.T) {
ts.Fatalf("reading %q; got %q want %q", args[0], got, want)
}
},
"testscript-update": func(ts *TestScript, neg bool, args []string) {
"testscript": func(ts *TestScript, neg bool, args []string) {
// Run testscript in testscript. Oooh! Meta!
if len(args) != 1 {
ts.Fatalf("testscript <dir>")
fset := flag.NewFlagSet("testscript", flag.ContinueOnError)
fUpdate := fset.Bool("update", false, "update scripts when cmp fails")
fVerbose := fset.Bool("verbose", false, "be verbose with output")
if err := fset.Parse(args); err != nil {
ts.Fatalf("failed to parse args for testscript: %v", err)
}
if fset.NArg() != 1 {
ts.Fatalf("testscript [-verbose] [-update] <dir>")
}
t := &fakeT{ts: ts}
dir := fset.Arg(0)
t := &fakeT{ts: ts, verbose: *fVerbose}
func() {
defer func() {
if err := recover(); err != nil {
Expand All @@ -191,18 +200,19 @@ func TestScripts(t *testing.T) {
}
}()
RunT(t, Params{
Dir: ts.MkAbs(args[0]),
UpdateScripts: true,
Dir: ts.MkAbs(dir),
UpdateScripts: *fUpdate,
})
}()
ts.stdout = strings.Replace(t.log.String(), ts.workdir, "$WORK", -1)
if neg {
if len(t.failMsgs) == 0 {
ts.Fatalf("testscript-update unexpectedly succeeded")
ts.Fatalf("testscript unexpectedly succeeded")
}
return
}
if len(t.failMsgs) > 0 {
ts.Fatalf("testscript-update unexpectedly failed with errors: %q", t.failMsgs)
ts.Fatalf("testscript unexpectedly failed with errors: %q", t.failMsgs)
}
},
},
Expand Down Expand Up @@ -376,7 +386,9 @@ func waitFile(ts *TestScript, neg bool, args []string) {

type fakeT struct {
ts *TestScript
log bytes.Buffer
failMsgs []string
verbose bool
}

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

func (t *fakeT) Log(args ...interface{}) {
t.ts.Logf("testscript: %v", fmt.Sprint(args...))
fmt.Fprint(&t.log, args...)
}

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

func (t *fakeT) Verbose() bool {
return false
return t.verbose
}