UNPKG

2.04 kBJavaScriptView Raw
1var IgnoreFile = require("../")
2, fs = require('fs')
3
4// set the ignores just for this test
5var c = require("./common.js")
6c.ignores({ ".gitignore": ["a/b/c/abc"] })
7c.ignores({ ".ignore": ["*", "!a/b/c/abc"] })
8
9// the only files we expect to see
10var expected =
11 [ "/a"
12 , "/a/b"
13 , "/a/b/c"
14 , "/a/b/c/abc" ]
15
16var originalReadFile = fs.readFile
17, parallelCount = 0
18, firstCall
19
20// Overwrite fs.readFile so that when .gitignore and .ignore are read in
21// parallel, .ignore will always be read first.
22fs.readFile = function (filename, options, callback) {
23 if (typeof options === 'function') {
24 callback = options
25 options = false
26 }
27
28 parallelCount++
29
30 process.nextTick(function () {
31 if (parallelCount > 1) {
32 if (!firstCall) {
33 return firstCall = function (cb) {
34 originalReadFile(filename, options, function (err, data) {
35 callback(err, data)
36 if (cb) cb()
37 })
38 }
39 }
40
41 if (filename.indexOf('.gitignore') !== -1) {
42 firstCall(function () {
43 originalReadFile(filename, options, callback)
44 })
45 } else {
46 originalReadFile(filename, options, function (err, data) {
47 callback(err, data)
48 firstCall()
49 })
50 }
51 } else {
52 originalReadFile(filename, options, callback)
53 parallelCount = 0
54 }
55 })
56}
57
58require("tap").test("read file order", function (t) {
59 t.pass("start")
60
61 IgnoreFile({ path: __dirname + "/fixtures"
62 , ignoreFiles: [".gitignore", ".ignore"] })
63 .on("ignoreFile", function (e) {
64 console.error("ignore file!", e)
65 })
66 .on("child", function (e) {
67 var p = e.path.substr(e.root.path.length)
68 var i = expected.indexOf(p)
69 if (i === -1) {
70 t.fail("unexpected file found", {f: p})
71 } else {
72 t.pass(p)
73 expected.splice(i, 1)
74 }
75 })
76 .on("close", function () {
77 fs.readFile = originalReadFile
78 t.notOk(expected.length, "all expected files should be seen")
79 t.end()
80 })
81})