Skip to content

Commit

Permalink
Fix: Ignore atime/mtime if they are invalid (fixes #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna authored and phated committed Nov 28, 2017
1 parent b72fe8b commit 86492a7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

var fs = require('graceful-fs');

// http://stackoverflow.com/a/10589791/586382
function validDate(date) {
return date instanceof Date && !isNaN(date.valueOf());
}

function utimes(writePath, stat, cb) {
if (stat.mtime) {
var atime = stat.atime || new Date();
var mtime = stat.mtime;
var atime = stat.atime || new Date();

return fs.utimes(writePath, atime, stat.mtime, cb);
if (validDate(mtime) && validDate(atime)) {
return fs.utimes(writePath, atime, mtime, cb);
}

cb();
Expand Down
79 changes: 79 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,85 @@ describe('dest stream', function() {
stream.end();
});

it('should not modify file mtime and atime when provided mtime on the vinyl stat is invalid', function(done) {
var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedContents = fs.readFileSync(inputPath);
var expectedCwd = __dirname;
var expectedBase = path.join(__dirname, './out-fixtures');
var expectedMtime = new Date();
var invalidMtime = new Date(undefined);

var expectedFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: expectedContents,
stat: {
mtime: invalidMtime
}
});

var onEnd = function(){
buffered.length.should.equal(1);
buffered[0].should.equal(expectedFile);
fs.existsSync(expectedPath).should.equal(true);
fs.lstatSync(expectedPath).mtime.setMilliseconds(0).should.equal(expectedMtime.setMilliseconds(0));
done();
};

var stream = vfs.dest('./out-fixtures/', {cwd: __dirname});

var buffered = [];
var bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);

stream.pipe(bufferStream);
stream.write(expectedFile);
stream.end();
});

it('should not modify file mtime and atime when provided atime on the vinyl stat is invalid', function(done) {
var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedContents = fs.readFileSync(inputPath);
var expectedCwd = __dirname;
var expectedBase = path.join(__dirname, './out-fixtures');
var expectedAtime = new Date();
var expectedMtime = new Date();
var invalidAtime = new Date(undefined);

var expectedFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: expectedContents,
stat: {
atime: invalidAtime,
mtime: expectedMtime
}
});

var onEnd = function(){
buffered.length.should.equal(1);
buffered[0].should.equal(expectedFile);
fs.existsSync(expectedPath).should.equal(true);
fs.lstatSync(expectedPath).atime.setMilliseconds(0).should.equal(expectedAtime.setMilliseconds(0));
fs.lstatSync(expectedPath).mtime.setMilliseconds(0).should.equal(expectedMtime.setMilliseconds(0));
done();
};

var stream = vfs.dest('./out-fixtures/', {cwd: __dirname});

var buffered = [];
var bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);

stream.pipe(bufferStream);
stream.write(expectedFile);
stream.end();
});

it('should write file atime and mtime using the vinyl stat', function(done) {
var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
Expand Down

0 comments on commit 86492a7

Please sign in to comment.