recursive-readdir-test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* eslint-env mocha */
  2. var assert = require("assert");
  3. var p = require("path");
  4. var readdir = require("../index");
  5. function getAbsolutePath(file) {
  6. return p.join(__dirname, file);
  7. }
  8. function getAbsolutePaths(files) {
  9. return files.map(getAbsolutePath);
  10. }
  11. describe("readdir", function() {
  12. it("correctly lists all files in nested directories", function(done) {
  13. var expectedFiles = getAbsolutePaths([
  14. "/testdir/a/a",
  15. "/testdir/a/beans",
  16. "/testdir/b/123",
  17. "/testdir/b/b/hurp-durp",
  18. "/testdir/c.txt",
  19. "/testdir/d.txt"
  20. ]);
  21. readdir(p.join(__dirname, "testdir"), function(err, list) {
  22. assert.ifError(err);
  23. assert.deepEqual(list.sort(), expectedFiles.sort());
  24. done();
  25. });
  26. });
  27. it("ignores the files listed in the ignores array", function(done) {
  28. var notExpectedFiles = getAbsolutePaths([
  29. "/testdir/d.txt",
  30. "/testdir/a/beans"
  31. ]);
  32. readdir(p.join(__dirname, "testdir"), ["d.txt", "beans"], function(
  33. err,
  34. list
  35. ) {
  36. assert.ifError(err);
  37. list.forEach(function(file) {
  38. assert.equal(
  39. notExpectedFiles.indexOf(file),
  40. -1,
  41. 'Failed to ignore file "' + file + '".'
  42. );
  43. });
  44. done();
  45. });
  46. });
  47. it("ignores the directories listed in the ignores array", function(done) {
  48. var notExpectedFiles = getAbsolutePaths([
  49. "/testdir/a/a",
  50. "/testdir/a/beans"
  51. ]);
  52. readdir(p.join(__dirname, "testdir"), ["**/testdir/a"], function(
  53. err,
  54. list
  55. ) {
  56. assert.ifError(err);
  57. list.forEach(function(file) {
  58. assert.equal(
  59. notExpectedFiles.indexOf(file),
  60. -1,
  61. 'Failed to ignore file "' + file + '".'
  62. );
  63. });
  64. done();
  65. });
  66. });
  67. it("ignores symlinked files and directories listed in the ignores array", function(
  68. done
  69. ) {
  70. var notExpectedFiles = getAbsolutePaths([
  71. "/testsymlinks/testdir/linkeddir/hi.docx",
  72. "/testsymlinks/testdir/linkedfile.wmf"
  73. ]);
  74. readdir(
  75. p.join(__dirname, "testsymlinks/testdir"),
  76. ["linkeddir", "linkedfile.wmf"],
  77. function(err, list) {
  78. assert.ifError(err);
  79. list.forEach(function(file) {
  80. assert.equal(
  81. notExpectedFiles.indexOf(file),
  82. -1,
  83. 'Failed to ignore file "' + file + '".'
  84. );
  85. });
  86. done();
  87. }
  88. );
  89. });
  90. it("supports ignoring files with just basename globbing", function(done) {
  91. var notExpectedFiles = getAbsolutePaths([
  92. "/testdir/d.txt",
  93. "/testdir/a/beans"
  94. ]);
  95. readdir(p.join(__dirname, "testdir"), ["*.txt", "beans"], function(
  96. err,
  97. list
  98. ) {
  99. assert.ifError(err);
  100. list.forEach(function(file) {
  101. assert.equal(
  102. notExpectedFiles.indexOf(file),
  103. -1,
  104. 'Failed to ignore file "' + file + '".'
  105. );
  106. });
  107. done();
  108. });
  109. });
  110. it("supports ignoring files with the globstar syntax", function(done) {
  111. var notExpectedFiles = getAbsolutePaths([
  112. "/testdir/d.txt",
  113. "/testdir/a/beans"
  114. ]);
  115. var ignores = ["**/*.txt", "**/a/beans"];
  116. readdir(p.join(__dirname, "testdir"), ignores, function(err, list) {
  117. assert.ifError(err);
  118. list.forEach(function(file) {
  119. assert.equal(
  120. notExpectedFiles.indexOf(file),
  121. -1,
  122. 'Failed to ignore file "' + file + '".'
  123. );
  124. });
  125. done();
  126. });
  127. });
  128. context("when there is a function in the ignores array", function() {
  129. it("passes each file and directory path to the function", function(done) {
  130. var expectedPaths = getAbsolutePaths([
  131. "/testdir/a",
  132. "/testdir/a/a",
  133. "/testdir/a/beans",
  134. "/testdir/b",
  135. "/testdir/b/123",
  136. "/testdir/b/b",
  137. "/testdir/b/b/hurp-durp",
  138. "/testdir/c.txt",
  139. "/testdir/d.txt"
  140. ]);
  141. var paths = [];
  142. function ignoreFunction(path) {
  143. paths.push(path);
  144. return false;
  145. }
  146. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  147. err,
  148. list
  149. ) {
  150. assert.ifError(err);
  151. assert.deepEqual(paths.sort(), expectedPaths.sort());
  152. done();
  153. });
  154. });
  155. it("passes the stat object of each file to the function as its second argument", function(
  156. done
  157. ) {
  158. var paths = {};
  159. function ignoreFunction(path, stats) {
  160. paths[path] = stats;
  161. return false;
  162. }
  163. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  164. err,
  165. list
  166. ) {
  167. assert.ifError(err);
  168. assert(paths[getAbsolutePath("/testdir/a")].isDirectory());
  169. assert(paths[getAbsolutePath("/testdir/c.txt")].isFile());
  170. done();
  171. });
  172. });
  173. it("ignores files that the function returns true for", function(done) {
  174. var ignoredFiles = getAbsolutePaths([
  175. "/testdir/d.txt",
  176. "/testdir/a/beans"
  177. ]);
  178. function ignoreFunction(path) {
  179. return ignoredFiles.indexOf(path) != -1;
  180. }
  181. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  182. err,
  183. list
  184. ) {
  185. assert.ifError(err);
  186. list.forEach(function(file) {
  187. assert.equal(
  188. ignoredFiles.indexOf(file),
  189. -1,
  190. 'Failed to ignore file "' + file + '".'
  191. );
  192. });
  193. done();
  194. });
  195. });
  196. it("does not ignore files that the function returns false for", function(
  197. done
  198. ) {
  199. var notIgnoredFiles = getAbsolutePaths([
  200. "/testdir/d.txt",
  201. "/testdir/a/beans"
  202. ]);
  203. function ignoreFunction(path) {
  204. return notIgnoredFiles.indexOf(path) == -1;
  205. }
  206. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  207. err,
  208. list
  209. ) {
  210. assert.ifError(err);
  211. notIgnoredFiles.forEach(function(file) {
  212. assert.notEqual(
  213. notIgnoredFiles.indexOf(file),
  214. -1,
  215. 'Incorrectly ignored file "' + file + '".'
  216. );
  217. });
  218. done();
  219. });
  220. });
  221. it("ignores directories that the function returns true for", function(
  222. done
  223. ) {
  224. var ignoredDirectory = getAbsolutePath("/testdir/a");
  225. var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
  226. function ignoreFunction(path) {
  227. return ignoredDirectory == path;
  228. }
  229. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  230. err,
  231. list
  232. ) {
  233. assert.ifError(err);
  234. list.forEach(function(file) {
  235. assert.equal(
  236. ignoredFiles.indexOf(file),
  237. -1,
  238. 'Failed to ignore file "' + file + '".'
  239. );
  240. });
  241. done();
  242. });
  243. });
  244. it("does not ignore directories that the function returns false for", function(
  245. done
  246. ) {
  247. var ignoredDirectory = getAbsolutePath("/testdir/a");
  248. var notIgnoredFiles = getAbsolutePaths([
  249. "/testdir/b/123",
  250. "/testdir/b/b/hurp-durp"
  251. ]);
  252. function ignoreFunction(path) {
  253. return ignoredDirectory == path;
  254. }
  255. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  256. err,
  257. list
  258. ) {
  259. assert.ifError(err);
  260. notIgnoredFiles.forEach(function(file) {
  261. assert.notEqual(
  262. notIgnoredFiles.indexOf(file),
  263. -1,
  264. 'Incorrectly ignored file "' + file + '".'
  265. );
  266. });
  267. done();
  268. });
  269. });
  270. it("does not descend into directories that the function returns true for", function(
  271. done
  272. ) {
  273. var ignoredDirectory = getAbsolutePath("/testdir/a");
  274. var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
  275. var paths = [];
  276. function ignoreFunction(path) {
  277. paths.push(path);
  278. return ignoredDirectory == path;
  279. }
  280. readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
  281. err,
  282. list
  283. ) {
  284. assert.ifError(err);
  285. paths.forEach(function(file) {
  286. assert.equal(
  287. ignoredFiles.indexOf(file),
  288. -1,
  289. 'Transversed file in ignored directory "' + file + '".'
  290. );
  291. });
  292. done();
  293. });
  294. });
  295. });
  296. it("works when there are no files to report except ignored files", function(
  297. done
  298. ) {
  299. readdir(p.join(__dirname, "testdirBeta"), ["*"], function(err, list) {
  300. assert.ifError(err);
  301. assert.equal(list.length, 0, "expect to report 0 files");
  302. done();
  303. });
  304. });
  305. it("works when negated ignore list is given", function(done) {
  306. var expectedFiles = getAbsolutePaths(["/testdirBeta/ignore.txt"]);
  307. readdir(p.join(__dirname, "testdirBeta"), ["!*.txt"], function(err, list) {
  308. assert.ifError(err);
  309. assert.deepEqual(
  310. list.sort(),
  311. expectedFiles,
  312. "Failed to find expected files."
  313. );
  314. done();
  315. });
  316. });
  317. it("traverses directory and file symbolic links", function(done) {
  318. var expectedFiles = getAbsolutePaths([
  319. "/testsymlinks/testdir/linkeddir/hi.docx",
  320. "/testsymlinks/testdir/linkedfile.wmf"
  321. ]);
  322. readdir(p.join(__dirname, "testsymlinks", "testdir"), function(err, list) {
  323. assert.ifError(err);
  324. assert.deepEqual(
  325. list.sort(),
  326. expectedFiles,
  327. "Failed to find expected files."
  328. );
  329. done();
  330. });
  331. });
  332. if (!global.Promise) {
  333. console.log("Native Promise not supported - skipping tests");
  334. } else {
  335. it("works with promises", function(done) {
  336. var expectedFiles = getAbsolutePaths([
  337. "/testdir/a/a",
  338. "/testdir/a/beans",
  339. "/testdir/b/123",
  340. "/testdir/b/b/hurp-durp",
  341. "/testdir/c.txt",
  342. "/testdir/d.txt"
  343. ]);
  344. readdir(p.join(__dirname, "testdir"))
  345. .then(function(list) {
  346. assert.deepEqual(list.sort(), expectedFiles.sort());
  347. done();
  348. })
  349. .catch(done);
  350. });
  351. it("correctly ignores when using promises", function(done) {
  352. var expectedFiles = getAbsolutePaths([
  353. "/testdir/a/a",
  354. "/testdir/a/beans",
  355. "/testdir/b/123",
  356. "/testdir/b/b/hurp-durp"
  357. ]);
  358. readdir(p.join(__dirname, "testdir"), ["*.txt"])
  359. .then(function(list) {
  360. assert.deepEqual(list.sort(), expectedFiles.sort());
  361. done();
  362. })
  363. .catch(done);
  364. });
  365. }
  366. });