fix(mocktesting): add missing TempDirs() function

Without this function there was no way to inspect the list of temporary
directories created by calls to TempDir(), also making it impossible to
verify that TempDir() has been called.
This commit is contained in:
2021-11-22 02:02:42 +00:00
parent d745f3907e
commit 108ae0ec9a
2 changed files with 58 additions and 0 deletions

15
t.go
View File

@@ -424,3 +424,18 @@ func (t *T) Subtests() []*T {
return t.subtests
}
// TempDirs returns a string slice of temporary directories created by
// TempDir().
func (t *T) TempDirs() []string {
if t.tempdirs == nil {
t.mux.Lock()
t.tempdirs = []string{}
t.mux.Unlock()
}
t.mux.RLock()
defer t.mux.RUnlock()
return t.tempdirs
}

View File

@@ -2458,3 +2458,46 @@ func TestT_Subtests(t *testing.T) {
})
}
}
func TestT_TempDirs(t *testing.T) {
type fields struct {
tempdirs []string
}
tests := []struct {
name string
fields fields
want []string
}{
{
name: "nil",
fields: fields{tempdirs: nil},
want: []string{},
},
{
name: "empty",
fields: fields{tempdirs: []string{}},
want: []string{},
},
{
name: "one dir",
fields: fields{tempdirs: []string{"/tmp/foo"}},
want: []string{"/tmp/foo"},
},
{
name: "many dirs",
fields: fields{
tempdirs: []string{"/tmp/foo", "/tmp/foo", "/tmp/nope"},
},
want: []string{"/tmp/foo", "/tmp/foo", "/tmp/nope"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mt := &T{tempdirs: tt.fields.tempdirs}
got := mt.TempDirs()
assert.Equal(t, tt.want, got)
})
}
}