From fe4414b8aec319f5ae0d40a97cdb7c05ac3b16ed Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Thu, 4 Oct 2018 17:57:58 +0100 Subject: [PATCH] testscript: minor cleanups Move a stray test into the test file. Clean up some docs. --- .travis.yml | 5 +++++ testscript/doc.go | 12 +++++++----- testscript/testscript.go | 34 +--------------------------------- testscript/testscript_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..1acd8eb3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: go +go_import_path: github.com/rogpeppe/modinternal +go: + - "1.11" +script: go test ./... diff --git a/testscript/doc.go b/testscript/doc.go index 1469c622..59e67297 100644 --- a/testscript/doc.go +++ b/testscript/doc.go @@ -23,13 +23,15 @@ To run a specific script foo.txt where TestName is the name of the test that Run is called from. -To define an executable command that can be run as part of the script, -the command should be registered inside a TestMain function before calling -M.Run. For example: +To define an executable command (or several) that can be run as part of the script, +call RunMain with the functions that implement the command's functionality. +The command functions will be called in a separate process, so are +free to mutate global variables without polluting the top level test binary. func TestMain(m *testing.M) { - testscript.RegisterCommand("testscript", testscriptMain) - os.Exit(m.Run()) + os.Exit(testscript.RunMain(m, map[string] func() int{ + "testscript": testscriptMain, + })) } In general script files should have short names: a few words, not whole sentences. diff --git a/testscript/testscript.go b/testscript/testscript.go index a5b7675d..7781eb3b 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -40,6 +40,7 @@ type Env struct { Cd string } +// Params holds parameters for a call to Run. type Params struct { // Dir holds the name of the directory holding the scripts. // All files in the directory with a .txt suffix will be considered @@ -538,39 +539,6 @@ func diff(text1, text2 string) string { return buf.String() } -var diffTests = []struct { - text1 string - text2 string - diff string -}{ - {"a b c", "a b d e f", "a b -c +d +e +f"}, - {"", "a b c", "+a +b +c"}, - {"a b c", "", "-a -b -c"}, - {"a b c", "d e f", "-a -b -c +d +e +f"}, - {"a b c d e f", "a b d e f", "a b -c d e f"}, - {"a b c e f", "a b c d e f", "a b c +d e f"}, -} - -func TestDiff(t *testing.T) { - for _, tt := range diffTests { - // Turn spaces into \n. - text1 := strings.Replace(tt.text1, " ", "\n", -1) - if text1 != "" { - text1 += "\n" - } - text2 := strings.Replace(tt.text2, " ", "\n", -1) - if text2 != "" { - text2 += "\n" - } - out := diff(text1, text2) - // Cut final \n, cut spaces, turn remaining \n into spaces. - out = strings.Replace(strings.Replace(strings.TrimSuffix(out, "\n"), " ", "", -1), "\n", " ", -1) - if out != tt.diff { - t.Errorf("diff(%q, %q) = %q, want %q", text1, text2, out, tt.diff) - } - } -} - func removeAll(dir string) error { // module cache has 0444 directories; // make them writable in order to remove content. diff --git a/testscript/testscript_test.go b/testscript/testscript_test.go index cf545d71..89e87f99 100644 --- a/testscript/testscript_test.go +++ b/testscript/testscript_test.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "strconv" + "strings" "testing" ) @@ -35,3 +36,36 @@ func TestSimple(t *testing.T) { }) // TODO check that the temp directory has been removed. } + +var diffTests = []struct { + text1 string + text2 string + diff string +}{ + {"a b c", "a b d e f", "a b -c +d +e +f"}, + {"", "a b c", "+a +b +c"}, + {"a b c", "", "-a -b -c"}, + {"a b c", "d e f", "-a -b -c +d +e +f"}, + {"a b c d e f", "a b d e f", "a b -c d e f"}, + {"a b c e f", "a b c d e f", "a b c +d e f"}, +} + +func TestDiff(t *testing.T) { + for _, tt := range diffTests { + // Turn spaces into \n. + text1 := strings.Replace(tt.text1, " ", "\n", -1) + if text1 != "" { + text1 += "\n" + } + text2 := strings.Replace(tt.text2, " ", "\n", -1) + if text2 != "" { + text2 += "\n" + } + out := diff(text1, text2) + // Cut final \n, cut spaces, turn remaining \n into spaces. + out = strings.Replace(strings.Replace(strings.TrimSuffix(out, "\n"), " ", "", -1), "\n", " ", -1) + if out != tt.diff { + t.Errorf("diff(%q, %q) = %q, want %q", text1, text2, out, tt.diff) + } + } +}