Skip to content

Commit

Permalink
Merge pull request #4 from rogpeppe/004-testscript-cleanup
Browse files Browse the repository at this point in the history
testscript: minor cleanups
  • Loading branch information
rogpeppe authored Oct 4, 2018
2 parents 65e9056 + fe4414b commit 585cf0b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: go
go_import_path: github.com/rogpeppe/modinternal
go:
- "1.11"
script: go test ./...
12 changes: 7 additions & 5 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 1 addition & 33 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 585cf0b

Please sign in to comment.