Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ncp.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var assert = require('assert'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. rimraf = require('rimraf'),
  5. readDirFiles = require('read-dir-files'),
  6. util = require('util'),
  7. ncp = require('../').ncp;
  8. describe('ncp', function () {
  9. describe('regular files and directories', function () {
  10. var fixtures = path.join(__dirname, 'regular-fixtures'),
  11. src = path.join(fixtures, 'src'),
  12. out = path.join(fixtures, 'out');
  13. before(function (cb) {
  14. rimraf(out, function() {
  15. ncp(src, out, cb);
  16. });
  17. });
  18. describe('when copying a directory of files', function () {
  19. it('files are copied correctly', function (cb) {
  20. readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
  21. readDirFiles(out, 'utf8', function (outErr, outFiles) {
  22. assert.ifError(srcErr);
  23. assert.deepEqual(srcFiles, outFiles);
  24. cb();
  25. });
  26. });
  27. });
  28. });
  29. describe('when copying files using filter', function () {
  30. before(function (cb) {
  31. var filter = function(name) {
  32. return name.substr(name.length - 1) != 'a';
  33. };
  34. rimraf(out, function () {
  35. ncp(src, out, {filter: filter}, cb);
  36. });
  37. });
  38. it('files are copied correctly', function (cb) {
  39. readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
  40. function filter(files) {
  41. for (var fileName in files) {
  42. var curFile = files[fileName];
  43. if (curFile instanceof Object)
  44. return filter(curFile);
  45. if (fileName.substr(fileName.length - 1) == 'a')
  46. delete files[fileName];
  47. }
  48. }
  49. filter(srcFiles);
  50. readDirFiles(out, 'utf8', function (outErr, outFiles) {
  51. assert.ifError(outErr);
  52. assert.deepEqual(srcFiles, outFiles);
  53. cb();
  54. });
  55. });
  56. });
  57. });
  58. describe('when using clobber=false', function () {
  59. it('the copy is completed successfully', function (cb) {
  60. ncp(src, out, function() {
  61. ncp(src, out, {clobber: false}, function(err) {
  62. assert.ifError(err);
  63. cb();
  64. });
  65. });
  66. });
  67. });
  68. describe('when using transform', function () {
  69. it('file descriptors are passed correctly', function (cb) {
  70. ncp(src, out, {
  71. transform: function(read,write,file) {
  72. assert.notEqual(file.name, undefined);
  73. assert.strictEqual(typeof file.mode,'number');
  74. read.pipe(write);
  75. }
  76. }, cb);
  77. });
  78. });
  79. describe('when using rename', function() {
  80. it('output files are correctly redirected', function(cb) {
  81. ncp(src, out, {
  82. rename: function(target) {
  83. if(path.basename(target) == 'a') return path.resolve(path.dirname(target), 'z');
  84. return target;
  85. }
  86. }, function(err) {
  87. if(err) return cb(err);
  88. readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
  89. readDirFiles(out, 'utf8', function (outErr, outFiles) {
  90. assert.ifError(srcErr);
  91. assert.deepEqual(srcFiles.a, outFiles.z);
  92. cb();
  93. });
  94. });
  95. });
  96. });
  97. });
  98. });
  99. describe('symlink handling', function () {
  100. var fixtures = path.join(__dirname, 'symlink-fixtures'),
  101. src = path.join(fixtures, 'src'),
  102. out = path.join(fixtures, 'out');
  103. beforeEach(function (cb) {
  104. rimraf(out, cb);
  105. });
  106. it('copies symlinks by default', function (cb) {
  107. ncp(src, out, function (err) {
  108. if (err) return cb(err);
  109. assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), 'foo');
  110. assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), 'dir');
  111. cb();
  112. })
  113. });
  114. it('copies file contents when dereference=true', function (cb) {
  115. ncp(src, out, { dereference: true }, function (err) {
  116. var fileSymlinkPath = path.join(out, 'file-symlink');
  117. assert.ok(fs.lstatSync(fileSymlinkPath).isFile());
  118. assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents');
  119. var dirSymlinkPath = path.join(out, 'dir-symlink');
  120. assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory());
  121. assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']);
  122. cb();
  123. });
  124. });
  125. });
  126. describe('broken symlink handling', function () {
  127. var fixtures = path.join(__dirname, 'broken-symlink-fixtures'),
  128. src = path.join(fixtures, 'src'),
  129. out = path.join(fixtures, 'out');
  130. beforeEach(function (cb) {
  131. rimraf(out, cb);
  132. });
  133. it('copies broken symlinks by default', function (cb) {
  134. ncp(src, out, function (err) {
  135. if (err) return cb(err);
  136. assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), 'does-not-exist');
  137. cb();
  138. })
  139. });
  140. it('returns an error when dereference=true', function (cb) {
  141. ncp(src, out, {dereference: true}, function (err) {
  142. assert.equal(err.length, 1);
  143. assert.equal(err[0].code, 'ENOENT');
  144. cb();
  145. });
  146. });
  147. });
  148. describe('modified files copies', function () {
  149. var fixtures = path.join(__dirname, 'modified-files'),
  150. src = path.join(fixtures, 'src'),
  151. out = path.join(fixtures, 'out');
  152. it('if file not exists copy file to target', function(cb) {
  153. rimraf(out, function() {
  154. ncp(src, out, {modified: true, clobber: false}, function (err) {
  155. assert.equal(fs.existsSync(out), true);
  156. cb();
  157. });
  158. });
  159. });
  160. it('change source file mtime and copy', function(cb) {
  161. fs.utimesSync(src+"/a", new Date().getTime()/1000, new Date('2015-01-01 00:00:00').getTime()/1000);
  162. ncp(src, out, {modified: true, clobber: false}, function (err) {
  163. fs.stat(out+"/a", function(err, stats) {
  164. assert.equal(stats.mtime.getTime(), new Date('2015-01-01 00:00:00').getTime());
  165. cb();
  166. });
  167. });
  168. });
  169. });
  170. });