From 56caf089c888b714931c0def010ff536b9cbb652 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Fri, 26 Feb 2021 09:37:22 +0000 Subject: [PATCH] testscript: rename testscript-update meta command to testscript This allows us to use testscript the meta command in non-update mode. Also allow the commmand to take an -update flag. --- .../testdata/testscript_update_script.txt | 2 +- ...estscript_update_script_actual_is_file.txt | 2 +- ..._update_script_expected_not_in_archive.txt | 2 +- .../testscript_update_script_quote.txt | 2 +- .../testscript_update_script_stderr.txt | 2 +- testscript/testscript_test.go | 34 +++++++++++++------ 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/testscript/testdata/testscript_update_script.txt b/testscript/testdata/testscript_update_script.txt index db481ec8..b53e42fb 100644 --- a/testscript/testdata/testscript_update_script.txt +++ b/testscript/testdata/testscript_update_script.txt @@ -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 -- diff --git a/testscript/testdata/testscript_update_script_actual_is_file.txt b/testscript/testdata/testscript_update_script_actual_is_file.txt index 297c7aa4..ff1dc373 100644 --- a/testscript/testdata/testscript_update_script_actual_is_file.txt +++ b/testscript/testdata/testscript_update_script_actual_is_file.txt @@ -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 -- diff --git a/testscript/testdata/testscript_update_script_expected_not_in_archive.txt b/testscript/testdata/testscript_update_script_expected_not_in_archive.txt index cda575c9..7832159c 100644 --- a/testscript/testdata/testscript_update_script_expected_not_in_archive.txt +++ b/testscript/testdata/testscript_update_script_expected_not_in_archive.txt @@ -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 -- diff --git a/testscript/testdata/testscript_update_script_quote.txt b/testscript/testdata/testscript_update_script_quote.txt index a634b8b8..b1b7b039 100644 --- a/testscript/testdata/testscript_update_script_quote.txt +++ b/testscript/testdata/testscript_update_script_quote.txt @@ -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 -- diff --git a/testscript/testdata/testscript_update_script_stderr.txt b/testscript/testdata/testscript_update_script_stderr.txt index 66882b83..808ebe0b 100644 --- a/testscript/testdata/testscript_update_script_stderr.txt +++ b/testscript/testdata/testscript_update_script_stderr.txt @@ -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 -- diff --git a/testscript/testscript_test.go b/testscript/testscript_test.go index 0fef259c..faf1e54c 100644 --- a/testscript/testscript_test.go +++ b/testscript/testscript_test.go @@ -7,6 +7,7 @@ package testscript import ( "bytes" "errors" + "flag" "fmt" "io/ioutil" "os" @@ -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, @@ -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 ") + 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] ") } - t := &fakeT{ts: ts} + dir := fset.Arg(0) + t := &fakeT{ts: ts, verbose: *fVerbose} func() { defer func() { if err := recover(); err != nil { @@ -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) } }, }, @@ -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") @@ -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() { @@ -405,5 +417,5 @@ func (t *fakeT) Run(name string, f func(T)) { } func (t *fakeT) Verbose() bool { - return false + return t.verbose }