]> go.fuhry.dev Git - fsnotify.git/commitdiff
Run tests in random temp directories. Fixes issue #57.
authorChris Howey <chris@howey.me>
Sun, 8 Sep 2013 15:17:08 +0000 (10:17 -0500)
committerChris Howey <chris@howey.me>
Sun, 8 Sep 2013 15:17:08 +0000 (10:17 -0500)
fsnotify_symlink_test.go
fsnotify_test.go

index 795e9230a7a17d1038ee2c29aa92b09e5d151cd5..601bc08f16c2513be78dfe5cd2b083c9ad58afc8 100644 (file)
@@ -8,6 +8,7 @@ package fsnotify
 
 import (
        "os"
+       "path/filepath"
        "testing"
        "time"
 )
@@ -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 {
@@ -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")
index 269f8165303a730b7363d86be1e74062b602c894..e5346d12c6d01a19750dc1add04f8d699c4c079a 100644 (file)
@@ -5,6 +5,8 @@
 package fsnotify
 
 import (
+       "fmt"
+       "math/rand"
        "os"
        "os/exec"
        "path/filepath"
@@ -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
@@ -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 {
@@ -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)
@@ -178,7 +192,7 @@ 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 {
@@ -186,7 +200,7 @@ func TestFsnotifyMultipleCreates(t *testing.T) {
        }
        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)
@@ -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 {
@@ -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)
@@ -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
@@ -423,7 +437,7 @@ 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 {
@@ -431,7 +445,7 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) {
        }
 
        // 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)
@@ -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 {
@@ -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 {
@@ -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
@@ -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 {
@@ -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
@@ -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 {
@@ -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 {
@@ -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
@@ -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")
        }