fsnotify_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !plan9
  5. package fsnotify
  6. import (
  7. "os"
  8. "testing"
  9. "time"
  10. )
  11. func TestEventStringWithValue(t *testing.T) {
  12. for opMask, expectedString := range map[Op]string{
  13. Chmod | Create: `"/usr/someFile": CREATE|CHMOD`,
  14. Rename: `"/usr/someFile": RENAME`,
  15. Remove: `"/usr/someFile": REMOVE`,
  16. Write | Chmod: `"/usr/someFile": WRITE|CHMOD`,
  17. } {
  18. event := Event{Name: "/usr/someFile", Op: opMask}
  19. if event.String() != expectedString {
  20. t.Fatalf("Expected %s, got: %v", expectedString, event.String())
  21. }
  22. }
  23. }
  24. func TestEventOpStringWithValue(t *testing.T) {
  25. expectedOpString := "WRITE|CHMOD"
  26. event := Event{Name: "someFile", Op: Write | Chmod}
  27. if event.Op.String() != expectedOpString {
  28. t.Fatalf("Expected %s, got: %v", expectedOpString, event.Op.String())
  29. }
  30. }
  31. func TestEventOpStringWithNoValue(t *testing.T) {
  32. expectedOpString := ""
  33. event := Event{Name: "testFile", Op: 0}
  34. if event.Op.String() != expectedOpString {
  35. t.Fatalf("Expected %s, got: %v", expectedOpString, event.Op.String())
  36. }
  37. }
  38. // TestWatcherClose tests that the goroutine started by creating the watcher can be
  39. // signalled to return at any time, even if there is no goroutine listening on the events
  40. // or errors channels.
  41. func TestWatcherClose(t *testing.T) {
  42. t.Parallel()
  43. name := tempMkFile(t, "")
  44. w := newWatcher(t)
  45. err := w.Add(name)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. err = os.Remove(name)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. // Allow the watcher to receive the event.
  54. time.Sleep(time.Millisecond * 100)
  55. err = w.Close()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. }