notify_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package watch
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/windmilleng/tilt/internal/testutils/tempdir"
  12. )
  13. // Each implementation of the notify interface should have the same basic
  14. // behavior.
  15. func TestNoEvents(t *testing.T) {
  16. f := newNotifyFixture(t)
  17. defer f.tearDown()
  18. f.assertEvents()
  19. }
  20. func TestEventOrdering(t *testing.T) {
  21. f := newNotifyFixture(t)
  22. defer f.tearDown()
  23. count := 8
  24. dirs := make([]string, count)
  25. for i, _ := range dirs {
  26. dir := f.TempDir("watched")
  27. dirs[i] = dir
  28. err := f.notify.Add(dir)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. }
  33. f.fsync()
  34. f.events = nil
  35. var expected []string
  36. for i, dir := range dirs {
  37. base := fmt.Sprintf("%d.txt", i)
  38. p := filepath.Join(dir, base)
  39. err := ioutil.WriteFile(p, []byte(base), os.FileMode(0777))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. expected = append(expected, filepath.Join(dir, base))
  44. }
  45. f.assertEvents(expected...)
  46. }
  47. func TestWatchesAreRecursive(t *testing.T) {
  48. f := newNotifyFixture(t)
  49. defer f.tearDown()
  50. root := f.TempDir("root")
  51. // add a sub directory
  52. subPath := filepath.Join(root, "sub")
  53. f.MkdirAll(subPath)
  54. // watch parent
  55. err := f.notify.Add(root)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. f.fsync()
  60. f.events = nil
  61. // change sub directory
  62. changeFilePath := filepath.Join(subPath, "change")
  63. _, err = os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. f.assertEvents(changeFilePath)
  68. }
  69. func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
  70. f := newNotifyFixture(t)
  71. defer f.tearDown()
  72. root := f.TempDir("root")
  73. // watch parent
  74. err := f.notify.Add(root)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. f.fsync()
  79. f.events = nil
  80. // add a sub directory
  81. subPath := filepath.Join(root, "sub")
  82. f.MkdirAll(subPath)
  83. // change something inside sub directory
  84. changeFilePath := filepath.Join(subPath, "change")
  85. _, err = os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. f.assertEvents(subPath, changeFilePath)
  90. }
  91. func TestWatchNonExistentPath(t *testing.T) {
  92. f := newNotifyFixture(t)
  93. defer f.tearDown()
  94. root := f.TempDir("root")
  95. path := filepath.Join(root, "change")
  96. err := f.notify.Add(path)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. f.fsync()
  101. d1 := "hello\ngo\n"
  102. f.WriteFile(path, d1)
  103. f.assertEvents(path)
  104. }
  105. func TestRemove(t *testing.T) {
  106. f := newNotifyFixture(t)
  107. defer f.tearDown()
  108. root := f.TempDir("root")
  109. path := filepath.Join(root, "change")
  110. d1 := "hello\ngo\n"
  111. f.WriteFile(path, d1)
  112. err := f.notify.Add(path)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. f.fsync()
  117. f.events = nil
  118. err = os.Remove(path)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. f.assertEvents(path)
  123. }
  124. func TestRemoveAndAddBack(t *testing.T) {
  125. f := newNotifyFixture(t)
  126. defer f.tearDown()
  127. path := filepath.Join(f.watched, "change")
  128. d1 := []byte("hello\ngo\n")
  129. err := ioutil.WriteFile(path, d1, 0644)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. err = f.notify.Add(path)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. f.assertEvents(path)
  138. err = os.Remove(path)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. f.assertEvents(path)
  143. f.events = nil
  144. err = ioutil.WriteFile(path, d1, 0644)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. f.assertEvents(path)
  149. }
  150. func TestSingleFile(t *testing.T) {
  151. f := newNotifyFixture(t)
  152. defer f.tearDown()
  153. root := f.TempDir("root")
  154. path := filepath.Join(root, "change")
  155. d1 := "hello\ngo\n"
  156. f.WriteFile(path, d1)
  157. err := f.notify.Add(path)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. f.fsync()
  162. d2 := []byte("hello\nworld\n")
  163. err = ioutil.WriteFile(path, d2, 0644)
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. f.assertEvents(path)
  168. }
  169. func TestWriteBrokenLink(t *testing.T) {
  170. f := newNotifyFixture(t)
  171. defer f.tearDown()
  172. link := filepath.Join(f.watched, "brokenLink")
  173. missingFile := filepath.Join(f.watched, "missingFile")
  174. err := os.Symlink(missingFile, link)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. f.assertEvents(link)
  179. }
  180. func TestWriteGoodLink(t *testing.T) {
  181. f := newNotifyFixture(t)
  182. defer f.tearDown()
  183. goodFile := filepath.Join(f.watched, "goodFile")
  184. err := ioutil.WriteFile(goodFile, []byte("hello"), 0644)
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. link := filepath.Join(f.watched, "goodFileSymlink")
  189. err = os.Symlink(goodFile, link)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. f.assertEvents(goodFile, link)
  194. }
  195. func TestWatchBrokenLink(t *testing.T) {
  196. f := newNotifyFixture(t)
  197. defer f.tearDown()
  198. newRoot, err := NewDir(t.Name())
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. defer newRoot.TearDown()
  203. link := filepath.Join(newRoot.Path(), "brokenLink")
  204. missingFile := filepath.Join(newRoot.Path(), "missingFile")
  205. err = os.Symlink(missingFile, link)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. err = f.notify.Add(newRoot.Path())
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. os.Remove(link)
  214. f.assertEvents(link)
  215. }
  216. func TestMoveAndReplace(t *testing.T) {
  217. f := newNotifyFixture(t)
  218. defer f.tearDown()
  219. root := f.TempDir("root")
  220. file := filepath.Join(root, "myfile")
  221. f.WriteFile(file, "hello")
  222. err := f.notify.Add(file)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. tmpFile := filepath.Join(root, ".myfile.swp")
  227. f.WriteFile(tmpFile, "world")
  228. err = os.Rename(tmpFile, file)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. f.assertEvents(file)
  233. }
  234. func TestWatchBothDirAndFile(t *testing.T) {
  235. f := newNotifyFixture(t)
  236. defer f.tearDown()
  237. dir := f.JoinPath("foo")
  238. fileA := f.JoinPath("foo", "a")
  239. fileB := f.JoinPath("foo", "b")
  240. f.WriteFile(fileA, "a")
  241. f.WriteFile(fileB, "b")
  242. f.watch(fileA)
  243. f.watch(dir)
  244. f.fsync()
  245. f.events = nil
  246. f.WriteFile(fileB, "b-new")
  247. f.assertEvents(fileB)
  248. }
  249. func TestWatchNonexistentDirectory(t *testing.T) {
  250. f := newNotifyFixture(t)
  251. defer f.tearDown()
  252. root := f.JoinPath("root")
  253. err := os.Mkdir(root, 0777)
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. parent := f.JoinPath("root", "parent")
  258. file := f.JoinPath("root", "parent", "a")
  259. f.watch(file)
  260. f.fsync()
  261. f.events = nil
  262. f.WriteFile(file, "hello")
  263. if runtime.GOOS == "darwin" {
  264. f.assertEvents(file)
  265. } else {
  266. f.assertEvents(parent, file)
  267. }
  268. }
  269. type notifyFixture struct {
  270. *tempdir.TempDirFixture
  271. notify Notify
  272. watched string
  273. events []FileEvent
  274. }
  275. func newNotifyFixture(t *testing.T) *notifyFixture {
  276. SetLimitChecksEnabled(false)
  277. notify, err := NewWatcher()
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. f := tempdir.NewTempDirFixture(t)
  282. watched := f.TempDir("watched")
  283. err = notify.Add(watched)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. return &notifyFixture{
  288. TempDirFixture: f,
  289. watched: watched,
  290. notify: notify,
  291. }
  292. }
  293. func (f *notifyFixture) watch(path string) {
  294. err := f.notify.Add(path)
  295. if err != nil {
  296. f.T().Fatalf("notify.Add: %s", path)
  297. }
  298. }
  299. func (f *notifyFixture) assertEvents(expected ...string) {
  300. f.fsync()
  301. if len(f.events) != len(expected) {
  302. f.T().Fatalf("Got %d events (expected %d): %v %v", len(f.events), len(expected), f.events, expected)
  303. }
  304. for i, actual := range f.events {
  305. e := FileEvent{expected[i]}
  306. if actual != e {
  307. f.T().Fatalf("Got event %v (expected %v)", actual, e)
  308. }
  309. }
  310. }
  311. func (f *notifyFixture) fsync() {
  312. syncPathBase := fmt.Sprintf("sync-%d.txt", time.Now().UnixNano())
  313. syncPath := filepath.Join(f.watched, syncPathBase)
  314. anySyncPath := filepath.Join(f.watched, "sync-")
  315. timeout := time.After(time.Second)
  316. f.WriteFile(syncPath, fmt.Sprintf("%s", time.Now()))
  317. F:
  318. for {
  319. select {
  320. case err := <-f.notify.Errors():
  321. f.T().Fatal(err)
  322. case event := <-f.notify.Events():
  323. if strings.Contains(event.Path, syncPath) {
  324. break F
  325. }
  326. if strings.Contains(event.Path, anySyncPath) {
  327. continue
  328. }
  329. // Don't bother tracking duplicate changes to the same path
  330. // for testing.
  331. if len(f.events) > 0 && f.events[len(f.events)-1].Path == event.Path {
  332. continue
  333. }
  334. f.events = append(f.events, event)
  335. case <-timeout:
  336. f.T().Fatalf("fsync: timeout")
  337. }
  338. }
  339. }
  340. func (f *notifyFixture) tearDown() {
  341. SetLimitChecksEnabled(true)
  342. err := f.notify.Close()
  343. if err != nil {
  344. f.T().Fatal(err)
  345. }
  346. f.TempDirFixture.TearDown()
  347. }