summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/fstream-npm/node_modules/fstream-ignore/test/read-file-order.js
blob: 0d851164f2f2ca437a0f24a61b95d9422be4ba9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var IgnoreFile = require("../")
, fs = require('fs')

// set the ignores just for this test
var c = require("./common.js")
c.ignores({ ".gitignore": ["a/b/c/abc"] })
c.ignores({ ".ignore": ["*", "!a/b/c/abc"] })

// the only files we expect to see
var expected =
  [ "/a"
  , "/a/b"
  , "/a/b/c"
  , "/a/b/c/abc" ]

var originalReadFile = fs.readFile
, parallelCount = 0
, firstCall

// Overwrite fs.readFile so that when .gitignore and .ignore are read in
// parallel, .ignore will always be read first.
fs.readFile = function (filename, options, callback) {
  if (typeof options === 'function') {
    callback = options
    options = false
  }

  parallelCount++

  process.nextTick(function () {
    if (parallelCount > 1) {
      if (!firstCall) {
        return firstCall = function (cb) {
          originalReadFile(filename, options, function (err, data) {
            callback(err, data)
            if (cb) cb()
          })
        }
      }

      if (filename.indexOf('.gitignore') !== -1) {
        firstCall(function () {
          originalReadFile(filename, options, callback)
        })
      } else {
        originalReadFile(filename, options, function (err, data) {
          callback(err, data)
          firstCall()
        })
      }
    } else {
      originalReadFile(filename, options, callback)
      parallelCount = 0
    }
  })
}

require("tap").test("read file order", function (t) {
  t.pass("start")

  IgnoreFile({ path: __dirname + "/fixtures"
             , ignoreFiles: [".gitignore", ".ignore"] })
    .on("ignoreFile", function (e) {
      console.error("ignore file!", e)
    })
    .on("child", function (e) {
      var p = e.path.substr(e.root.path.length)
      var i = expected.indexOf(p)
      if (i === -1) {
        t.fail("unexpected file found", {f: p})
      } else {
        t.pass(p)
        expected.splice(i, 1)
      }
    })
    .on("close", function () {
      fs.readFile = originalReadFile
      t.notOk(expected.length, "all expected files should be seen")
      t.end()
    })
})