Skip to content

Commit

Permalink
Update: When testing utime-related cases, check fs.futimes() argume…
Browse files Browse the repository at this point in the history
…nts instead of filesystem times (fixes #208)
  • Loading branch information
OpenGG authored and phated committed Nov 30, 2017
1 parent 75c8cb5 commit 97649a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
42 changes: 26 additions & 16 deletions test/dest-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('.dest() with custom times', function() {
return;
}

// Use the mtime of this file to have proper resolution
var mtime = fs.statSync(__filename).mtime;
// Use new mtime
var mtime = new Date(Date.now() - 2048);

var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();

Expand All @@ -83,10 +83,14 @@ describe('.dest() with custom times', function() {
});

function assert() {
var stats = fs.lstatSync(outputPath);

expect(futimesSpy.calls.length).toEqual(1);
expect(stats.mtime.getTime()).toEqual(mtime.getTime());

// Compare args instead of fs.lstats(), since mtime may be drifted in x86 Node.js
var mtimeSpy = futimesSpy.calls[0].arguments[2];

expect(mtimeSpy.getTime())
.toEqual(mtime.getTime());

expect(file.stat.mtime).toEqual(mtime);
}

Expand Down Expand Up @@ -136,8 +140,8 @@ describe('.dest() with custom times', function() {
return;
}

// Use the mtime of this file to have proper resolution
var mtime = fs.lstatSync(__filename).mtime;
// Use new mtime
var mtime = new Date(Date.now() - 2048);
var invalidAtime = new Date(undefined);

var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
Expand All @@ -153,10 +157,11 @@ describe('.dest() with custom times', function() {
});

function assert() {
var stats = fs.lstatSync(outputPath);

expect(futimesSpy.calls.length).toEqual(1);
expect(stats.mtime.getTime()).toEqual(mtime.getTime());

var mtimeSpy = futimesSpy.calls[0].arguments[2];

expect(mtimeSpy.getTime()).toEqual(mtime.getTime());
}

pipe([
Expand All @@ -172,9 +177,11 @@ describe('.dest() with custom times', function() {
return;
}

// Use the atime/mtime of this file to have proper resolution
var atime = fs.lstatSync(__filename).atime;
var mtime = fs.lstatSync(__filename).mtime;
// Use new atime/mtime
var atime = new Date(Date.now() - 2048);
var mtime = new Date(Date.now() - 1024);

var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();

var file = new File({
base: inputBase,
Expand All @@ -187,10 +194,13 @@ describe('.dest() with custom times', function() {
});

function assert() {
var stats = fs.lstatSync(outputPath);
expect(futimesSpy.calls.length).toEqual(1);

var atimeSpy = futimesSpy.calls[0].arguments[1];
var mtimeSpy = futimesSpy.calls[0].arguments[2];

expect(stats.atime.getTime()).toEqual(atime.getTime());
expect(stats.mtime.getTime()).toEqual(mtime.getTime());
expect(atimeSpy.getTime()).toEqual(atime.getTime());
expect(mtimeSpy.getTime()).toEqual(mtime.getTime());
expect(file.stat.mtime).toEqual(mtime);
expect(file.stat.atime).toEqual(atime);
};
Expand Down
30 changes: 16 additions & 14 deletions test/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,9 @@ describe('updateMetadata', function() {

var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();

// Use the mtime/atime of this file to have proper resolution
var stats = fs.statSync(__filename);
var mtime = stats.mtime;
var atime = stats.atime;
// Use new atime/mtime
var atime = new Date(Date.now() - 2048);
var mtime = new Date(Date.now() - 1024);
var mtimeEarlier = mtime.getTime() - 1000;
var atimeEarlier = atime.getTime() - 1000;

Expand All @@ -1051,12 +1050,15 @@ describe('updateMetadata', function() {

updateMetadata(fd, file, function() {
expect(futimesSpy.calls.length).toEqual(1);
var stats = fs.fstatSync(fd);
// Var stats = fs.fstatSync(fd);

var atimeSpy = futimesSpy.calls[0].arguments[1];
var mtimeSpy = futimesSpy.calls[0].arguments[2];

expect(file.stat.mtime).toEqual(new Date(mtimeEarlier));
expect(stats.mtime.getTime()).toEqual(mtimeEarlier);
expect(mtimeSpy.getTime()).toEqual(mtimeEarlier);
expect(file.stat.atime).toEqual(new Date(atimeEarlier));
expect(stats.atime.getTime()).toEqual(atimeEarlier);
expect(atimeSpy.getTime()).toEqual(atimeEarlier);

fs.close(fd, done);
});
Expand Down Expand Up @@ -1193,10 +1195,9 @@ describe('updateMetadata', function() {
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();

// Use the mtime/atime of this file to have proper resolution
var stats = fs.statSync(__filename);
var mtime = stats.mtime;
var atime = stats.atime;
// Use new atime/mtime
var atime = new Date(Date.now() - 2048);
var mtime = new Date(Date.now() - 1024);
var mtimeEarlier = mtime.getTime() - 1000;
var atimeEarlier = atime.getTime() - 1000;

Expand All @@ -1219,12 +1220,13 @@ describe('updateMetadata', function() {
expect(fchmodSpy.calls.length).toEqual(1);
expect(futimesSpy.calls.length).toEqual(1);

var stats = fs.fstatSync(fd);
var atimeSpy = futimesSpy.calls[0].arguments[1];
var mtimeSpy = futimesSpy.calls[0].arguments[2];

expect(file.stat.mtime).toEqual(new Date(mtimeEarlier));
expect(stats.mtime.getTime()).toEqual(mtimeEarlier);
expect(mtimeSpy.getTime()).toEqual(mtimeEarlier);
expect(file.stat.atime).toEqual(new Date(atimeEarlier));
expect(stats.atime.getTime()).toEqual(atimeEarlier);
expect(atimeSpy.getTime()).toEqual(atimeEarlier);

fs.close(fd, done);
});
Expand Down

0 comments on commit 97649a6

Please sign in to comment.