pause-resume.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // run from the root of the project
  12. // this is usually where you're at anyway, but be sure.
  13. process.chdir(path.resolve(__dirname, ".."))
  14. function alphasort (a, b) {
  15. a = a.toLowerCase()
  16. b = b.toLowerCase()
  17. return a > b ? 1 : a < b ? -1 : 0
  18. }
  19. function cleanResults (m) {
  20. // normalize discrepancies in ordering, duplication,
  21. // and ending slashes.
  22. return m.map(function (m) {
  23. return m.replace(/\/+/g, "/").replace(/\/$/, "")
  24. }).sort(alphasort).reduce(function (set, f) {
  25. if (f !== set[set.length - 1]) set.push(f)
  26. return set
  27. }, []).sort(alphasort).map(function (f) {
  28. // de-windows
  29. return (process.platform !== 'win32') ? f
  30. : f.replace(/^[a-zA-Z]:\\\\/, '/').replace(/\\/g, '/')
  31. })
  32. }
  33. var globResults = []
  34. tap.test("use a Glob object, and pause/resume it", function (t) {
  35. var g = new Glob(pattern)
  36. , paused = false
  37. , res = []
  38. , expect = bashResults[pattern]
  39. g.on("pause", function () {
  40. console.error("pause")
  41. })
  42. g.on("resume", function () {
  43. console.error("resume")
  44. })
  45. g.on("match", function (m) {
  46. t.notOk(g.paused, "must not be paused")
  47. globResults.push(m)
  48. g.pause()
  49. t.ok(g.paused, "must be paused")
  50. setTimeout(g.resume.bind(g), 10)
  51. })
  52. g.on("end", function (matches) {
  53. t.pass("reached glob end")
  54. globResults = cleanResults(globResults)
  55. matches = cleanResults(matches)
  56. t.deepEqual(matches, globResults,
  57. "end event matches should be the same as match events")
  58. t.deepEqual(matches, expect,
  59. "glob matches should be the same as bash results")
  60. t.end()
  61. })
  62. })