dir.test.foo.txt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Module dependencies.
  3. */
  4. var dir = require('../lib/dir');
  5. var should = require('should');
  6. var path = require('path');
  7. var fs = require('fs');
  8. var exec = require('child_process').exec;
  9. describe('dir', function() {
  10. describe('#walk()', function() {
  11. var root = path.dirname(__dirname) + '/';
  12. function check(dir, files) {
  13. fs.statSync(dir).isDirectory().should.be.true;
  14. files.should.be.a('object');
  15. Object.keys(files).length.should.equal(fs.readdirSync(dir).length);
  16. for (var k in files) {
  17. k.should.be.a('string');
  18. k.should.include(dir);
  19. var stats = files[k];
  20. stats.should.be.an.instanceof(fs.Stats);
  21. }
  22. }
  23. it('should walk dir all', function(done) {
  24. var walker = new dir.Walk(root);
  25. walker.on('dir', check);
  26. walker.on('end', done);
  27. });
  28. it('should walk dir all in callback', function(done) {
  29. var walker = new dir.Walk(root, check, done);
  30. });
  31. it('walk(dir, ondir, onend, onerr) should worked', function(done) {
  32. dir.walk(root, check, done, function(err, p) {
  33. console.log(p);
  34. throw err;
  35. });
  36. });
  37. it('should success when walk empty dir', function(done) {
  38. dir.walk(__dirname + '/emptydir', check, done);
  39. });
  40. });
  41. describe('#copyfile()', function() {
  42. var from = __dirname + '/dir.test.foo.txt';
  43. var to = __dirname + '/dir.test.bar.txt';
  44. var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt';
  45. before(function() {
  46. path.existsSync(to) && fs.unlinkSync(to);
  47. });
  48. it('should worked', function(done) {
  49. dir.copyfile(from, to, function(err) {
  50. should.not.exist(err);
  51. fs.statSync(to).isFile().should.be.true;
  52. fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
  53. dir.copyfile(to, to, function(err) {
  54. err.should.be.an.instanceof(Error);
  55. done();
  56. });
  57. });
  58. });
  59. it('should copy toParentNotExists', function(done) {
  60. dir.copyfile(from, toParentNotExists, function(exists, next) {
  61. exists.should.be.false;
  62. next(true);
  63. }, function(err) {
  64. should.not.exist(err);
  65. fs.statSync(toParentNotExists).isFile().should.be.true;
  66. fs.readFileSync(toParentNotExists).toString().should.equal(fs.readFileSync(from).toString());
  67. done();
  68. });
  69. });
  70. });
  71. describe('#mkdir()', function() {
  72. var existsDir = '/tmp/dir.test.exists.dir';
  73. var notExistsDir = '/tmp/dir.test/not.exists.dir';
  74. before(function(done) {
  75. !path.existsSync(existsDir) && fs.mkdirSync(existsDir);
  76. exec('rm -rf /tmp/dir.test', done);
  77. });
  78. after(function() {
  79. fs.rmdirSync(existsDir);
  80. });
  81. it('should make exists dir success', function(done) {
  82. path.existsSync(existsDir).should.be.true;
  83. dir.mkdir(existsDir, function(err) {
  84. path.existsSync(existsDir).should.be.true;
  85. done(err);
  86. });
  87. });
  88. it('should make not exists dir success', function(done) {
  89. path.existsSync(notExistsDir).should.be.false;
  90. dir.mkdir(notExistsDir, function(err) {
  91. path.existsSync(notExistsDir).should.be.true;
  92. done(err);
  93. });
  94. });
  95. });
  96. });