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