Skip to content

Commit

Permalink
Fixed behavior in case to has trailing path separator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lydie Danet committed Jun 9, 2016
1 parent 128570a commit a79261d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Expand Up @@ -217,7 +217,7 @@ var confirm = function(files, options, callback) {
});
}
}

/*istanbul ignore else - filtered should be an array, but just in case*/
if (filtered.length) {
filtered.forEach(function(file) {
Expand Down Expand Up @@ -304,6 +304,10 @@ var cpr = function(from, to, opts, callback) {
}
} else {
if (stat.isFile()) {
var dirRegex = new RegExp(path.sep + '$');
if (dirRegex.test(to)) { // Create directory if has trailing separator
to = path.join(to, path.basename(options.from));
}
return copyFile(options.from, to, options, callback);
}
callback(new Error('From should be a file or directory'));
Expand Down
49 changes: 29 additions & 20 deletions tests/full.js
Expand Up @@ -10,25 +10,25 @@ var assert = require('assert'),

describe('cpr test suite', function() {
this.timeout(55000);

describe('loading', function() {
before(function() {
rimraf.sync(to);
});

it('should export raw method', function () {
assert.equal(typeof cpr, 'function');
});

it('should export cpr method too', function () {
assert.equal(typeof cpr.cpr, 'function');
});
});

describe('should copy node_modules', function() {
var out = path.join(to, '0');
var data = {};

before(function(done) {
cpr(from, out, function(err, status) {
data = {
Expand All @@ -38,7 +38,7 @@ describe('cpr test suite', function() {
done();
});
});

it('has ./out/0', function() {
var stat = fs.statSync(out);
assert.ok(stat.isDirectory());
Expand All @@ -63,7 +63,7 @@ describe('cpr test suite', function() {
});

});

describe('should NOT copy node_modules', function() {
var out = path.join(to, '1'),
data;
Expand All @@ -81,15 +81,15 @@ describe('cpr test suite', function() {
});
});
});

it('does not have ./out/1', function() {
assert.ok(data.stat); // Should be an error
});
it('threw an error', function() {
assert(data.err instanceof Error); // Should be an error
assert.equal(data.err.message, 'No files to copy');
});

});

describe('should not copy yui-lint from regex', function() {
Expand All @@ -112,7 +112,7 @@ describe('cpr test suite', function() {
done();
});
});

it('returns files array with confirm', function() {
assert.ok(Array.isArray(data.status));
assert.ok(data.status.length > 0);
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('cpr test suite', function() {
});
assert.equal(false, toHas);
});

});

describe('should copy minimatch from bad filter', function() {
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('cpr test suite', function() {
});
assert.equal(true, toHasGFS);
});

});

describe('should copy node_modules with overwrite flag', function() {
Expand Down Expand Up @@ -270,11 +270,11 @@ describe('cpr test suite', function() {
});
assert.equal(true, toHasGFS);
});

});

describe('error handling', function() {

it('should fail on non-existant from dir', function(done) {
cpr('./does/not/exist', path.join(to, 'does/not/matter'), function(err, status) {
assert.equal(undefined, status);
Expand All @@ -283,7 +283,7 @@ describe('cpr test suite', function() {
done();
});
});

it('should fail on non-file', function(done) {
cpr('/dev/null', path.join(to, 'does/not/matter'), function(err, status) {
assert.equal(undefined, status);
Expand Down Expand Up @@ -325,7 +325,7 @@ describe('cpr test suite', function() {
});

describe('validations', function() {

it('should copy empty directory', function(done) {
mkdirp.sync(path.join(to, 'empty-src'));
cpr(path.join(to, 'empty-src'), path.join(to, 'empty-dest'), function() {
Expand All @@ -334,7 +334,7 @@ describe('cpr test suite', function() {
done();
});
});

it('should not delete existing folders in out dir', function(done) {
mkdirp.sync(path.join(to, 'empty-src', 'a'));
mkdirp.sync(path.join(to, 'empty-dest', 'b'));
Expand All @@ -347,7 +347,7 @@ describe('cpr test suite', function() {
done();
});
});

it('should copy one file', function(done) {
cpr(__filename, path.join(to, 'one-file-test.js'), { overwrite: true }, function(err) {
assert.equal(undefined, err);
Expand All @@ -357,6 +357,15 @@ describe('cpr test suite', function() {
});
});

it('should copy one file in dir if to has trailing sep', function(done) {
cpr(__filename, path.join(to, 'one-file-dir'+path.sep), { overwrite: true }, function(err) {
assert.equal(undefined, err);
var stat = fs.statSync(path.join(to, 'one-file-dir','full.js'));
assert.ok(stat.isFile());
done();
});
});

it('should not copy because file exists', function(done) {
cpr(__filename, path.join(to, 'one-file-test.js'), function(err, status) {
assert.equal(undefined, status);
Expand All @@ -383,7 +392,7 @@ describe('cpr test suite', function() {
done();
});
});

it('has ./out/4', function() {
var stat = fs.statSync(out);
assert.ok(stat.isDirectory());
Expand All @@ -403,7 +412,7 @@ describe('cpr test suite', function() {
});
assert.equal(true, toHasGFS);
});

});

});

0 comments on commit a79261d

Please sign in to comment.