From: Chris Howey Date: Thu, 7 Feb 2013 04:01:39 +0000 (-0600) Subject: Fix tests for Windows : '\\' and '/' path seperators X-Git-Tag: v1.7.2~381 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=92144df54a8649958bda8640adedf1e2107d2691;p=fsnotify.git Fix tests for Windows : '\\' and '/' path seperators --- diff --git a/fsnotify_test.go b/fsnotify_test.go index d57d6d5..0ae9ed9 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -1,18 +1,19 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. package fsnotify import ( "os" "os/exec" + "path/filepath" "sync/atomic" "testing" "time" ) -// An atomic counter +// An atomic counter type counter struct { val int32 } @@ -30,13 +31,13 @@ func (c *counter) value() int32 { } func TestFsnotifyMultipleOperations(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -46,13 +47,13 @@ func TestFsnotifyMultipleOperations(t *testing.T) { const testDir string = "_test" const testDirToMoveFiles string = "_test2" - // Create directory to watch + // 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) - // Create directory to that's not watched + // Create directory to that's not watched if err := os.Mkdir(testDirToMoveFiles, 0777); err != nil { t.Fatalf("Failed to create test directory: %s", err) } @@ -61,19 +62,19 @@ func TestFsnotifyMultipleOperations(t *testing.T) { const testFile string = "_test/TestFsnotifySeq.testfile" const testFileRenamed string = "_test2/TestFsnotifySeqRename.testfile" - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var createReceived, modifyReceived, deleteReceived, renameReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { t.Logf("event received: %s", event) if event.IsDelete() { deleteReceived.increment() @@ -94,8 +95,8 @@ func TestFsnotifyMultipleOperations(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -108,7 +109,7 @@ func TestFsnotifyMultipleOperations(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete cmd := exec.Command("mv", testFile, testFileRenamed) err = cmd.Run() @@ -116,7 +117,7 @@ func TestFsnotifyMultipleOperations(t *testing.T) { t.Fatalf("rename failed: %s", err) } - // Modify the file outside of the watched dir + // Modify the file outside of the watched dir f, err = os.Open(testFileRenamed) if err != nil { t.Fatalf("open test renamed file failed: %s", err) @@ -125,17 +126,17 @@ func TestFsnotifyMultipleOperations(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // Recreate the file that was moved + // Recreate the file that was moved f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) } f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) cReceived := createReceived.value() if cReceived != 2 { @@ -151,7 +152,7 @@ func TestFsnotifyMultipleOperations(t *testing.T) { t.Fatalf("incorrect number of rename+delete events received after 500 ms (%d vs %d)", rReceived+dReceived, 1) } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -164,13 +165,13 @@ func TestFsnotifyMultipleOperations(t *testing.T) { } func TestFsnotifyMultipleCreates(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -179,7 +180,7 @@ func TestFsnotifyMultipleCreates(t *testing.T) { const testDir string = "_test" - // Create directory to watch + // Create directory to watch if err := os.Mkdir(testDir, 0777); err != nil { t.Fatalf("Failed to create test directory: %s", err) } @@ -187,19 +188,19 @@ func TestFsnotifyMultipleCreates(t *testing.T) { const testFile string = "_test/TestFsnotifySeq.testfile" - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var createReceived, modifyReceived, deleteReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { t.Logf("event received: %s", event) if event.IsDelete() { deleteReceived.increment() @@ -217,8 +218,8 @@ func TestFsnotifyMultipleCreates(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -231,21 +232,21 @@ func TestFsnotifyMultipleCreates(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete os.Remove(testFile) - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // Recreate the file + // Recreate the file f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) } f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // Modify + // Modify f, err = os.OpenFile(testFile, os.O_WRONLY, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) @@ -257,9 +258,9 @@ func TestFsnotifyMultipleCreates(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // Modify + // Modify f, err = os.OpenFile(testFile, os.O_WRONLY, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) @@ -271,9 +272,9 @@ func TestFsnotifyMultipleCreates(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) cReceived := createReceived.value() if cReceived != 2 { @@ -288,7 +289,7 @@ func TestFsnotifyMultipleCreates(t *testing.T) { t.Fatalf("incorrect number of rename+delete events received after 500 ms (%d vs %d)", dReceived, 1) } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -301,7 +302,7 @@ func TestFsnotifyMultipleCreates(t *testing.T) { } func TestFsnotifyDirOnly(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -309,14 +310,14 @@ func TestFsnotifyDirOnly(t *testing.T) { const testDir string = "_test" - // Create directory to watch + // 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) - // Create a file before watching directory - // This should NOT add any events to the fsnotify event queue + // Create a file before watching directory + // This should NOT add any events to the fsnotify event queue const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile" { var f *os.File @@ -328,13 +329,13 @@ func TestFsnotifyDirOnly(t *testing.T) { f.Close() } - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -343,14 +344,14 @@ func TestFsnotifyDirOnly(t *testing.T) { const testFile string = "_test/TestFsnotifyDirOnly.testfile" - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var createReceived, modifyReceived, deleteReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile || event.Name == testFileAlreadyExists { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileAlreadyExists) { t.Logf("event received: %s", event) if event.IsDelete() { deleteReceived.increment() @@ -368,8 +369,8 @@ func TestFsnotifyDirOnly(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -382,12 +383,12 @@ func TestFsnotifyDirOnly(t *testing.T) { f.Sync() f.Close() - time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete + time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete os.Remove(testFile) os.Remove(testFileAlreadyExists) - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) cReceived := createReceived.value() if cReceived != 1 { @@ -402,7 +403,7 @@ func TestFsnotifyDirOnly(t *testing.T) { t.Fatalf("incorrect number of delete events received after 500 ms (%d vs %d)", dReceived, 2) } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -415,7 +416,7 @@ func TestFsnotifyDirOnly(t *testing.T) { } func TestFsnotifyDeleteWatchedDir(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -424,12 +425,12 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) { const testDir string = "_test" - // Create directory to watch + // 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 + // Create a file before watching directory const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile" { var f *os.File @@ -441,32 +442,32 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) { f.Close() } - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Add a watch for testFile + // Add a watch for testFile err = watcher.Watch(testFileAlreadyExists) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) } }() - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var deleteReceived counter go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFileAlreadyExists { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFileAlreadyExists) { t.Logf("event received: %s", event) if event.IsDelete() { deleteReceived.increment() @@ -479,7 +480,7 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) { os.RemoveAll(testDir) - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) dReceived := deleteReceived.value() if dReceived < 2 { @@ -488,7 +489,7 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) { } func TestFsnotifySubDir(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -499,27 +500,27 @@ func TestFsnotifySubDir(t *testing.T) { const testSubDir string = "_test/sub" const testSubDirFile string = "_test/sub/TestFsnotifyFile1.testfile" - // Create directory to watch + // 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) - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) } }() - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var createReceived, deleteReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testSubDir || event.Name == testFile1 { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testSubDir) || event.Name == filepath.Clean(testFile1) { t.Logf("event received: %s", event) if event.IsCreate() { createReceived.increment() @@ -534,18 +535,18 @@ func TestFsnotifySubDir(t *testing.T) { done <- true }() - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Create sub-directory + // Create sub-directory if err := os.Mkdir(testSubDir, 0777); err != nil { t.Fatalf("Failed to create test sub-directory: %s", err) } - // Create a file + // Create a file var f *os.File f, err = os.OpenFile(testFile1, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -554,7 +555,7 @@ func TestFsnotifySubDir(t *testing.T) { f.Sync() f.Close() - // Create a file (Should not see this! we are not watching subdir) + // Create a file (Should not see this! we are not watching subdir) var fs *os.File fs, err = os.OpenFile(testSubDirFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -563,11 +564,11 @@ func TestFsnotifySubDir(t *testing.T) { fs.Sync() fs.Close() - // Make sure receive deletes for both file and sub-directory + // Make sure receive deletes for both file and sub-directory os.RemoveAll(testSubDir) os.Remove(testFile1) - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) cReceived := createReceived.value() if cReceived != 2 { @@ -578,7 +579,7 @@ func TestFsnotifySubDir(t *testing.T) { t.Fatalf("incorrect number of delete events received after 500 ms (%d vs %d)", dReceived, 2) } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -591,7 +592,7 @@ func TestFsnotifySubDir(t *testing.T) { } func TestFsnotifyRename(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -599,19 +600,19 @@ func TestFsnotifyRename(t *testing.T) { const testDir string = "_test" - // Create directory to watch + // 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) - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -621,14 +622,14 @@ func TestFsnotifyRename(t *testing.T) { const testFile string = "_test/TestFsnotifyEvents.testfile" const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed" - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var renameReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile || event.Name == testFileRenamed { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileRenamed) { if event.IsRename() { renameReceived.increment() } @@ -640,8 +641,8 @@ func TestFsnotifyRename(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -653,7 +654,7 @@ func TestFsnotifyRename(t *testing.T) { f.Sync() f.Close() - // Add a watch for testFile + // Add a watch for testFile err = watcher.Watch(testFile) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) @@ -665,13 +666,13 @@ func TestFsnotifyRename(t *testing.T) { t.Fatalf("rename failed: %s", err) } - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) if renameReceived.value() == 0 { t.Fatal("fsnotify rename events have not been received after 500 ms") } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -686,7 +687,7 @@ func TestFsnotifyRename(t *testing.T) { } func TestFsnotifyRenameToCreate(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -695,25 +696,25 @@ func TestFsnotifyRenameToCreate(t *testing.T) { const testDir string = "_test" const testDirFrom string = "_testfrom" - // Create directory to watch + // 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) - // Create directory to get file + // Create directory to get file if err := os.Mkdir(testDirFrom, 0777); err != nil { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll(testDirFrom) - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -723,14 +724,14 @@ func TestFsnotifyRenameToCreate(t *testing.T) { const testFile string = "_testfrom/TestFsnotifyEvents.testfile" const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed" - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var createReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile || event.Name == testFileRenamed { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileRenamed) { if event.IsCreate() { createReceived.increment() } @@ -742,8 +743,8 @@ func TestFsnotifyRenameToCreate(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -758,13 +759,13 @@ func TestFsnotifyRenameToCreate(t *testing.T) { t.Fatalf("rename failed: %s", err) } - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) if createReceived.value() == 0 { t.Fatal("fsnotify create events have not been received after 500 ms") } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -779,7 +780,7 @@ func TestFsnotifyRenameToCreate(t *testing.T) { } func TestFsnotifyRenameToOverwrite(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -791,19 +792,19 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) { const testFile string = "_testfrom/TestFsnotifyEvents.testfile" const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed" - // Create directory to watch + // 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) - // Create directory to get file + // Create directory to get file if err := os.Mkdir(testDirFrom, 0777); err != nil { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll(testDirFrom) - // Create a file + // Create a file var fr *os.File fr, err = os.OpenFile(testFileRenamed, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -812,27 +813,27 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) { fr.Sync() fr.Close() - // Add a watch for testDir + // Add a watch for testDir err = watcher.Watch(testDir) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) } - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) } }() - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var eventReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testFileRenamed { + // Only count relevant events + if event.Name == filepath.Clean(testFileRenamed) { eventReceived.increment() t.Logf("event received: %s", event) } else { @@ -842,8 +843,8 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -858,13 +859,13 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) { t.Fatalf("rename failed: %s", err) } - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) if eventReceived.value() == 0 { t.Fatal("fsnotify events have not been received after 500 ms") } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -879,7 +880,7 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) { } func TestFsnotifyAttrib(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -887,13 +888,13 @@ func TestFsnotifyAttrib(t *testing.T) { const testDir string = "_test" - // Create directory to watch + // 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) - // Receive errors on the error channel on a separate goroutine + // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) @@ -902,14 +903,14 @@ func TestFsnotifyAttrib(t *testing.T) { const testFile string = "_test/TestFsnotifyAttrib.testfile" - // Receive events on the event channel on a separate goroutine + // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var attribReceived counter done := make(chan bool) go func() { for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile { + // Only count relevant events + if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { if event.IsModify() { attribReceived.increment() } @@ -921,8 +922,8 @@ func TestFsnotifyAttrib(t *testing.T) { done <- true }() - // Create a file - // This should add at least one event to the fsnotify event queue + // Create a file + // This should add at least one event to the fsnotify event queue var f *os.File f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { @@ -934,7 +935,7 @@ func TestFsnotifyAttrib(t *testing.T) { f.Sync() f.Close() - // Add a watch for testFile + // Add a watch for testFile err = watcher.Watch(testFile) if err != nil { t.Fatalf("Watcher.Watch() failed: %s", err) @@ -946,13 +947,13 @@ func TestFsnotifyAttrib(t *testing.T) { t.Fatalf("chmod failed: %s", err) } - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) if attribReceived.value() == 0 { t.Fatal("fsnotify attribute events have not received after 500 ms") } - // Try closing the fsnotify instance + // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() t.Log("waiting for the event channel to become closed...") @@ -976,7 +977,7 @@ func TestFsnotifyClose(t *testing.T) { atomic.StoreInt32(&done, 1) }() - time.Sleep(50e6) // 50 ms + time.Sleep(50e6) // 50 ms if atomic.LoadInt32(&done) == 0 { t.Fatal("double Close() test failed: second Close() call didn't return") }