PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_tree.rb

https://github.com/NV/em-dir-watcher
Ruby | 440 lines | 352 code | 88 blank | 0 comment | 8 complexity | 44b2da3b17d5e5f42b7488d800d281d2 MD5 | raw file
  1. require 'helper'
  2. require 'fileutils'
  3. require 'eventmachine'
  4. class TestTreeFileList < Test::Unit::TestCase
  5. def setup
  6. FileUtils.rm_rf TEST_DIR
  7. FileUtils.mkdir_p TEST_DIR
  8. end
  9. should "should be empty for an empty directory" do
  10. @tree = EMDirWatcher::Tree.new TEST_DIR
  11. assert_equal "", @tree.full_file_list.join(", ").strip
  12. end
  13. should "should return a single file" do
  14. FileUtils.touch File.join(TEST_DIR, 'foo')
  15. @tree = EMDirWatcher::Tree.new TEST_DIR
  16. assert_equal "foo", @tree.full_file_list.join(", ").strip
  17. end
  18. should "should return a file in a subdirectory" do
  19. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  20. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  21. @tree = EMDirWatcher::Tree.new TEST_DIR
  22. assert_equal "bar/foo", @tree.full_file_list.join(", ").strip
  23. end
  24. should "should return a sorted list of files" do
  25. FileUtils.touch File.join(TEST_DIR, 'aa')
  26. FileUtils.touch File.join(TEST_DIR, 'zz')
  27. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  28. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  29. @tree = EMDirWatcher::Tree.new TEST_DIR
  30. assert_equal "aa, bar/foo, zz", @tree.full_file_list.join(", ").strip
  31. end
  32. end
  33. class TestTreeInclusions < Test::Unit::TestCase
  34. def setup
  35. FileUtils.rm_rf TEST_DIR
  36. FileUtils.mkdir_p TEST_DIR
  37. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  38. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  39. FileUtils.touch File.join(TEST_DIR, 'aa')
  40. FileUtils.touch File.join(TEST_DIR, 'biz')
  41. FileUtils.touch File.join(TEST_DIR, 'zz')
  42. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  43. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  44. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  45. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  46. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  47. end
  48. should "ignore files not included by path" do
  49. @tree = EMDirWatcher::Tree.new TEST_DIR, ['/bar']
  50. assert_equal join(['bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort), join(@tree.full_file_list)
  51. end
  52. should "ignore files not included by extension glob" do
  53. @tree = EMDirWatcher::Tree.new TEST_DIR, ['*.html']
  54. assert_equal join(['bar/biz.html'].sort), join(@tree.full_file_list)
  55. end
  56. end
  57. class TestTreeExclusions < Test::Unit::TestCase
  58. def setup
  59. FileUtils.rm_rf TEST_DIR
  60. FileUtils.mkdir_p TEST_DIR
  61. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  62. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  63. FileUtils.touch File.join(TEST_DIR, 'aa')
  64. FileUtils.touch File.join(TEST_DIR, 'biz')
  65. FileUtils.touch File.join(TEST_DIR, 'zz')
  66. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  67. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  68. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  69. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  70. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  71. end
  72. should "ignore a single file excluded by path" do
  73. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, ['bar/biz']
  74. assert_equal join(@list - ['bar/biz']), join(@tree.full_file_list)
  75. end
  76. should "ignore files excluded by name" do
  77. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, ['biz']
  78. assert_equal join(@list - ['biz', 'bar/biz']), join(@tree.full_file_list)
  79. end
  80. should "ignore files excluded by name glob" do
  81. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, ['biz*']
  82. assert_equal join(@list - ['biz', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz']), join(@tree.full_file_list)
  83. end
  84. should "ignore a directory excluded by name glob" do
  85. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, ['bo*']
  86. assert_equal join(@list - ['bar/boo/bizzz']), join(@tree.full_file_list)
  87. end
  88. should "ignore a files and directories excluded by regexp" do
  89. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, [/b/]
  90. assert_equal join(['aa', 'zz']), join(@tree.full_file_list)
  91. end
  92. end
  93. class TestTreeInclusionsWithExclusions < Test::Unit::TestCase
  94. def setup
  95. FileUtils.rm_rf TEST_DIR
  96. FileUtils.mkdir_p TEST_DIR
  97. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  98. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  99. FileUtils.touch File.join(TEST_DIR, 'aa')
  100. FileUtils.touch File.join(TEST_DIR, 'biz')
  101. FileUtils.touch File.join(TEST_DIR, 'zz')
  102. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  103. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  104. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  105. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  106. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  107. end
  108. should "ignore files that match both inclusions and exclusions" do
  109. @tree = EMDirWatcher::Tree.new TEST_DIR, ['/bar'], ['*.html']
  110. assert_equal join(['bar/foo', 'bar/biz', 'bar/boo/bizzz'].sort), join(@tree.full_file_list)
  111. end
  112. end
  113. class TestTreeRefreshing < Test::Unit::TestCase
  114. def setup
  115. FileUtils.rm_rf TEST_DIR
  116. FileUtils.mkdir_p TEST_DIR
  117. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  118. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  119. FileUtils.touch File.join(TEST_DIR, 'aa')
  120. FileUtils.touch File.join(TEST_DIR, 'biz')
  121. FileUtils.touch File.join(TEST_DIR, 'zz')
  122. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  123. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  124. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  125. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  126. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  127. end
  128. should "no changes when nothing has changed" do
  129. @tree = EMDirWatcher::Tree.new TEST_DIR
  130. changed_paths = @tree.refresh!
  131. assert_equal "", join(changed_paths)
  132. assert_equal join(@list), join(@tree.full_file_list)
  133. end
  134. should "single file modification" do
  135. @tree = EMDirWatcher::Tree.new TEST_DIR
  136. sleep 1
  137. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  138. changed_paths = @tree.refresh!
  139. assert_equal join(['bar/foo']), join(changed_paths)
  140. end
  141. should "single file deletion" do
  142. @tree = EMDirWatcher::Tree.new TEST_DIR
  143. FileUtils.rm File.join(TEST_DIR, 'bar', 'biz')
  144. changed_paths = @tree.refresh!
  145. assert_equal join(['bar/biz']), join(changed_paths)
  146. end
  147. should "single directory deletion" do
  148. @tree = EMDirWatcher::Tree.new TEST_DIR
  149. FileUtils.rm_rf File.join(TEST_DIR, 'bar')
  150. changed_paths = @tree.refresh!
  151. assert_equal join(['bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort), join(changed_paths)
  152. end
  153. should "single file creation" do
  154. @tree = EMDirWatcher::Tree.new TEST_DIR
  155. FileUtils.touch File.join(TEST_DIR, 'bar', 'miz')
  156. changed_paths = @tree.refresh!
  157. assert_equal join(['bar/miz']), join(changed_paths)
  158. end
  159. should "single directory creation" do
  160. @tree = EMDirWatcher::Tree.new TEST_DIR
  161. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'koo')
  162. FileUtils.touch File.join(TEST_DIR, 'bar', 'koo', 'aaa')
  163. FileUtils.touch File.join(TEST_DIR, 'bar', 'koo', 'zzz')
  164. changed_paths = @tree.refresh!
  165. assert_equal join(['bar/koo/aaa', 'bar/koo/zzz'].sort), join(changed_paths)
  166. end
  167. should "not report changes on empty directory creation" do
  168. @tree = EMDirWatcher::Tree.new TEST_DIR
  169. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'koo')
  170. changed_paths = @tree.refresh!
  171. assert_equal "", join(changed_paths)
  172. end
  173. should "files turned into a directory" do
  174. @tree = EMDirWatcher::Tree.new TEST_DIR
  175. FileUtils.rm File.join(TEST_DIR, 'bar', 'foo')
  176. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'foo')
  177. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo', 'aaa')
  178. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo', 'zzz')
  179. changed_paths = @tree.refresh!
  180. assert_equal join(['bar/foo', 'bar/foo/aaa', 'bar/foo/zzz'].sort), join(changed_paths)
  181. end
  182. should "directory turned into a file" do
  183. @tree = EMDirWatcher::Tree.new TEST_DIR
  184. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'boo')
  185. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo')
  186. changed_paths = @tree.refresh!
  187. assert_equal join(['bar/boo/bizzz', 'bar/boo'].sort), join(changed_paths)
  188. end
  189. should "avoid traversing excluded directories" do
  190. @tree = EMDirWatcher::Tree.new TEST_DIR, nil, ['death']
  191. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'death')) unless EMDirWatcher::PLATFORM == 'Windows'
  192. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  193. changed_paths = @tree.refresh!
  194. assert_equal "bar/foo", join(changed_paths)
  195. end
  196. end
  197. class TestTreeScopedRefresh < Test::Unit::TestCase
  198. def setup
  199. FileUtils.rm_rf TEST_DIR
  200. FileUtils.mkdir_p TEST_DIR
  201. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  202. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  203. FileUtils.touch File.join(TEST_DIR, 'aa')
  204. FileUtils.touch File.join(TEST_DIR, 'biz')
  205. FileUtils.touch File.join(TEST_DIR, 'zz')
  206. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  207. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  208. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  209. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  210. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  211. end
  212. unless EMDirWatcher::PLATFORM == 'Windows'
  213. should "fail with an exception when faced with an endless symlink loop" do
  214. assert_raises Errno::ELOOP do
  215. @tree = EMDirWatcher::Tree.new TEST_DIR
  216. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'bar', 'death'))
  217. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  218. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar')
  219. end
  220. end
  221. end
  222. should "report file deletion in inner directory when the scope specifies the directory" do
  223. @tree = EMDirWatcher::Tree.new TEST_DIR
  224. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'death')) unless EMDirWatcher::PLATFORM == 'Windows'
  225. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  226. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar')
  227. assert_equal "bar/foo", join(changed_paths)
  228. end
  229. should "report file deletion in inner directory when the scope specifies the file" do
  230. @tree = EMDirWatcher::Tree.new TEST_DIR
  231. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'death')) unless EMDirWatcher::PLATFORM == 'Windows'
  232. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  233. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar', 'foo')
  234. assert_equal "bar/foo", join(changed_paths)
  235. end
  236. should "not refresh the whole directory when the scope specifies a single file" do
  237. @tree = EMDirWatcher::Tree.new TEST_DIR
  238. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'bar', 'death')) unless EMDirWatcher::PLATFORM == 'Windows'
  239. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  240. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar', 'foo')
  241. assert_equal "bar/foo", join(changed_paths)
  242. end
  243. should "report file deletion in a subtree when the scope specifies a directory" do
  244. @tree = EMDirWatcher::Tree.new TEST_DIR
  245. FileUtils.ln_s(TEST_DIR, File.join(TEST_DIR, 'death')) unless EMDirWatcher::PLATFORM == 'Windows'
  246. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  247. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar')
  248. assert_equal "bar/boo/bizzz", join(changed_paths)
  249. end
  250. should "report removed files when doing cascaded scoped refreshes" do
  251. @tree = EMDirWatcher::Tree.new TEST_DIR
  252. FileUtils.rm_rf File.join(TEST_DIR, 'bar')
  253. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar', 'boo')
  254. assert_equal "bar/boo/bizzz", join(changed_paths)
  255. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar')
  256. assert_equal "bar/biz, bar/biz.html, bar/foo", join(changed_paths)
  257. end
  258. end
  259. class TestTreeScopedNonRecursiveRefresh < Test::Unit::TestCase
  260. def setup
  261. FileUtils.rm_rf TEST_DIR
  262. FileUtils.mkdir_p TEST_DIR
  263. FileUtils.rm_rf ALT_TEST_DIR
  264. FileUtils.mkdir_p ALT_TEST_DIR
  265. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  266. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  267. FileUtils.touch File.join(TEST_DIR, 'aa')
  268. FileUtils.touch File.join(TEST_DIR, 'biz')
  269. FileUtils.touch File.join(TEST_DIR, 'zz')
  270. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  271. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  272. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  273. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  274. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  275. end
  276. should "not report changes in a child directory" do
  277. @tree = EMDirWatcher::Tree.new TEST_DIR
  278. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  279. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar'), false
  280. assert_equal "", join(changed_paths)
  281. end
  282. should "report removed files" do
  283. @tree = EMDirWatcher::Tree.new TEST_DIR
  284. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  285. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar'), false
  286. assert_equal "bar/foo", join(changed_paths)
  287. end
  288. should "report added files" do
  289. @tree = EMDirWatcher::Tree.new TEST_DIR
  290. FileUtils.touch File.join(TEST_DIR, 'bar', 'coo')
  291. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar'), false
  292. assert_equal "bar/coo", join(changed_paths)
  293. end
  294. should "report entire subtree of a removed directory when the scope specifies that directory" do
  295. @tree = EMDirWatcher::Tree.new TEST_DIR
  296. FileUtils.rm_rf File.join(TEST_DIR, 'bar')
  297. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar'), false
  298. assert_equal "bar/biz, bar/biz.html, bar/boo/bizzz, bar/foo", join(changed_paths)
  299. end
  300. should "report entire subtree of an added directory when the scope specifies that directory" do
  301. FileUtils.mv File.join(TEST_DIR, 'bar'), ALT_TEST_DIR
  302. @tree = EMDirWatcher::Tree.new TEST_DIR
  303. FileUtils.mv File.join(ALT_TEST_DIR, 'bar'), TEST_DIR
  304. changed_paths = @tree.refresh! File.join(TEST_DIR, 'bar'), false
  305. assert_equal "bar/biz, bar/biz.html, bar/boo/bizzz, bar/foo", join(changed_paths)
  306. end
  307. should "report entire subtree of a removed directory when the scope specifies a parent directory" do
  308. @tree = EMDirWatcher::Tree.new TEST_DIR
  309. FileUtils.rm_rf File.join(TEST_DIR, 'bar')
  310. changed_paths = @tree.refresh! File.join(TEST_DIR), false
  311. assert_equal "bar/biz, bar/biz.html, bar/boo/bizzz, bar/foo", join(changed_paths)
  312. end
  313. should "report entire subtree of an added directory when the scope specifies a parent directory" do
  314. FileUtils.mv File.join(TEST_DIR, 'bar'), ALT_TEST_DIR
  315. @tree = EMDirWatcher::Tree.new TEST_DIR
  316. FileUtils.mv File.join(ALT_TEST_DIR, 'bar'), TEST_DIR
  317. changed_paths = @tree.refresh! File.join(TEST_DIR), false
  318. assert_equal "bar/biz, bar/biz.html, bar/boo/bizzz, bar/foo", join(changed_paths)
  319. end
  320. end
  321. unless EMDirWatcher::PLATFORM == 'Windows'
  322. class TestTreeSymlinkHandling < Test::Unit::TestCase
  323. def setup
  324. FileUtils.rm_rf TEST_DIR
  325. FileUtils.rm_rf ALT_TEST_DIR
  326. FileUtils.mkdir_p TEST_DIR
  327. FileUtils.mkdir File.join(TEST_DIR, 'bar')
  328. FileUtils.mkdir File.join(TEST_DIR, 'bar', 'boo')
  329. FileUtils.touch File.join(TEST_DIR, 'aa')
  330. FileUtils.touch File.join(TEST_DIR, 'biz')
  331. FileUtils.touch File.join(TEST_DIR, 'zz')
  332. FileUtils.touch File.join(TEST_DIR, 'bar', 'foo')
  333. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz')
  334. FileUtils.touch File.join(TEST_DIR, 'bar', 'biz.html')
  335. FileUtils.touch File.join(TEST_DIR, 'bar', 'boo', 'bizzz')
  336. FileUtils.ln_s TEST_DIR, ALT_TEST_DIR
  337. @list = ['aa', 'biz', 'zz', 'bar/foo', 'bar/biz', 'bar/biz.html', 'bar/boo/bizzz'].sort
  338. end
  339. should "handle referencing root scope via symlink" do
  340. @tree = EMDirWatcher::Tree.new TEST_DIR
  341. FileUtils.rm_rf File.join(TEST_DIR, 'bar', 'foo')
  342. changed_paths = @tree.refresh! ALT_TEST_DIR
  343. assert_equal "bar/foo", join(changed_paths)
  344. end
  345. should "handle referencing root scope by real path when monitoring a symlinked path" do
  346. @tree = EMDirWatcher::Tree.new ALT_TEST_DIR
  347. FileUtils.rm_rf File.join(ALT_TEST_DIR, 'bar', 'foo')
  348. changed_paths = @tree.refresh! TEST_DIR
  349. assert_equal "bar/foo", join(changed_paths)
  350. end
  351. end
  352. end