aboutsummaryrefslogtreecommitdiff
path: root/node_modules/walkdir/test/async.js
blob: ef02ab487c5e7ab5e88581762dffcda7046a29e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var test = require('tape'),
walkdir = require('../walkdir.js');

var expectedPaths = {
'dir/foo/x':'file',
'dir/foo/a':'dir',
'dir/foo/a/y':'file',
'dir/foo/a/b':'dir',
'dir/foo/a/b/z':'file',
'dir/foo/a/b/c':'dir',
'dir/foo/a/b/c/w':'file'
};

test('async events',function(t){
  var paths = [],
  files = [],
  dirs = [];


  var emitter = walkdir(__dirname+'/dir/foo',function(path){
    //console.log('path: ',path);
    paths.push(path.replace(__dirname+'/',''));
  });

  emitter.on('directory',function(path,stat){
    dirs.push(path.replace(__dirname+'/',''));
  });

  emitter.on('file',function(path,stat){
    //console.log('file: ',path); 
    files.push(path.replace(__dirname+'/',''));
  });

  emitter.on('end',function(){

     files.forEach(function(v,k){
       t.equals(expectedPaths[v],'file','path from file event should be file');  
     });

     var expected = Object.keys(expectedPaths);

     t.ok(expected.length == paths.length, 'expected and emitted paths should have the same length');

     expected.forEach(function(v,k){
       if(expectedPaths[v] == 'file') {
          t.ok(files.indexOf(v) > -1,'should have file in files array');
       }
     });

     dirs.forEach(function(v,k){
       t.equals(expectedPaths[v],'dir','path from dir event should be dir '+v);  
     });

     expected.forEach(function(v,k){
       if(expectedPaths[v] == 'dir') {
          t.ok(dirs.indexOf(v) > -1,'should have dir in dirs array');
       }
     });

     expected.forEach(function(v,k){
       t.ok(paths.indexOf(v) !== -1,'should have found all expected paths '+v);
     });

     t.end();
  });
});