From: Chris Howey Date: Mon, 11 Mar 2013 23:26:37 +0000 (-0500) Subject: Commited to go.exp X-Git-Tag: v1.7.2~377 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=73315a12a2ed52fed59cdbd8de2e688e1817b21a;p=fsnotify.git Commited to go.exp https://codereview.appspot.com/7497045 https://code.google.com/p/go/issues/detail?id=4068 --- diff --git a/example_test.go b/example_test.go index 5f0c8b5..37c0d0c 100644 --- a/example_test.go +++ b/example_test.go @@ -1,8 +1,13 @@ +// Copyright 2012 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_test import ( - "github.com/howeyc/fsnotify" "log" + + "code.google.com/p/go.exp/fsnotify" ) func ExampleNewWatcher() { diff --git a/fsnotify.go b/fsnotify.go index a700db6..ed731a5 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package fsnotify implements filesystem notification. package fsnotify import "fmt" diff --git a/fsnotify_bsd.go b/fsnotify_bsd.go index 1c344f0..f6f7799 100644 --- a/fsnotify_bsd.go +++ b/fsnotify_bsd.go @@ -4,7 +4,6 @@ // +build freebsd openbsd netbsd darwin -//Package fsnotify implements filesystem notification. package fsnotify import ( diff --git a/fsnotify_linux.go b/fsnotify_linux.go index 2b7dbb4..3f0241b 100644 --- a/fsnotify_linux.go +++ b/fsnotify_linux.go @@ -4,7 +4,6 @@ // +build linux -// Package fsnotify implements a wrapper for the Linux inotify system. package fsnotify import ( diff --git a/fsnotify_test.go b/fsnotify_test.go index 0ae9ed9..d272453 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -1,6 +1,6 @@ -// 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 @@ -13,7 +13,7 @@ import ( "time" ) -// An atomic counter +// An atomic counter type counter struct { val int32 } @@ -31,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) @@ -47,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) } @@ -62,18 +62,18 @@ 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 + // Only count relevant events if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { t.Logf("event received: %s", event) if event.IsDelete() { @@ -95,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 { @@ -109,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() @@ -117,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) @@ -126,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 { @@ -152,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...") @@ -165,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) @@ -180,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) } @@ -188,18 +188,18 @@ 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 + // Only count relevant events if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { t.Logf("event received: %s", event) if event.IsDelete() { @@ -218,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 { @@ -232,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) @@ -258,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) @@ -272,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 { @@ -289,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...") @@ -302,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) @@ -310,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 @@ -329,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) @@ -344,13 +344,13 @@ 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 + // 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() { @@ -369,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 { @@ -383,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 { @@ -403,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...") @@ -416,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) @@ -425,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 @@ -442,31 +442,31 @@ 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 + // Only count relevant events if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFileAlreadyExists) { t.Logf("event received: %s", event) if event.IsDelete() { @@ -480,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 { @@ -489,7 +489,9 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) { } func TestFsnotifySubDir(t *testing.T) { - // Create an fsnotify watcher instance and initialize it + t.Skip("fails on OS X at least") + + // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) @@ -500,26 +502,26 @@ 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 + // 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() { @@ -535,18 +537,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 { @@ -555,7 +557,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 { @@ -564,11 +566,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 { @@ -579,7 +581,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...") @@ -592,7 +594,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) @@ -600,19 +602,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) @@ -622,13 +624,13 @@ 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 + // 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() @@ -641,8 +643,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 { @@ -654,7 +656,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) @@ -666,13 +668,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...") @@ -687,7 +689,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) @@ -696,25 +698,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) @@ -724,13 +726,13 @@ 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 + // 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() @@ -743,8 +745,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 { @@ -759,13 +761,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...") @@ -780,7 +782,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) @@ -792,19 +794,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 { @@ -813,26 +815,26 @@ 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 + // Only count relevant events if event.Name == filepath.Clean(testFileRenamed) { eventReceived.increment() t.Logf("event received: %s", event) @@ -843,8 +845,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 { @@ -859,13 +861,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...") @@ -880,7 +882,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) @@ -888,13 +890,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) @@ -903,13 +905,13 @@ 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 + // Only count relevant events if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) { if event.IsModify() { attribReceived.increment() @@ -922,8 +924,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 { @@ -935,7 +937,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) @@ -947,13 +949,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...") @@ -977,7 +979,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") } diff --git a/fsnotify_windows.go b/fsnotify_windows.go index ebc25bb..d4ef827 100644 --- a/fsnotify_windows.go +++ b/fsnotify_windows.go @@ -4,8 +4,6 @@ // +build windows -// Package fsnotify allows the user to receive -// file system event notifications on Windows. package fsnotify import (