notify_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 TestWatchNonExistentPathDoesNotFireSiblingEvent(t *testing.T) {
  106. f := newNotifyFixture(t)
  107. defer f.tearDown()
  108. root := f.TempDir("root")
  109. watchedFile := filepath.Join(root, "a.txt")
  110. unwatchedSibling := filepath.Join(root, "b.txt")
  111. err := f.notify.Add(watchedFile)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. f.fsync()
  116. d1 := "hello\ngo\n"
  117. f.WriteFile(unwatchedSibling, d1)
  118. f.assertEvents()
  119. }
  120. func TestRemove(t *testing.T) {
  121. f := newNotifyFixture(t)
  122. defer f.tearDown()
  123. root := f.TempDir("root")
  124. path := filepath.Join(root, "change")
  125. d1 := "hello\ngo\n"
  126. f.WriteFile(path, d1)
  127. err := f.notify.Add(path)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. f.fsync()
  132. f.events = nil
  133. err = os.Remove(path)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. f.assertEvents(path)
  138. }
  139. func TestRemoveAndAddBack(t *testing.T) {
  140. f := newNotifyFixture(t)
  141. defer f.tearDown()
  142. path := filepath.Join(f.watched, "change")
  143. d1 := []byte("hello\ngo\n")
  144. err := ioutil.WriteFile(path, d1, 0644)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. err = f.notify.Add(path)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. f.assertEvents(path)
  153. err = os.Remove(path)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. f.assertEvents(path)
  158. f.events = nil
  159. err = ioutil.WriteFile(path, d1, 0644)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. f.assertEvents(path)
  164. }
  165. func TestSingleFile(t *testing.T) {
  166. f := newNotifyFixture(t)
  167. defer f.tearDown()
  168. root := f.TempDir("root")
  169. path := filepath.Join(root, "change")
  170. d1 := "hello\ngo\n"
  171. f.WriteFile(path, d1)
  172. err := f.notify.Add(path)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. f.fsync()
  177. d2 := []byte("hello\nworld\n")
  178. err = ioutil.WriteFile(path, d2, 0644)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. f.assertEvents(path)
  183. }
  184. func TestWriteBrokenLink(t *testing.T) {
  185. f := newNotifyFixture(t)
  186. defer f.tearDown()
  187. link := filepath.Join(f.watched, "brokenLink")
  188. missingFile := filepath.Join(f.watched, "missingFile")
  189. err := os.Symlink(missingFile, link)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. f.assertEvents(link)
  194. }
  195. func TestWriteGoodLink(t *testing.T) {
  196. f := newNotifyFixture(t)
  197. defer f.tearDown()
  198. goodFile := filepath.Join(f.watched, "goodFile")
  199. err := ioutil.WriteFile(goodFile, []byte("hello"), 0644)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. link := filepath.Join(f.watched, "goodFileSymlink")
  204. err = os.Symlink(goodFile, link)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. f.assertEvents(goodFile, link)
  209. }
  210. func TestWatchBrokenLink(t *testing.T) {
  211. f := newNotifyFixture(t)
  212. defer f.tearDown()
  213. newRoot, err := NewDir(t.Name())
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. defer newRoot.TearDown()
  218. link := filepath.Join(newRoot.Path(), "brokenLink")
  219. missingFile := filepath.Join(newRoot.Path(), "missingFile")
  220. err = os.Symlink(missingFile, link)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. err = f.notify.Add(newRoot.Path())
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. os.Remove(link)
  229. f.assertEvents(link)
  230. }
  231. func TestMoveAndReplace(t *testing.T) {
  232. f := newNotifyFixture(t)
  233. defer f.tearDown()
  234. root := f.TempDir("root")
  235. file := filepath.Join(root, "myfile")
  236. f.WriteFile(file, "hello")
  237. err := f.notify.Add(file)
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. tmpFile := filepath.Join(root, ".myfile.swp")
  242. f.WriteFile(tmpFile, "world")
  243. err = os.Rename(tmpFile, file)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. f.assertEvents(file)
  248. }
  249. func TestWatchBothDirAndFile(t *testing.T) {
  250. f := newNotifyFixture(t)
  251. defer f.tearDown()
  252. dir := f.JoinPath("foo")
  253. fileA := f.JoinPath("foo", "a")
  254. fileB := f.JoinPath("foo", "b")
  255. f.WriteFile(fileA, "a")
  256. f.WriteFile(fileB, "b")
  257. f.watch(fileA)
  258. f.watch(dir)
  259. f.fsync()
  260. f.events = nil
  261. f.WriteFile(fileB, "b-new")
  262. f.assertEvents(fileB)
  263. }
  264. func TestWatchNonexistentFileInNonexistentDirectoryCreatedSimultaneously(t *testing.T) {
  265. f := newNotifyFixture(t)
  266. defer f.tearDown()
  267. root := f.JoinPath("root")
  268. err := os.Mkdir(root, 0777)
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. file := f.JoinPath("root", "parent", "a")
  273. f.watch(file)
  274. f.fsync()
  275. f.events = nil
  276. f.WriteFile(file, "hello")
  277. f.assertEvents(file)
  278. }
  279. func TestWatchNonexistentDirectory(t *testing.T) {
  280. f := newNotifyFixture(t)
  281. defer f.tearDown()
  282. root := f.JoinPath("root")
  283. err := os.Mkdir(root, 0777)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. parent := f.JoinPath("parent")
  288. file := f.JoinPath("parent", "a")
  289. f.watch(parent)
  290. f.fsync()
  291. f.events = nil
  292. err = os.Mkdir(parent, 0777)
  293. if err != nil {
  294. t.Fatal(err)
  295. }
  296. if runtime.GOOS == "darwin" {
  297. // for directories that were the root of an Add, we don't report creation, cf. watcher_darwin.go
  298. f.assertEvents()
  299. } else {
  300. f.assertEvents(parent)
  301. }
  302. f.WriteFile(file, "hello")
  303. if runtime.GOOS == "darwin" {
  304. // mac doesn't return the dir change as part of file creation
  305. f.assertEvents(file)
  306. } else {
  307. f.assertEvents(parent, file)
  308. }
  309. }
  310. // doesn't work on linux
  311. // func TestWatchNonexistentFileInNonexistentDirectory(t *testing.T) {
  312. // f := newNotifyFixture(t)
  313. // defer f.tearDown()
  314. // root := f.JoinPath("root")
  315. // err := os.Mkdir(root, 0777)
  316. // if err != nil {
  317. // t.Fatal(err)
  318. // }
  319. // parent := f.JoinPath("parent")
  320. // file := f.JoinPath("parent", "a")
  321. // f.watch(file)
  322. // f.assertEvents()
  323. // err = os.Mkdir(parent, 0777)
  324. // if err != nil {
  325. // t.Fatal(err)
  326. // }
  327. // f.assertEvents()
  328. // f.WriteFile(file, "hello")
  329. // f.assertEvents(file)
  330. // }
  331. type notifyFixture struct {
  332. *tempdir.TempDirFixture
  333. notify Notify
  334. watched string
  335. events []FileEvent
  336. }
  337. func newNotifyFixture(t *testing.T) *notifyFixture {
  338. notify, err := NewWatcher()
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. f := tempdir.NewTempDirFixture(t)
  343. watched := f.TempDir("watched")
  344. err = notify.Add(watched)
  345. if err != nil {
  346. t.Fatal(err)
  347. }
  348. return &notifyFixture{
  349. TempDirFixture: f,
  350. watched: watched,
  351. notify: notify,
  352. }
  353. }
  354. func (f *notifyFixture) watch(path string) {
  355. err := f.notify.Add(path)
  356. if err != nil {
  357. f.T().Fatalf("notify.Add: %s", path)
  358. }
  359. }
  360. func (f *notifyFixture) assertEvents(expected ...string) {
  361. f.fsync()
  362. if len(f.events) != len(expected) {
  363. f.T().Fatalf("Got %d events (expected %d): %v %v", len(f.events), len(expected), f.events, expected)
  364. }
  365. for i, actual := range f.events {
  366. e := FileEvent{expected[i]}
  367. if actual != e {
  368. f.T().Fatalf("Got event %v (expected %v)", actual, e)
  369. }
  370. }
  371. }
  372. func (f *notifyFixture) fsync() {
  373. syncPathBase := fmt.Sprintf("sync-%d.txt", time.Now().UnixNano())
  374. syncPath := filepath.Join(f.watched, syncPathBase)
  375. anySyncPath := filepath.Join(f.watched, "sync-")
  376. timeout := time.After(time.Second)
  377. f.WriteFile(syncPath, fmt.Sprintf("%s", time.Now()))
  378. F:
  379. for {
  380. select {
  381. case err := <-f.notify.Errors():
  382. f.T().Fatal(err)
  383. case event := <-f.notify.Events():
  384. if strings.Contains(event.Path, syncPath) {
  385. break F
  386. }
  387. if strings.Contains(event.Path, anySyncPath) {
  388. continue
  389. }
  390. // Don't bother tracking duplicate changes to the same path
  391. // for testing.
  392. if len(f.events) > 0 && f.events[len(f.events)-1].Path == event.Path {
  393. continue
  394. }
  395. f.events = append(f.events, event)
  396. case <-timeout:
  397. f.T().Fatalf("fsync: timeout")
  398. }
  399. }
  400. }
  401. func (f *notifyFixture) tearDown() {
  402. err := f.notify.Close()
  403. if err != nil {
  404. f.T().Fatal(err)
  405. }
  406. // drain channels from watcher
  407. go func() {
  408. for _ = range f.notify.Events() {
  409. }
  410. }()
  411. go func() {
  412. for _ = range f.notify.Errors() {
  413. }
  414. }()
  415. f.TempDirFixture.TearDown()
  416. }