Skip to content

Commit

Permalink
Check if atime Date object is invalid and set a new one explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna committed Oct 27, 2015
1 parent a28ba42 commit f575377
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ function validDate(date) {
}

function utimes(writePath, stat, cb) {
// Given `mtime` is not valid, do nothing
var mtime = stat.mtime;
var atime = stat.atime || new Date();
if (!validDate(mtime)) {
return cb();
}

if (validDate(mtime) && validDate(atime)) {
return fs.utimes(writePath, atime, mtime, cb);
// Given `atime` is not valid, assign a new one with current time
var atime = stat.atime;
if (!validDate(atime)) {
atime = new Date();
}

cb();
// Set file `atime` and `mtime` fields
fs.utimes(writePath, atime, mtime, cb);
}

module.exports = utimes;
12 changes: 8 additions & 4 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,15 +697,15 @@ describe('dest stream', function() {
stream.end();
});

it('should not modify file mtime and atime when provided atime on the vinyl stat is invalid', function(done) {
it('should write file mtime when provided mtime on the vinyl stat is valid but provided atime 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 expectedMtime = fs.lstatSync(inputPath).mtime;
var invalidAtime = new Date(undefined);

var expectedFile = new File({
Expand All @@ -719,12 +719,16 @@ describe('dest stream', function() {
}
});

// Node.js uses `utime()`, so `fs.utimes()` has a resolution of 1 second
expectedAtime.setMilliseconds(0)
expectedMtime.setMilliseconds(0)

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));
fs.lstatSync(expectedPath).atime.getTime().should.equal(expectedAtime.getTime());
fs.lstatSync(expectedPath).mtime.getTime().should.equal(expectedMtime.getTime());
done();
};

Expand Down

0 comments on commit f575377

Please sign in to comment.