Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert filet to support parallel execution #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 41 additions & 10 deletions filet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ 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.
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.
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}

/*
Expand All @@ -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)
}
}

/*
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}
}
}
43 changes: 40 additions & 3 deletions filet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -45,6 +52,8 @@ func TestFile(t *testing.T) {
}

func TestFileSays(t *testing.T) {
t.Parallel()

defer CleanUp(t)

file := TmpFile(t, "", "Gandhi")
Expand All @@ -60,6 +69,8 @@ func TestFileSays(t *testing.T) {
}

func TestExists(t *testing.T) {
t.Parallel()

defer CleanUp(t)

file := TmpFile(t, "", "I exist")
Expand All @@ -70,6 +81,8 @@ func TestExists(t *testing.T) {
}

func TestDirContains(t *testing.T) {
t.Parallel()

defer CleanUp(t)

dir := TmpDir(t, "")
Expand All @@ -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, "")
Expand All @@ -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)
}