Initial commit
[yaffs-website] / node_modules / globule / node_modules / glob / test / pause-resume.js
1 // show that no match events happen while paused.
2 var tap = require("tap")
3 , child_process = require("child_process")
4 // just some gnarly pattern with lots of matches
5 , pattern = "test/a/!(symlink)/**"
6 , bashResults = require("./bash-results.json")
7 , patterns = Object.keys(bashResults)
8 , glob = require("../")
9 , Glob = glob.Glob
10 , path = require("path")
11
12 // run from the root of the project
13 // this is usually where you're at anyway, but be sure.
14 process.chdir(path.resolve(__dirname, ".."))
15
16 function alphasort (a, b) {
17   a = a.toLowerCase()
18   b = b.toLowerCase()
19   return a > b ? 1 : a < b ? -1 : 0
20 }
21
22 function cleanResults (m) {
23   // normalize discrepancies in ordering, duplication,
24   // and ending slashes.
25   return m.map(function (m) {
26     return m.replace(/\/+/g, "/").replace(/\/$/, "")
27   }).sort(alphasort).reduce(function (set, f) {
28     if (f !== set[set.length - 1]) set.push(f)
29     return set
30   }, []).sort(alphasort).map(function (f) {
31     // de-windows
32     return (process.platform !== 'win32') ? f
33            : f.replace(/^[a-zA-Z]:\\\\/, '/').replace(/\\/g, '/')
34   })
35 }
36
37 var globResults = []
38 tap.test("use a Glob object, and pause/resume it", function (t) {
39   var g = new Glob(pattern)
40   , paused = false
41   , res = []
42   , expect = bashResults[pattern]
43
44   g.on("pause", function () {
45     console.error("pause")
46   })
47
48   g.on("resume", function () {
49     console.error("resume")
50   })
51
52   g.on("match", function (m) {
53     t.notOk(g.paused, "must not be paused")
54     globResults.push(m)
55     g.pause()
56     t.ok(g.paused, "must be paused")
57     setTimeout(g.resume.bind(g), 10)
58   })
59
60   g.on("end", function (matches) {
61     t.pass("reached glob end")
62     globResults = cleanResults(globResults)
63     matches = cleanResults(matches)
64     t.deepEqual(matches, globResults,
65       "end event matches should be the same as match events")
66
67     t.deepEqual(matches, expect,
68       "glob matches should be the same as bash results")
69
70     t.end()
71   })
72 })
73