diff --git a/t.go b/t.go index 640d269..0fa4de9 100644 --- a/t.go +++ b/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 +} diff --git a/t_test.go b/t_test.go index 56f5519..118b801 100644 --- a/t_test.go +++ b/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) + }) + } +}