reader.js 1.4 KB

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