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
/**
 * Module dependencies.
 */
 
var dir = require('../lib/dir');
var should = require('should');
var path = require('path');
var fs = require('fs');
var exec = require('child_process').exec;
 
describe('dir', function() {
  describe('#walk()', function() {
    var root = path.dirname(__dirname) + '/';
 
    function check(dir, files) {
      fs.statSync(dir).isDirectory().should.be.true;
      files.should.be.a('object');
      Object.keys(files).length.should.equal(fs.readdirSync(dir).length);
      for (var k in files) {
        k.should.be.a('string');
        k.should.include(dir);
        var stats = files[k];
        stats.should.be.an.instanceof(fs.Stats);
      }
    }
 
    it('should walk dir all', function(done) {
      var walker = new dir.Walk(root);
      walker.on('dir', check);
      walker.on('end', done);
    });
 
    it('should walk dir all in callback', function(done) {
      var walker = new dir.Walk(root, check, done);
    });
 
    it('walk(dir, ondir, onend, onerr) should worked', function(done) {
      dir.walk(root, check, done, function(err, p) {
        console.log(p);
        throw err;
      });
    });
 
    it('should success when walk empty dir', function(done) {
      dir.walk(__dirname + '/emptydir', check, done);
    });
  });
 
  describe('#copyfile()', function() {
    var from = __dirname + '/dir.test.foo.txt';
    var to = __dirname + '/dir.test.bar.txt';
    var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt';
    before(function() {
      path.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) {
          err.should.be.an.instanceof(Error);
          done();
        });
      });
    });
    it('should copy toParentNotExists', function(done) {
      dir.copyfile(from, toParentNotExists, function(exists, next) {
        exists.should.be.false;
        next(true);
      }, 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';
    before(function(done) {
      !path.existsSync(existsDir) && fs.mkdirSync(existsDir);
      exec('rm -rf /tmp/dir.test', done);
    });
 
    after(function() {
      fs.rmdirSync(existsDir);
    });
 
    it('should make exists dir success', function(done) {
      path.existsSync(existsDir).should.be.true;
      dir.mkdir(existsDir, function(err) {
        path.existsSync(existsDir).should.be.true;
        done(err);
      });
    });
 
    it('should make not exists dir success', function(done) {
      path.existsSync(notExistsDir).should.be.false;
      dir.mkdir(notExistsDir, function(err) {
        path.existsSync(notExistsDir).should.be.true;
        done(err);
      });
    });
  });
});