ndir.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Module dependencies.
  3. */
  4. var dir = require('../');
  5. var should = require('../node_modules/should');
  6. var path = require('path');
  7. var fs = require('fs');
  8. var exec = require('child_process').exec;
  9. var root = path.resolve('.');
  10. describe('ndir', function() {
  11. describe('#walk()', function() {
  12. var emptydir = path.join(root, 'test/emptydir');
  13. before(function() {
  14. if (!path.existsSync(emptydir)) {
  15. fs.mkdirSync(emptydir);
  16. }
  17. });
  18. after(function() {
  19. if (path.existsSync(emptydir)) {
  20. fs.rmdirSync(emptydir);
  21. }
  22. });
  23. function check(dir, files) {
  24. fs.statSync(dir).isDirectory().should.be.true;
  25. files.should.be.an.instanceof(Array);
  26. files.length.should.equal(fs.readdirSync(dir).length);
  27. for (var i = 0, l = files.length; i < l; i++) {
  28. var info = files[i];
  29. info[0].should.be.a('string');
  30. info[0].should.include(dir);
  31. var stats = info[1];
  32. stats.should.be.an.instanceof(fs.Stats);
  33. }
  34. }
  35. var walkdir = path.join(root, 'test');
  36. it('should walk dir ' + walkdir, function end(done) {
  37. var walker = new dir.Walk(walkdir);
  38. walker.on('dir', check);
  39. var dirCount = 1;
  40. var fileCount = 0;
  41. walker.on('dir', function(dirpath, files) {
  42. for (var i = 0, l = files.length; i < l; i++) {
  43. var info = files[i];
  44. var stats = info[1];
  45. if (stats.isDirectory()) {
  46. dirCount++;
  47. } else if(!stats.isSymbolicLink() && stats.isFile()) {
  48. fileCount++;
  49. }
  50. }
  51. });
  52. walker.on('end', function() {
  53. dirCount.should.equal(2);
  54. fileCount.should.equal(4);
  55. done();
  56. });
  57. });
  58. it('should walk "' + root + '" in callback mode', function(done) {
  59. var walker = new dir.Walk(__dirname, check, done);
  60. });
  61. it('should walk "' + root + '" no error', function(done) {
  62. dir.walk(root, check, done, function(err, p) {
  63. should.not.exist(err);
  64. should.not.exist(p);
  65. });
  66. });
  67. it('should success when walk empty dir', function(done) {
  68. dir.walk(emptydir, check, done, function(err, p) {
  69. should.not.exist(err);
  70. should.not.exist(p);
  71. });
  72. });
  73. it('should error when walk not exists dir', function(done) {
  74. dir.walk('test/not-exists-dir', check, done, function(err) {
  75. err.should.be.an.instanceof(Error);
  76. err.message.should.include('ENOENT, no such file or directory');
  77. });
  78. });
  79. it('should error when walk a file', function(done) {
  80. dir.walk('test/ndir.test.js', check, done, function(err) {
  81. err.should.be.an.instanceof(Error);
  82. err.message.should.include('ENOTDIR, not a directory');
  83. });
  84. });
  85. if (path.existsSync('/.fseventsd')) {
  86. it('should error when walk noPermission dir', function(done) {
  87. dir.walk('/.fseventsd', check, done, function(err) {
  88. err.should.be.an.instanceof(Error);
  89. err.message.should.include('EACCES, permission denied');
  90. });
  91. });
  92. }
  93. });
  94. describe('#copyfile()', function() {
  95. var from = 'test/dir.test.foo.txt';
  96. var to = 'test/dir.test.bar.txt';
  97. var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt';
  98. before(function() {
  99. path.existsSync(to) && fs.unlinkSync(to);
  100. });
  101. it('should worked', function(done) {
  102. dir.copyfile(from, to, function(err) {
  103. should.not.exist(err);
  104. fs.statSync(to).isFile().should.be.true;
  105. fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
  106. dir.copyfile(to, to, function(err) {
  107. // copy save should callback(err)
  108. err.should.be.an.instanceof(Error);
  109. err.message.should.include('not copied');
  110. fs.statSync(to).isFile().should.be.true;
  111. fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
  112. done();
  113. });
  114. });
  115. });
  116. it('should copy toParentNotExists', function(done) {
  117. dir.copyfile(from, toParentNotExists, function(err) {
  118. should.not.exist(err);
  119. fs.statSync(toParentNotExists).isFile().should.be.true;
  120. fs.readFileSync(toParentNotExists).toString().should.equal(fs.readFileSync(from).toString());
  121. done();
  122. });
  123. });
  124. });
  125. describe('#mkdir()', function() {
  126. var existsDir = '/tmp/dir.test.exists.dir';
  127. var notExistsDir = '/tmp/dir.test/not.exists.dir';
  128. before(function(done) {
  129. !path.existsSync(existsDir) && fs.mkdirSync(existsDir);
  130. exec('rm -rf /tmp/dir.test', done);
  131. });
  132. after(function() {
  133. fs.rmdirSync(existsDir);
  134. });
  135. it('should make exists dir success', function(done) {
  136. path.existsSync(existsDir).should.be.true;
  137. dir.mkdir(existsDir, function(err) {
  138. path.existsSync(existsDir).should.be.true;
  139. done(err);
  140. });
  141. });
  142. it('should make not exists dir success', function(done) {
  143. path.existsSync(notExistsDir).should.be.false;
  144. dir.mkdir(notExistsDir, function(err) {
  145. path.existsSync(notExistsDir).should.be.true;
  146. done(err);
  147. });
  148. });
  149. });
  150. describe('#createLineReader()', function() {
  151. it('should read line by line', function(done) {
  152. var logfile = __dirname + '/access.log';
  153. var lines = fs.readFileSync(logfile, 'utf8').split('\n');
  154. var index = 0;
  155. dir.createLineReader(logfile)
  156. .on('line', function(line) {
  157. line.should.be.an.instanceof(Buffer);
  158. var s = line.toString();
  159. s.should.equal(lines[index++]);
  160. if (s) {
  161. s[s.length - 1].should.not.equal('\n');
  162. }
  163. })
  164. .on('end', done)
  165. .on('error', done);
  166. });
  167. });
  168. });