Skip to content

Commit

Permalink
feat(cp): support update flag when recursing (#889)
Browse files Browse the repository at this point in the history
Fixed `cp -Ru` ignoring `-u`, and added a test.

Fixes #808
  • Loading branch information
joshi-sh authored and nfischer committed Nov 13, 2018
1 parent 5da1dda commit 18d8bbf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cp.js
Expand Up @@ -260,7 +260,7 @@ function _cp(options, sources, dest) {

try {
common.statFollowLinks(path.dirname(dest));
cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink });
cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink, update: options.update });
} catch (e) {
/* istanbul ignore next */
common.error("cannot create directory '" + dest + "': No such file or directory");
Expand Down
44 changes: 44 additions & 0 deletions test/cp.js
Expand Up @@ -451,6 +451,50 @@ test('-R implies -P', t => {
});
});

test('-Ru respects the -u flag recursively (don\'t update newer file)', t => {
// Setup code
const TWO_DAYS_IN_MS = 2 * 24 * 60 * 60 * 1000;
const dir = `${t.context.tmp}/cp-Ru`;
const sourceDir = `${dir}/old`;
const sourceFile = `${sourceDir}/file`;
const destDir = `${dir}/new`;
const destFile = `${destDir}/file`;
[sourceDir, destDir].forEach(d => shell.mkdir('-p', d));
shell.ShellString('Source File Contents\n').to(sourceFile);
shell.ShellString('Destination File Contents\n').to(destFile);
// End setup
// Get the old mtime for dest
const oldTime = fs.statSync(destFile).mtimeMs;
// Set the source file to be older than the destination file
shell.touch('-m', oldTime - TWO_DAYS_IN_MS, sourceFile);
// Now, copy the old dir to the new one
shell.cp('-Ru', sourceDir, destDir);
// Check that dest has not been updated
t.is(shell.cat(destFile).stdout, 'Destination File Contents\n');
});

test('-Ru respects the -u flag recursively (update older file)', t => {
// Setup code
const TWO_DAYS_IN_MS = 2 * 24 * 60 * 60 * 1000;
const dir = `${t.context.tmp}/cp-Ru`;
const sourceDir = `${dir}/old`;
const sourceFile = `${sourceDir}/file`;
const destDir = `${dir}/new`;
const destFile = `${destDir}/file`;
[sourceDir, destDir].forEach(d => shell.mkdir('-p', d));
shell.ShellString('Source File Contents\n').to(sourceFile);
shell.ShellString('Destination File Contents\n').to(destFile);
// End setup
// Get the old mtime for dest
const oldTime = fs.statSync(destFile).mtimeMs;
// Set the destination file to be older than the source file
shell.touch('-m', oldTime + TWO_DAYS_IN_MS, sourceFile);
// Now, copy the old dir to the new one
shell.cp('-Ru', sourceDir, destDir);
// Check that dest has been updated
t.is(shell.cat(sourceFile).stdout, 'Source File Contents\n');
});

test('using -P explicitly works', t => {
utils.skipOnWin(t, () => {
shell.cp('-P', 'test/resources/cp/links/sym.lnk', t.context.tmp);
Expand Down

0 comments on commit 18d8bbf

Please sign in to comment.