reader.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var fstream = require("../fstream.js")
  2. var tap = require("tap")
  3. var fs = require("fs")
  4. var path = require("path")
  5. var children = -1
  6. var dir = path.dirname(__dirname)
  7. var gotReady = false
  8. var ended = false
  9. tap.test("reader test", function (t) {
  10. var r = fstream.Reader({ path: dir
  11. , filter: function () {
  12. // return this.parent === r
  13. return this.parent === r || this === r
  14. }
  15. })
  16. r.on("ready", function () {
  17. gotReady = true
  18. children = fs.readdirSync(dir).length
  19. console.error("Setting expected children to "+children)
  20. t.equal(r.type, "Directory", "should be a directory")
  21. })
  22. r.on("entry", function (entry) {
  23. children --
  24. if (!gotReady) {
  25. t.fail("children before ready!")
  26. }
  27. t.equal(entry.dirname, r.path, "basename is parent dir")
  28. })
  29. r.on("error", function (er) {
  30. t.fail(er)
  31. t.end()
  32. process.exit(1)
  33. })
  34. r.on("end", function () {
  35. t.equal(children, 0, "should have seen all children")
  36. ended = true
  37. })
  38. var closed = false
  39. r.on("close", function () {
  40. t.ok(ended, "saw end before close")
  41. t.notOk(closed, "close should only happen once")
  42. closed = true
  43. t.end()
  44. })
  45. })