Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Run tests in random temp directories. Fixes issue #57.
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Sep 8, 2013
1 parent 7beb451 commit 2c4a662
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
5 changes: 3 additions & 2 deletions fsnotify_symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package fsnotify

import (
"os"
"path/filepath"
"testing"
"time"
)
Expand All @@ -19,7 +20,7 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
t.Fatalf("Watcher.Watch() failed: %s", err)
}

if os.Symlink("_test/zzz", "_test/zzznew") != nil {
if os.Symlink(filepath.Join(testDir, "zzz"), filepath.Join(testDir, "zzznew")) != nil {
t.Fatalf("Failed to create bogus symlink: %s", err)
}
t.Logf("Created bogus symlink")
Expand Down
72 changes: 43 additions & 29 deletions fsnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package fsnotify

import (
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand All @@ -14,6 +16,18 @@ import (
"time"
)

var r *rand.Rand

func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func testTempDir() string {
osTempDir := os.TempDir()
randDir := fmt.Sprintf("%d", r.Int())
return filepath.Join(osTempDir, randDir)
}

// An atomic counter
type counter struct {
val int32
Expand Down Expand Up @@ -45,8 +59,8 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
}
}()

const testDir string = "_test"
const testDirToMoveFiles string = "_test2"
var testDir string = testTempDir()
var testDirToMoveFiles string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand All @@ -60,8 +74,8 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
}
defer os.RemoveAll(testDirToMoveFiles)

const testFile string = "_test/TestFsnotifySeq.testfile"
const testFileRenamed string = "_test2/TestFsnotifySeqRename.testfile"
var testFile string = filepath.Join(testDir, "TestFsnotifySeq.testfile")
var testFileRenamed string = filepath.Join(testDirToMoveFiles, "TestFsnotifySeqRename.testfile")

// Add a watch for testDir
err = watcher.Watch(testDir)
Expand Down Expand Up @@ -178,15 +192,15 @@ func TestFsnotifyMultipleCreates(t *testing.T) {
}
}()

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
t.Fatalf("failed to create test directory: %s", err)
}
defer os.RemoveAll(testDir)

const testFile string = "_test/TestFsnotifySeq.testfile"
var testFile string = filepath.Join(testDir, "TestFsnotifySeq.testfile")

// Add a watch for testDir
err = watcher.Watch(testDir)
Expand Down Expand Up @@ -308,7 +322,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand All @@ -318,7 +332,7 @@ func TestFsnotifyDirOnly(t *testing.T) {

// Create a file before watching directory
// This should NOT add any events to the fsnotify event queue
const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile"
var testFileAlreadyExists string = filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
{
var f *os.File
f, err = os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666)
Expand All @@ -342,7 +356,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
}
}()

const testFile string = "_test/TestFsnotifyDirOnly.testfile"
var testFile string = filepath.Join(testDir, "TestFsnotifyDirOnly.testfile")

// Receive events on the event channel on a separate goroutine
eventstream := watcher.Event
Expand Down Expand Up @@ -423,15 +437,15 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) {
}
defer watcher.Close()

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
t.Fatalf("failed to create test directory: %s", err)
}

// Create a file before watching directory
const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile"
var testFileAlreadyExists string = filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
{
var f *os.File
f, err = os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666)
Expand Down Expand Up @@ -495,10 +509,10 @@ func TestFsnotifySubDir(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
const testFile1 string = "_test/TestFsnotifyFile1.testfile"
const testSubDir string = "_test/sub"
const testSubDirFile string = "_test/sub/TestFsnotifyFile1.testfile"
var testDir string = testTempDir()
var testFile1 string = filepath.Join(testDir, "TestFsnotifyFile1.testfile")
var testSubDir string = filepath.Join(testDir, "sub")
var testSubDirFile string = filepath.Join(testDir, "sub/TestFsnotifyFile1.testfile")

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand Down Expand Up @@ -600,7 +614,7 @@ func TestFsnotifyRename(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand All @@ -621,8 +635,8 @@ func TestFsnotifyRename(t *testing.T) {
}
}()

const testFile string = "_test/TestFsnotifyEvents.testfile"
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
var testFile string = filepath.Join(testDir, "TestFsnotifyEvents.testfile")
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")

// Receive events on the event channel on a separate goroutine
eventstream := watcher.Event
Expand Down Expand Up @@ -694,8 +708,8 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
const testDirFrom string = "_testfrom"
var testDir string = testTempDir()
var testDirFrom string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand All @@ -722,8 +736,8 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
}
}()

const testFile string = "_testfrom/TestFsnotifyEvents.testfile"
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
var testFile string = filepath.Join(testDirFrom, "TestFsnotifyEvents.testfile")
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")

// Receive events on the event channel on a separate goroutine
eventstream := watcher.Event
Expand Down Expand Up @@ -790,11 +804,11 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
const testDirFrom string = "_testfrom"
var testDir string = testTempDir()
var testDirFrom string = testTempDir()

const testFile string = "_testfrom/TestFsnotifyEvents.testfile"
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
var testFile string = filepath.Join(testDirFrom, "TestFsnotifyEvents.testfile")
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand Down Expand Up @@ -892,7 +906,7 @@ func TestFsnotifyAttrib(t *testing.T) {
t.Fatalf("NewWatcher() failed: %s", err)
}

const testDir string = "_test"
var testDir string = testTempDir()

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
Expand All @@ -907,7 +921,7 @@ func TestFsnotifyAttrib(t *testing.T) {
}
}()

const testFile string = "_test/TestFsnotifyAttrib.testfile"
var testFile string = filepath.Join(testDir, "TestFsnotifyAttrib.testfile")

// Receive events on the event channel on a separate goroutine
eventstream := watcher.Event
Expand Down Expand Up @@ -987,7 +1001,7 @@ func TestFsnotifyClose(t *testing.T) {
t.Fatal("double Close() test failed: second Close() call didn't return")
}

err := watcher.Watch("_test")
err := watcher.Watch(testTempDir())
if err == nil {
t.Fatal("expected error on Watch() after Close(), got nil")
}
Expand Down

0 comments on commit 2c4a662

Please sign in to comment.