mirror of
https://github.com/jimeh/go-mocktesting.git
synced 2026-02-19 03:46:40 +00:00
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:
15
t.go
15
t.go
@@ -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
|
||||
}
|
||||
|
||||
43
t_test.go
43
t_test.go
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user