ndir.test.js 5.7 KB

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