Skip to content

Commit

Permalink
Merge pull request #268 from nalajcie/master
Browse files Browse the repository at this point in the history
fix syncing utimes if copied files
  • Loading branch information
XhmikosR committed Feb 25, 2016
2 parents ade2465 + 147a84b commit 829032d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
7 changes: 5 additions & 2 deletions tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function(grunt) {
var fs = require('fs');
var chalk = require('chalk');
var fileSyncCmp = require('file-sync-cmp');
var isWindows = process.platform === 'win32';

grunt.registerMultiTask('copy', 'Copy files.', function() {

Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = function(grunt) {
};

var unixifyPath = function(filepath) {
if (process.platform === 'win32') {
if (isWindows) {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
Expand All @@ -119,6 +120,8 @@ module.exports = function(grunt) {
return;
}

fs.utimesSync(dest, stat.atime, stat.mtime);
var fd = fs.openSync(dest, isWindows ? 'r+' : 'r');
fs.futimesSync(fd, stat.atime, stat.mtime);
fs.closeSync(fd);
};
};
23 changes: 17 additions & 6 deletions test/copy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var grunt = require('grunt');
var fs = require('fs');
var isWindows = process.platform === 'win32';

exports.copy = {
main: function(test) {
Expand Down Expand Up @@ -64,8 +65,10 @@ exports.copy = {

modeDir: function(test) {
test.expect(2);
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder').mode.toString(8).slice(-3), '777');
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder/sub_folder').mode.toString(8).slice(-3), '777');
// on windows DIRs do not have 'executable' flag, see: https://github.com/nodejs/node/blob/master/deps/uv/src/win/fs.c#L1064
var expectedMode = isWindows ? '666' : '777';
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder').mode.toString(8).slice(-3), expectedMode);
test.equal(fs.lstatSync('tmp/copy_test_modeDir/time_folder/sub_folder').mode.toString(8).slice(-3), expectedMode);
test.done();
},

Expand All @@ -77,14 +80,22 @@ exports.copy = {
test.done();
},

timestamp: function(test) {
test.expect(4);

timestamp_equal: function(test) {
if (isWindows) {
// Known Issue: this test will not pass on Windows due to bug in nodejs (https://github.com/nodejs/node/issues/2069)
test.done();
return;
}
test.expect(2);
test.equal(fs.lstatSync('tmp/copy_test_timestamp/sub_folder').mtime.getTime(), fs.lstatSync('test/fixtures/time_folder/sub_folder').mtime.getTime());
test.equal(fs.lstatSync('tmp/copy_test_timestamp/test.js').mtime.getTime(), fs.lstatSync('test/fixtures/time_folder/test.js').mtime.getTime());
test.done();
},

timestamp_changed: function(test) {
test.expect(2);
test.notEqual(fs.lstatSync('tmp/copy_test_timestamp/test1.js').mtime.getTime(), fs.lstatSync('test/fixtures/time_folder/test.js').mtime.getTime());
test.notEqual(fs.lstatSync('tmp/copy_test_timestamp/test_process.js').mtime.getTime(), fs.lstatSync('test/fixtures/time_folder/test_process.js').mtime.getTime());

test.done();
}
};

0 comments on commit 829032d

Please sign in to comment.