[Pkg-javascript-commits] [node-ncp] 04/05: Imported Upstream version 0.6.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Wed Jul 9 23:14:16 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-ncp.

commit 9f8883fa7ca47078c9d82eea4a69c47ea534d5bc
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Wed Jul 9 23:13:57 2014 +0000

    Imported Upstream version 0.6.0
---
 .gitignore                                      |   2 +-
 README.md                                       |   5 +
 lib/ncp.js                                      |  29 ++--
 package.json                                    |   2 +-
 test/broken-symlink-fixtures/src/broken-symlink |   1 +
 test/ncp.js                                     | 191 +++++++++++++++++-------
 test/{fixtures => regular-fixtures}/src/a       |   0
 test/{fixtures => regular-fixtures}/src/b       |   0
 test/{fixtures => regular-fixtures}/src/c       |   0
 test/{fixtures => regular-fixtures}/src/d       |   0
 test/{fixtures => regular-fixtures}/src/e       |   0
 test/{fixtures => regular-fixtures}/src/f       |   0
 test/{fixtures => regular-fixtures}/src/sub/a   |   0
 test/{fixtures => regular-fixtures}/src/sub/b   |   0
 test/symlink-fixtures/src/dir-symlink           |   1 +
 test/symlink-fixtures/src/dir/bar               |   1 +
 test/symlink-fixtures/src/file-symlink          |   1 +
 test/symlink-fixtures/src/foo                   |   1 +
 18 files changed, 168 insertions(+), 66 deletions(-)

diff --git a/.gitignore b/.gitignore
index 9ecd205..3e6a4d7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
 node_modules
 .*.sw[op]
 .DS_Store
-test/fixtures/out
+test/*fixtures/out
diff --git a/README.md b/README.md
index 71dca8f..9480032 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,11 @@ You can also call ncp like `ncp(source, destination, options, callback)`.
   * `options.clobber` - boolean=true. if set to false, `ncp` will not overwrite 
   destination files that already exist.
 
+  * `options.dereference` - boolean=false. If set to true, `ncp` will follow symbolic
+  links. For example, a symlink in the source tree pointing to a regular file
+  will become a regular file in the destination tree. Broken symlinks will result in
+  errors.
+
   * `options.stopOnErr` - boolean=false.  If set to true, `ncp` will behave like `cp -r`,
   and stop on the first error it encounters. By default, `ncp` continues copying, logging all
   errors and returning an array.
diff --git a/lib/ncp.js b/lib/ncp.js
index f0d9f6d..fb7930c 100644
--- a/lib/ncp.js
+++ b/lib/ncp.js
@@ -1,6 +1,8 @@
 var fs = require('fs'),
     path = require('path');
 
+const modern = /^v0\.1\d\.\d+$/.test(process.version);
+
 module.exports = ncp;
 ncp.ncp = ncp;
 
@@ -16,10 +18,13 @@ function ncp (source, dest, options, callback) {
       currentPath = path.resolve(basePath, source),
       targetPath = path.resolve(basePath, dest),
       filter = options.filter,
+      rename = options.rename,
       transform = options.transform,
       clobber = options.clobber !== false,
+      dereference = options.dereference,
       errs = null,
-      eventName = /^v0\.10\.\d+$/.test(process.version) ? 'finish' : 'close',
+      eventName = modern ? 'finish' : 'close',
+      defer = modern ? setImmediate : process.nextTick,
       started = 0,
       finished = 0,
       running = 0,
@@ -46,20 +51,15 @@ function ncp (source, dest, options, callback) {
     return getStats(source);
   }
 
-  function defer(fn) {
-    if (typeof(setImmediate) === 'function')
-      return setImmediate(fn);
-    return process.nextTick(fn);
-  }
-
   function getStats(source) {
+    var stat = dereference ? fs.stat : fs.lstat;
     if (running >= limit) {
       return defer(function () {
         getStats(source);
       });
     }
     running++;
-    fs.lstat(source, function (err, stats) {
+    stat(source, function (err, stats) {
       var item = {};
       if (err) {
         return onError(err);
@@ -84,6 +84,9 @@ function ncp (source, dest, options, callback) {
 
   function onFile(file) {
     var target = file.name.replace(currentPath, targetPath);
+    if(rename) {
+      target =  rename(target);
+    }
     isWritable(target, function (writable) {
       if (writable) {
         return copyFile(file, target);
@@ -101,10 +104,16 @@ function ncp (source, dest, options, callback) {
   function copyFile(file, target) {
     var readStream = fs.createReadStream(file.name),
         writeStream = fs.createWriteStream(target, { mode: file.mode });
+    
+    readStream.on('error', onError);
+    writeStream.on('error', onError);
+    
     if(transform) {
-      transform(readStream, writeStream,file);
+      transform(readStream, writeStream, file);
     } else {
-      readStream.pipe(writeStream);
+      writeStream.on('open', function() {
+        readStream.pipe(writeStream);
+      });
     }
     writeStream.once(eventName, cb);
   }
diff --git a/package.json b/package.json
index d4d67e3..67364d6 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     "name" : "ncp",
-    "version" : "0.5.1",
+    "version" : "0.6.0",
     "author": "AvianFlu <charlie at charlieistheman.com>",
     "description" : "Asynchronous recursive file copy utility.",
     "bin": {
diff --git a/test/broken-symlink-fixtures/src/broken-symlink b/test/broken-symlink-fixtures/src/broken-symlink
new file mode 120000
index 0000000..cfa0a46
--- /dev/null
+++ b/test/broken-symlink-fixtures/src/broken-symlink
@@ -0,0 +1 @@
+does-not-exist
\ No newline at end of file
diff --git a/test/ncp.js b/test/ncp.js
index d9798f9..3ddc310 100644
--- a/test/ncp.js
+++ b/test/ncp.js
@@ -1,86 +1,169 @@
 
 
 var assert = require('assert'),
+    fs = require('fs'),
     path = require('path'),
     rimraf = require('rimraf'),
     readDirFiles = require('read-dir-files'),
     ncp = require('../').ncp;
 
 
-var fixtures = path.join(__dirname, 'fixtures'),
-    src = path.join(fixtures, 'src'),
-    out = path.join(fixtures, 'out');
-
 describe('ncp', function () {
-  before(function (cb) {
-    rimraf(out, function() {
-      ncp(src, out, cb);
+  describe('regular files and directories', function () {
+    var fixtures = path.join(__dirname, 'regular-fixtures'),
+        src = path.join(fixtures, 'src'),
+        out = path.join(fixtures, 'out');
+
+    before(function (cb) {
+      rimraf(out, function() {
+        ncp(src, out, cb);
+      });
     });
-  });
 
-  describe('when copying a directory of files', function () {
-    it('files are copied correctly', function (cb) {
-      readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
-        readDirFiles(out, 'utf8', function (outErr, outFiles) {
-          assert.ifError(srcErr);
-          assert.deepEqual(srcFiles, outFiles);
-          cb();
+    describe('when copying a directory of files', function () {
+      it('files are copied correctly', function (cb) {
+        readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
+          readDirFiles(out, 'utf8', function (outErr, outFiles) {
+            assert.ifError(srcErr);
+            assert.deepEqual(srcFiles, outFiles);
+            cb();
+          });
         });
       });
     });
-  });
 
-  describe('when copying files using filter', function () {
-    before(function (cb) {
-      var filter = function(name) {
-        return name.substr(name.length - 1) != 'a';
-      };
-      rimraf(out, function () {
-        ncp(src, out, {filter: filter}, cb);
+    describe('when copying files using filter', function () {
+      before(function (cb) {
+        var filter = function(name) {
+          return name.substr(name.length - 1) != 'a';
+        };
+        rimraf(out, function () {
+          ncp(src, out, {filter: filter}, cb);
+        });
+      });
+
+      it('files are copied correctly', function (cb) {
+        readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
+          function filter(files) {
+            for (var fileName in files) {
+              var curFile = files[fileName];
+              if (curFile instanceof Object)
+                return filter(curFile);
+              if (fileName.substr(fileName.length - 1) == 'a')
+                delete files[fileName];
+            }
+          }
+          filter(srcFiles);
+          readDirFiles(out, 'utf8', function (outErr, outFiles) {
+            assert.ifError(outErr);
+            assert.deepEqual(srcFiles, outFiles);
+            cb();
+          });
+        });
+      });
+    });
+
+    describe('when using clobber=false', function () {
+      it('the copy is completed successfully', function (cb) {
+        ncp(src, out, function() {
+          ncp(src, out, {clobber: false}, function(err) {
+            assert.ifError(err);
+            cb();
+          });
+        });
+      });
+    });
+
+    describe('when using transform', function () {
+      it('file descriptors are passed correctly', function (cb) {
+        ncp(src, out, {
+           transform: function(read,write,file) {
+              assert.notEqual(file.name, undefined);
+              assert.strictEqual(typeof file.mode,'number');
+              read.pipe(write);
+           }
+        }, cb);
       });
     });
 
-    it('files are copied correctly', function (cb) {
-      readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
-        function filter(files) {
-          for (var fileName in files) {
-            var curFile = files[fileName];
-            if (curFile instanceof Object)
-              return filter(curFile);
-            if (fileName.substr(fileName.length - 1) == 'a')
-              delete files[fileName];
+    describe('when using rename', function() {
+      it('output files are correctly redirected', function(cb) {
+        ncp(src, out, {
+          rename: function(target) {
+            if(path.basename(target) == 'a') return path.resolve(path.dirname(target), 'z');
+            return target;
           }
-        }
-        filter(srcFiles);
-        readDirFiles(out, 'utf8', function (outErr, outFiles) {
-          assert.ifError(outErr);
-          assert.deepEqual(srcFiles, outFiles);
-          cb();
+        }, function(err) {
+          if(err) return cb(err);
+
+          readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
+            readDirFiles(out, 'utf8', function (outErr, outFiles) {
+              assert.ifError(srcErr);
+              assert.deepEqual(srcFiles.a, outFiles.z);
+              cb();
+            });
+          });
         });
       });
     });
   });
 
-  describe('when using clobber=false', function () {
-    it('the copy is completed successfully', function (cb) {
-      ncp(src, out, function() {
-        ncp(src, out, {clobber: false}, function(err) {
-          assert.ifError(err);
-          cb();
-        });
+  describe('symlink handling', function () {
+    var fixtures = path.join(__dirname, 'symlink-fixtures'),
+        src = path.join(fixtures, 'src'),
+        out = path.join(fixtures, 'out');
+
+    beforeEach(function (cb) {
+      rimraf(out, cb);
+    });
+
+    it('copies symlinks by default', function (cb) {
+      ncp(src, out, function (err) {
+        if (err) return cb(err);
+        assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), 'foo');
+        assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), 'dir');
+        cb();
+      })
+    });
+
+    it('copies file contents when dereference=true', function (cb) {
+      ncp(src, out, { dereference: true }, function (err) {
+        var fileSymlinkPath = path.join(out, 'file-symlink');
+        assert.ok(fs.lstatSync(fileSymlinkPath).isFile());
+        assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents');
+
+        var dirSymlinkPath = path.join(out, 'dir-symlink');
+        assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory());
+        assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']);
+
+        cb();
       });
     });
   });
 
-  describe('when using transform', function () {
-    it('file descriptors are passed correctly', function (cb) {
-      ncp(src, out, {
-         transform: function(read,write,file) {
-            assert.notEqual(file.name, undefined);
-            assert.strictEqual(typeof file.mode,'number');
-            read.pipe(write);
-         }
-      }, cb);
+  describe('broken symlink handling', function () {
+    var fixtures = path.join(__dirname, 'broken-symlink-fixtures'),
+        src = path.join(fixtures, 'src'),
+        out = path.join(fixtures, 'out');
+
+    beforeEach(function (cb) {
+      rimraf(out, cb);
+    });
+
+    it('copies broken symlinks by default', function (cb) {
+      ncp(src, out, function (err) {
+        if (err) return cb(err);
+        assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), 'does-not-exist');
+        cb();
+      })
+    });
+
+    it('returns an error when dereference=true', function (cb) {
+      ncp(src, out, {dereference: true}, function (err) {
+        assert.equal(err.length, 1);
+        assert.equal(err[0].code, 'ENOENT');
+        cb();
+      });
     });
   });
 });
diff --git a/test/fixtures/src/a b/test/regular-fixtures/src/a
similarity index 100%
rename from test/fixtures/src/a
rename to test/regular-fixtures/src/a
diff --git a/test/fixtures/src/b b/test/regular-fixtures/src/b
similarity index 100%
rename from test/fixtures/src/b
rename to test/regular-fixtures/src/b
diff --git a/test/fixtures/src/c b/test/regular-fixtures/src/c
similarity index 100%
rename from test/fixtures/src/c
rename to test/regular-fixtures/src/c
diff --git a/test/fixtures/src/d b/test/regular-fixtures/src/d
similarity index 100%
rename from test/fixtures/src/d
rename to test/regular-fixtures/src/d
diff --git a/test/fixtures/src/e b/test/regular-fixtures/src/e
similarity index 100%
rename from test/fixtures/src/e
rename to test/regular-fixtures/src/e
diff --git a/test/fixtures/src/f b/test/regular-fixtures/src/f
similarity index 100%
rename from test/fixtures/src/f
rename to test/regular-fixtures/src/f
diff --git a/test/fixtures/src/sub/a b/test/regular-fixtures/src/sub/a
similarity index 100%
rename from test/fixtures/src/sub/a
rename to test/regular-fixtures/src/sub/a
diff --git a/test/fixtures/src/sub/b b/test/regular-fixtures/src/sub/b
similarity index 100%
rename from test/fixtures/src/sub/b
rename to test/regular-fixtures/src/sub/b
diff --git a/test/symlink-fixtures/src/dir-symlink b/test/symlink-fixtures/src/dir-symlink
new file mode 120000
index 0000000..8724519
--- /dev/null
+++ b/test/symlink-fixtures/src/dir-symlink
@@ -0,0 +1 @@
+dir
\ No newline at end of file
diff --git a/test/symlink-fixtures/src/dir/bar b/test/symlink-fixtures/src/dir/bar
new file mode 100644
index 0000000..fd06f5f
--- /dev/null
+++ b/test/symlink-fixtures/src/dir/bar
@@ -0,0 +1 @@
+bar contents
\ No newline at end of file
diff --git a/test/symlink-fixtures/src/file-symlink b/test/symlink-fixtures/src/file-symlink
new file mode 120000
index 0000000..1910281
--- /dev/null
+++ b/test/symlink-fixtures/src/file-symlink
@@ -0,0 +1 @@
+foo
\ No newline at end of file
diff --git a/test/symlink-fixtures/src/foo b/test/symlink-fixtures/src/foo
new file mode 100644
index 0000000..35fc060
--- /dev/null
+++ b/test/symlink-fixtures/src/foo
@@ -0,0 +1 @@
+foo contents
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ncp.git



More information about the Pkg-javascript-commits mailing list