333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 * Module dependencies.
 */
 
var dir = require('../');
var should = require('../node_modules/should');
var path = require('path');
var fs = require('fs');
var exec = require('child_process').exec;
fs.existsSync = fs.existsSync || path.existsSync;
var root = path.resolve('.');
 
describe('ndir', function () {
  describe('walk()', function () {
    var emptydir = path.join(root, 'test/emptydir');
 
    before(function () {
      if (!fs.existsSync(emptydir)) {
        fs.mkdirSync(emptydir, '0777');
      }
    });
    after(function () {
      if (fs.existsSync(emptydir)) {
        fs.rmdirSync(emptydir);
      }
    });
 
    function check(dir, files) {
      fs.statSync(dir).isDirectory().should.be.true;
      files.should.be.an.instanceof(Array);
      files.length.should.equal(fs.readdirSync(dir).length);
      for (var i = 0, l = files.length; i < l; i++) {
        var info = files[i];
        info[0].should.be.a('string');
        info[0].should.include(dir);
        var stats = info[1];
        stats.should.be.an.instanceof(fs.Stats);
      }
    }
 
    var walkdir = path.join(root, 'test');
    it('should walk dir ' + walkdir, function end(done) {
      var walker = new dir.Walk(walkdir);
      walker.on('dir', check);
      var dirCount = 1;
      var fileCount = 0;
      walker.on('dir', function (dirpath, files) {
        for (var i = 0, l = files.length; i < l; i++) {
          var info = files[i];
          var stats = info[1];
          if (stats.isDirectory()) {
            dirCount++;
          } else if (!stats.isSymbolicLink() && stats.isFile()) {
            fileCount++;
          }
        }
      });
      walker.on('end', function () {
        dirCount.should.equal(2);
        fileCount.should.equal(4);
        done();
      });
    });
 
    it('should walk "' + root + '" in callback mode', function (done) {
      var walker = new dir.Walk(__dirname, check, done);
    });
 
    it('should walk "' + root + '" no error', function (done) {
      dir.walk(root, check, done, function (err, p) {
        should.not.exist(err);
        should.not.exist(p);
      });
    });
 
    it('should success when walk empty dir', function (done) {
      dir.walk(emptydir, check, done, function (err, p) {
        should.not.exist(err);
        should.not.exist(p);
      });
    });
 
    it('should error when walk not exists dir', function (done) {
      dir.walk('test/not-exists-dir', check, done, function (err) {
        err.should.be.an.instanceof(Error);
        err.message.should.include('ENOENT');
      });
    });
 
    it('should error when walk a file', function (done) {
      dir.walk('test/ndir.test.js', check, done, function (err) {
        err.should.be.an.instanceof(Error);
        err.message.should.include('ENOTDIR');
      });
    });
 
    if (fs.existsSync('/.fseventsd')) {
      it('should error when walk noPermission dir', function (done) {
        dir.walk('/.fseventsd', check, done, function (err) {
          err.should.be.an.instanceof(Error);
          err.message.should.include('EACCES');
        });
      });
    }
    
  });
 
  describe('copyfile()', function () {
    var from = 'test/dir.test.foo.txt';
    var to = 'test/dir.test.bar.txt';
    var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt';
 
    before(function () {
      fs.existsSync(to) && fs.unlinkSync(to);
    });
 
    it('should worked', function (done) {
      dir.copyfile(from, to, function (err) {
        should.not.exist(err);
        fs.statSync(to).isFile().should.be.true;
        fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
        dir.copyfile(to, to, function (err) {
          // copy save should callback(err)
          err.should.be.an.instanceof(Error);
          err.message.should.include('not copied');
          fs.statSync(to).isFile().should.be.true;
          fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
          done();
        });
      });
    });
 
    it('should copy toParentNotExists', function (done) {
      dir.copyfile(from, toParentNotExists, function (err) {
        should.not.exist(err);
        fs.statSync(toParentNotExists).isFile().should.be.true;
        fs.readFileSync(toParentNotExists).toString().should.equal(fs.readFileSync(from).toString());
        done();
      });
    });
 
  });
 
  describe('mkdir()', function () {
    var existsDir = '/tmp/dir.test.exists.dir';
    var notExistsDir = '/tmp/dir.test/not.exists.dir/haha/1/2/3/4/2/3/1/2/3';
 
    before(function (done) {
      !fs.existsSync(existsDir) && fs.mkdirSync(existsDir, '0777');
      exec('rm -rf /tmp/dir.test', done);
    });
 
    after(function () {
      fs.rmdirSync(existsDir);
    });
 
    it('should make exists dir success', function (done) {
      fs.existsSync(existsDir).should.be.true;
      dir.mkdir(existsDir, function (err) {
        fs.existsSync(existsDir).should.be.true;
        done(err);
      });
    });
 
    it('should make not exists dir success', function (done) {
      fs.existsSync(notExistsDir).should.be.false;
      dir.mkdir(notExistsDir, function (err) {
        fs.existsSync(notExistsDir).should.be.true;
        done(err);
      });
    });
 
  });
 
  describe('createLineReader()', function () {
    it('should read line by line', function (done) {
      var logfile = __dirname + '/access.log';
      var lines = fs.readFileSync(logfile, 'utf8').split('\n');
      var index = 0;
      dir.createLineReader(logfile)
      .on('line', function (line) {
        line.should.be.an.instanceof(Buffer);
        var s = line.toString();
        s.should.equal(lines[index++]);
        if (s) {
          s[s.length - 1].should.not.equal('\n');
        }
      })
      .on('end', done)
      .on('error', done);
    });
  });
});