Skip to content

Commit bf08ea5

Browse files
authored
testscript: fail a test when a testscript Dir contains no scripts (#85)
This can happen when either the Dir does not exist, or it is empty.
1 parent d89504f commit bf08ea5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

testscript/testscript.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ func (t tshim) Verbose() bool {
152152
// RunT is like Run but uses an interface type instead of the concrete *testing.T
153153
// type to make it possible to use testscript functionality outside of go test.
154154
func RunT(t T, p Params) {
155-
files, err := filepath.Glob(filepath.Join(p.Dir, "*.txt"))
155+
glob := filepath.Join(p.Dir, "*.txt")
156+
files, err := filepath.Glob(glob)
156157
if err != nil {
157158
t.Fatal(err)
158159
}
160+
if len(files) == 0 {
161+
t.Fatal(fmt.Sprintf("no scripts found matching glob: %v", glob))
162+
}
159163
testTempDir, err := ioutil.TempDir(os.Getenv("GOTMPDIR"), "go-test-script")
160164
if err != nil {
161165
t.Fatal(err)

testscript/testscript_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ func TestTestwork(t *testing.T) {
202202
}
203203
}
204204

205+
// TestBadDir verifies that invoking testscript with a directory that either
206+
// does not exist or that contains no *.txt scripts fails the test
207+
func TestBadDir(t *testing.T) {
208+
ft := new(fakeT)
209+
func() {
210+
defer func() {
211+
if err := recover(); err != nil {
212+
if err != errAbort {
213+
panic(err)
214+
}
215+
}
216+
}()
217+
RunT(ft, Params{
218+
Dir: "thiswillnevermatch",
219+
})
220+
}()
221+
wantCount := 1
222+
if got := len(ft.failMsgs); got != wantCount {
223+
t.Fatalf("expected %v fail message; got %v", wantCount, got)
224+
}
225+
wantMsg := regexp.MustCompile(`no scripts found matching glob: thiswillnevermatch[/\\]\*\.txt`)
226+
if got := ft.failMsgs[0]; !wantMsg.MatchString(got) {
227+
t.Fatalf("expected msg to match `%v`; got:\n%v", wantMsg, got)
228+
}
229+
}
230+
205231
func setSpecialVal(ts *TestScript, neg bool, args []string) {
206232
ts.Setenv("SPECIALVAL", "42")
207233
}

0 commit comments

Comments
 (0)