From e97fb1df6c1ed74b1e927ba29e7a1439666513a8 Mon Sep 17 00:00:00 2001 From: Jo Vandeginste Date: Mon, 21 Dec 2020 15:10:35 +0100 Subject: [PATCH] Convert filet to support parallel execution We create a new list of files per TestReporter. This needed a map, and thus some helper functions to help with locking around the map. This however changes the way you can access the `Files` array. You now need to access it through the map key (see the README changes) Fixes #5 Signed-off-by: Jo Vandeginste --- README.md | 10 +++++++--- filet.go | 51 +++++++++++++++++++++++++++++++++++++++++---------- filet_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a41c290..866498f 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,15 @@ func TestFoo(t *testing.T) { `CleanUp` will call `t.Error` if something goes wrong when removing the file. -You can also access the `Files` itself if you want to add a specificly -named file to the cleanup list. +You can also access the `Files` itself if you want to check the content, or add +a specificly named file to the cleanup list. ``` -filet.Files = append(filet.Files, "path/to/my/named/file") +fmt.Printf("%#v\n", filet.Files(t)) + +filet.Append(t, "path/to/my/named/file") + +fmt.Printf("%#v\n", filet.Files(t)) ``` ## Helpers diff --git a/filet.go b/filet.go index b08765f..3abd759 100644 --- a/filet.go +++ b/filet.go @@ -2,9 +2,11 @@ package filet import ( "bytes" - "github.com/spf13/afero" "os" "path/filepath" + "sync" + + "github.com/spf13/afero" ) // TestReporter can be used to report test failures. It is satisfied by the standard library's *testing.T. @@ -12,9 +14,9 @@ type TestReporter interface { Error(args ...interface{}) } -// Files keeps track of files that we've used so we can clean up. -var Files []string +var files = map[TestReporter][]string{} var appFs = afero.NewOsFs() +var lock = sync.RWMutex{} /* TmpDir Creates a tmp directory for us to use. @@ -25,7 +27,7 @@ func TmpDir(t TestReporter, dir string) string { t.Error("Failed to create the tmpDir: "+name, err) } - Files = append(Files, name) + Append(t, name) return name } @@ -41,7 +43,7 @@ func TmpFile(t TestReporter, dir string, content string) afero.File { } file.WriteString(content) - Files = append(Files, file.Name()) + Append(t, file.Name()) return file } @@ -59,7 +61,7 @@ func File(t TestReporter, path string, content string) afero.File { } file.WriteString(content) - Files = append(Files, file.Name()) + Append(t, file.Name()) return file } @@ -82,13 +84,16 @@ CleanUp removes all files in our test registry and calls `t.Error` if something wrong. */ func CleanUp(t TestReporter) { - for _, path := range Files { + lock.Lock() + paths := files[t] + delete(files, t) + lock.Unlock() + + for _, path := range paths { if err := appFs.RemoveAll(path); err != nil { t.Error(appFs.Name(), err) } } - - Files = make([]string, 0) } /* @@ -110,4 +115,30 @@ something goes wrong while checking. func DirContains(t TestReporter, dir string, path string) bool { fullPath := filepath.Join(dir, path) return Exists(t, fullPath) -} \ No newline at end of file +} + +/* +Files keeps track of files that we've used so we can clean up. +*/ +func Files(t TestReporter) []string { + lock.RLock() + defer lock.RUnlock() + + return files[t] +} + +/* +Append adds a path to the files we need to clean up. +*/ +func Append(t TestReporter, path string) { + lock.Lock() + defer lock.Unlock() + + v, ok := files[t] + + if ok { + files[t] = append(v, path) + } else { + files[t] = []string{path} + } +} diff --git a/filet_test.go b/filet_test.go index 5c4facf..ff647ec 100644 --- a/filet_test.go +++ b/filet_test.go @@ -3,12 +3,15 @@ package filet import ( "path/filepath" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestTmpDir(t *testing.T) { + t.Parallel() + defer CleanUp(t) path := TmpDir(t, "") @@ -17,6 +20,8 @@ func TestTmpDir(t *testing.T) { } func TestTmpFile(t *testing.T) { + t.Parallel() + defer CleanUp(t) // Test that file is actually created @@ -32,6 +37,8 @@ func TestTmpFile(t *testing.T) { } func TestFile(t *testing.T) { + t.Parallel() + defer CleanUp(t) // Test that file is actually created @@ -45,6 +52,8 @@ func TestFile(t *testing.T) { } func TestFileSays(t *testing.T) { + t.Parallel() + defer CleanUp(t) file := TmpFile(t, "", "Gandhi") @@ -60,6 +69,8 @@ func TestFileSays(t *testing.T) { } func TestExists(t *testing.T) { + t.Parallel() + defer CleanUp(t) file := TmpFile(t, "", "I exist") @@ -70,6 +81,8 @@ func TestExists(t *testing.T) { } func TestDirContains(t *testing.T) { + t.Parallel() + defer CleanUp(t) dir := TmpDir(t, "") @@ -81,10 +94,9 @@ func TestDirContains(t *testing.T) { } func TestCleanUp(t *testing.T) { - defer CleanUp(t) // Kind of problematic. + t.Parallel() - // Clear Files - Files = make([]string, 0) + defer CleanUp(t) // Kind of problematic. // Create test files dir := TmpDir(t, "") @@ -100,3 +112,28 @@ func TestCleanUp(t *testing.T) { assert.Equal(t, Exists(t, newDir), true, "New files still exist after CleanUp") } + +func TestCleanUpEmpty(t *testing.T) { + t.Parallel() + + defer CleanUp(t) +} + +func TestMain(t *testing.T) { + theTest(t) +} + +func TestMain2(t *testing.T) { + theTest(t) +} + +func theTest(t *testing.T) { + t.Parallel() + + defer CleanUp(t) + + TmpDir(t, "") + + time.Sleep(20 * time.Millisecond) + assert.Len(t, Files(t), 1) +}