watcher.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. var assert = require('assert').strict,
  2. fs = require('fs-extra'),
  3. path = require('path'),
  4. temp = require('unique-temp-dir'),
  5. watcher = require('../lib/watcher');
  6. describe('watcher', function() {
  7. var main, sibling;
  8. var origin = path.join(__dirname, 'fixtures', 'watcher');
  9. beforeEach(function() {
  10. var fixture = temp();
  11. fs.ensureDirSync(fixture);
  12. fs.copySync(origin, fixture);
  13. main = fs.realpathSync(path.join(fixture, 'main'));
  14. sibling = fs.realpathSync(path.join(fixture, 'sibling'));
  15. });
  16. describe('with directory', function() {
  17. beforeEach(function() {
  18. watcher.reset({
  19. directory: main,
  20. includePath: [main]
  21. });
  22. });
  23. describe('when a file is changed', function() {
  24. describe('and it is in the graph', function() {
  25. describe('if it is a partial', function() {
  26. it('should record its ancestors as changed', function() {
  27. var file = path.join(main, 'partials', '_one.scss');
  28. var files = watcher.changed(file);
  29. assert.deepStrictEqual(files.changed, [
  30. path.join(main, 'one.scss'),
  31. ]);
  32. });
  33. it('should record its descendants as added', function() {
  34. var file = path.join(main, 'partials', '_one.scss');
  35. var files = watcher.changed(file);
  36. assert.deepStrictEqual(files.added, [
  37. path.join(main, 'partials', '_three.scss'),
  38. ]);
  39. });
  40. it('should record nothing as removed', function() {
  41. var file = path.join(main, 'partials', '_one.scss');
  42. var files = watcher.changed(file);
  43. assert.deepStrictEqual(files.removed, []);
  44. });
  45. });
  46. describe('if it is not a partial', function() {
  47. it('should record itself as changed', function() {
  48. var file = path.join(main, 'one.scss');
  49. var files = watcher.changed(file);
  50. assert.deepStrictEqual(files.changed, [
  51. file,
  52. ]);
  53. });
  54. it('should record its descendants as added', function() {
  55. var file = path.join(main, 'one.scss');
  56. var files = watcher.changed(file);
  57. assert.deepStrictEqual(files.added, [
  58. path.join(main, 'partials', '_one.scss'),
  59. path.join(main, 'partials', '_three.scss'),
  60. ]);
  61. });
  62. it('should record nothing as removed', function() {
  63. var file = path.join(main, 'one.scss');
  64. var files = watcher.changed(file);
  65. assert.deepStrictEqual(files.removed, []);
  66. });
  67. });
  68. });
  69. describe('and is not in the graph', function() {
  70. describe('if it is a partial', function() {
  71. it('should not record anything', function() {
  72. var file = path.join(sibling, 'partials', '_three.scss');
  73. var files = watcher.changed(file);
  74. assert.deepStrictEqual(files, {
  75. added: [],
  76. changed: [],
  77. removed: [],
  78. });
  79. });
  80. });
  81. describe('if it is not a partial', function() {
  82. it('should record itself as changed', function() {
  83. var file = path.join(sibling, 'three.scss');
  84. var files = watcher.changed(file);
  85. assert.deepStrictEqual(files, {
  86. added: [],
  87. changed: [file],
  88. removed: [],
  89. });
  90. });
  91. });
  92. });
  93. });
  94. describe('when a file is added', function() {
  95. describe('and it is in the graph', function() {
  96. describe('if it is a partial', function() {
  97. it('should record nothing as added', function() {
  98. var file = path.join(main, 'partials', '_three.scss');
  99. var files = watcher.added(file);
  100. assert.deepStrictEqual(files.added, []);
  101. });
  102. it('should record its descendants as added', function() {
  103. var file = path.join(main, 'partials', '_one.scss');
  104. var files = watcher.added(file);
  105. assert.deepStrictEqual(files.added, [
  106. path.join(main, 'partials', '_three.scss')
  107. ]);
  108. });
  109. it('should record nothing as changed', function() {
  110. var file = path.join(main, 'partials', '_three.scss');
  111. var files = watcher.added(file);
  112. assert.deepStrictEqual(files.changed, []);
  113. });
  114. it('should record nothing as removed', function() {
  115. var file = path.join(main, 'partials', '_three.scss');
  116. var files = watcher.added(file);
  117. assert.deepStrictEqual(files.removed, []);
  118. });
  119. });
  120. describe('if it is not a partial', function() {
  121. it('should record nothing as added', function() {
  122. var file = path.join(main, 'three.scss');
  123. var files = watcher.added(file);
  124. assert.deepStrictEqual(files.added, []);
  125. });
  126. it('should record its descendants as added', function() {
  127. var file = path.join(main, 'one.scss');
  128. var files = watcher.added(file);
  129. assert.deepStrictEqual(files.added, [
  130. path.join(main, 'partials', '_one.scss'),
  131. path.join(main, 'partials', '_three.scss'),
  132. ]);
  133. });
  134. it('should record nothing as changed', function() {
  135. var file = path.join(main, 'one.scss');
  136. var files = watcher.added(file);
  137. assert.deepStrictEqual(files.changed, []);
  138. });
  139. it('should record nothing as removed', function() {
  140. var file = path.join(main, 'one.scss');
  141. var files = watcher.added(file);
  142. assert.deepStrictEqual(files.removed, []);
  143. });
  144. });
  145. });
  146. });
  147. describe('when a file is removed', function() {
  148. describe('and it is in the graph', function() {
  149. describe('if it is a partial', function() {
  150. it('should record nothing as added', function() {
  151. var file = path.join(main, 'partials', '_one.scss');
  152. var files = watcher.removed(file);
  153. assert.deepStrictEqual(files.added, []);
  154. });
  155. it('should record its ancestors as changed', function() {
  156. var file = path.join(main, 'partials', '_one.scss');
  157. var files = watcher.removed(file);
  158. assert.deepStrictEqual(files.changed, [
  159. path.join(main, 'one.scss'),
  160. ]);
  161. });
  162. it('should record itself as removed', function() {
  163. var file = path.join(main, 'partials', '_one.scss');
  164. var files = watcher.removed(file);
  165. assert.deepStrictEqual(files.removed, [file]);
  166. });
  167. });
  168. describe('if it is not a partial', function() {
  169. it('should record nothing as added', function() {
  170. var file = path.join(main, 'one.scss');
  171. var files = watcher.removed(file);
  172. assert.deepStrictEqual(files.added, []);
  173. });
  174. it('should record nothing as changed', function() {
  175. var file = path.join(main, 'one.scss');
  176. var files = watcher.removed(file);
  177. assert.deepStrictEqual(files.changed, []);
  178. });
  179. it('should record itself as removed', function() {
  180. var file = path.join(main, 'one.scss');
  181. var files = watcher.removed(file);
  182. assert.deepStrictEqual(files.removed, [file]);
  183. });
  184. });
  185. });
  186. describe('and is not in the graph', function() {
  187. describe('if it is a partial', function() {
  188. it('should record nothing', function() {
  189. var file = path.join(sibling, 'partials', '_three.scss');
  190. var files = watcher.removed(file);
  191. assert.deepStrictEqual(files, {
  192. added: [],
  193. changed: [],
  194. removed: [],
  195. });
  196. });
  197. });
  198. describe('if it is not a partial', function() {
  199. it('should record nothing', function() {
  200. var file = path.join(sibling, 'three.scss');
  201. var files = watcher.removed(file);
  202. assert.deepStrictEqual(files, {
  203. added: [],
  204. changed: [],
  205. removed: [],
  206. });
  207. });
  208. });
  209. });
  210. });
  211. });
  212. describe('with file', function() {
  213. beforeEach(function() {
  214. watcher.reset({
  215. src: path.join(main, 'one.scss'),
  216. includePath: [main]
  217. });
  218. });
  219. describe('when a file is changed', function() {
  220. describe('and it is in the graph', function() {
  221. describe('if it is a partial', function() {
  222. it('should record its descendants as added', function() {
  223. var file = path.join(main, 'partials', '_one.scss');
  224. var files = watcher.changed(file);
  225. assert.deepStrictEqual(files.added, [
  226. path.join(main, 'partials', '_three.scss'),
  227. ]);
  228. });
  229. it('should record its ancenstors as changed', function() {
  230. var file = path.join(main, 'partials', '_one.scss');
  231. var files = watcher.changed(file);
  232. assert.deepStrictEqual(files.changed, [
  233. path.join(main, 'one.scss'),
  234. ]);
  235. });
  236. it('should record nothing as removed', function() {
  237. var file = path.join(main, 'partials', '_one.scss');
  238. var files = watcher.changed(file);
  239. assert.deepStrictEqual(files.removed, []);
  240. });
  241. });
  242. describe('if it is not a partial', function() {
  243. it('should record its descendants as added', function() {
  244. var file = path.join(main, 'one.scss');
  245. var files = watcher.changed(file);
  246. assert.deepStrictEqual(files.added, [
  247. path.join(main, 'partials', '_one.scss'),
  248. path.join(main, 'partials', '_three.scss'),
  249. ]);
  250. });
  251. it('should record itself as changed', function() {
  252. var file = path.join(main, 'one.scss');
  253. var files = watcher.changed(file);
  254. assert.deepStrictEqual(files.changed, [file]);
  255. });
  256. it('should record nothing as removed', function() {
  257. var file = path.join(main, 'one.scss');
  258. var files = watcher.changed(file);
  259. assert.deepStrictEqual(files.removed, []);
  260. });
  261. });
  262. });
  263. describe('and it is not in the graph', function() {
  264. describe('if it is a partial', function() {
  265. it('should record nothing', function() {
  266. var file = path.join(sibling, 'partials', '_three.scss');
  267. var files = watcher.changed(file);
  268. assert.deepStrictEqual(files, {
  269. added: [],
  270. changed: [],
  271. removed: [],
  272. });
  273. });
  274. });
  275. describe('if it is not a partial', function() {
  276. it('should record nothing as added', function() {
  277. var file = path.join(sibling, 'three.scss');
  278. var files = watcher.changed(file);
  279. assert.deepStrictEqual(files.added, []);
  280. });
  281. it('should record itself as changed', function() {
  282. var file = path.join(sibling, 'three.scss');
  283. var files = watcher.changed(file);
  284. assert.deepStrictEqual(files.changed, [file]);
  285. });
  286. it('should record nothing as removed', function() {
  287. var file = path.join(sibling, 'three.scss');
  288. var files = watcher.changed(file);
  289. assert.deepStrictEqual(files.removed, []);
  290. });
  291. });
  292. });
  293. });
  294. describe('when a file is added', function() {
  295. describe('and it is in the graph', function() {
  296. it('should record nothing as added', function() {
  297. var file = path.join(main, 'partials', '_three.scss');
  298. var files = watcher.added(file);
  299. assert.deepStrictEqual(files.added, []);
  300. });
  301. it('should record its descendants as added', function() {
  302. var file = path.join(main, 'partials', '_one.scss');
  303. var files = watcher.added(file);
  304. assert.deepStrictEqual(files.added, [
  305. path.join(main, 'partials', '_three.scss'),
  306. ]);
  307. });
  308. it('should record nothing as changed', function() {
  309. var file = path.join(main, 'partials', '_three.scss');
  310. var files = watcher.added(file);
  311. assert.deepStrictEqual(files.changed, []);
  312. });
  313. it('should record nothing as removed', function() {
  314. var file = path.join(main, 'partials', '_three.scss');
  315. var files = watcher.added(file);
  316. assert.deepStrictEqual(files.removed, []);
  317. });
  318. });
  319. describe('and it is not in the graph', function() {
  320. beforeEach(function() {
  321. watcher.reset({
  322. src: path.join(main, 'two.scss'),
  323. includePath: [main]
  324. });
  325. });
  326. describe('if it is a partial', function() {
  327. it('should record nothing as added', function() {
  328. var file = path.join(main, 'partials', '_three.scss');
  329. var files = watcher.added(file);
  330. assert.deepStrictEqual(files.added, [
  331. file,
  332. ]);
  333. });
  334. it('should not record its descendants as added', function() {
  335. var file = path.join(main, 'partials', '_one.scss');
  336. var files = watcher.added(file);
  337. assert.deepStrictEqual(files.added, [
  338. file,
  339. ]);
  340. });
  341. it('should record nothing as changed', function() {
  342. var file = path.join(main, 'partials', '_three.scss');
  343. var files = watcher.added(file);
  344. assert.deepStrictEqual(files.changed, []);
  345. });
  346. it('should record nothing as removed', function() {
  347. var file = path.join(main, 'partials', '_three.scss');
  348. var files = watcher.added(file);
  349. assert.deepStrictEqual(files.removed, []);
  350. });
  351. });
  352. describe('if it is not a partial', function() {
  353. it('should record itself as added', function() {
  354. var file = path.join(main, 'three.scss');
  355. var files = watcher.added(file);
  356. assert.deepStrictEqual(files.added, [
  357. file,
  358. ]);
  359. });
  360. it('should record nothing as changed', function() {
  361. var file = path.join(main, 'one.scss');
  362. var files = watcher.added(file);
  363. assert.deepStrictEqual(files.changed, []);
  364. });
  365. it('should record nothing as removed', function() {
  366. var file = path.join(main, 'one.scss');
  367. var files = watcher.added(file);
  368. assert.deepStrictEqual(files.removed, []);
  369. });
  370. });
  371. });
  372. });
  373. describe('when a file is removed', function() {
  374. describe('and it is in the graph', function() {
  375. describe('if it is a partial', function() {
  376. it('should record nothing as added', function() {
  377. var file = path.join(main, 'partials', '_one.scss');
  378. var files = watcher.removed(file);
  379. assert.deepStrictEqual(files.added, []);
  380. });
  381. it('should record its ancestors as changed', function() {
  382. var file = path.join(main, 'partials', '_one.scss');
  383. var files = watcher.removed(file);
  384. assert.deepStrictEqual(files.changed, [
  385. path.join(main, 'one.scss'),
  386. ]);
  387. });
  388. it('should record itself as removed', function() {
  389. var file = path.join(main, 'partials', '_one.scss');
  390. var files = watcher.removed(file);
  391. assert.deepStrictEqual(files.removed, [file]);
  392. });
  393. });
  394. describe('if it is not a partial', function() {
  395. it('should record nothing as added', function() {
  396. var file = path.join(main, 'one.scss');
  397. var files = watcher.removed(file);
  398. assert.deepStrictEqual(files.added, []);
  399. });
  400. it('should record nothing as changed', function() {
  401. var file = path.join(main, 'one.scss');
  402. var files = watcher.removed(file);
  403. assert.deepStrictEqual(files.changed, []);
  404. });
  405. it('should record itself as removed', function() {
  406. var file = path.join(main, 'one.scss');
  407. var files = watcher.removed(file);
  408. assert.deepStrictEqual(files.removed, [file]);
  409. });
  410. });
  411. });
  412. describe('and is not in the graph', function() {
  413. beforeEach(function() {
  414. watcher.reset({
  415. src: path.join(main, 'two.scss'),
  416. includePath: [main]
  417. });
  418. });
  419. describe('if it is a partial', function() {
  420. it('should record nothing as added', function() {
  421. var file = path.join(main, 'partials', '_one.scss');
  422. var files = watcher.removed(file);
  423. assert.deepStrictEqual(files, {
  424. added: [],
  425. changed: [],
  426. removed: [],
  427. });
  428. });
  429. });
  430. describe('if it is not a partial', function() {
  431. it('should record nothing', function() {
  432. var file = path.join(main, 'one.scss');
  433. var files = watcher.removed(file);
  434. assert.deepStrictEqual(files, {
  435. added: [],
  436. changed: [],
  437. removed: [],
  438. });
  439. });
  440. });
  441. });
  442. });
  443. });
  444. });