From 108ae0ec9a7d26e214bb81057d3fac30b5b24325 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2021 02:02:42 +0000 Subject: [PATCH] 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. --- t.go | 15 +++++++++++++++ t_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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) + }) + } +}