Skip to content

Commit

Permalink
Add tests to fileDatesEqual function in fs module
Browse files Browse the repository at this point in the history
  • Loading branch information
vbfox committed Apr 5, 2017
1 parent a715098 commit 47cbff3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
51 changes: 51 additions & 0 deletions __tests__/util/fs.js
@@ -0,0 +1,51 @@
/* @flow */

import {fileDatesEqual} from '../../src/util/fs.js';

describe('fileDatesEqual', () => {
const realPlatform = process.platform;

describe('!win32', () => {
beforeAll(() => {
process.platform = 'notWin32';
});

afterAll(() => {
process.platform = realPlatform;
});

test('Same dates equal', () => {
expect(fileDatesEqual(new Date(1491393798834), new Date(1491393798834))).toBeTruthy();
expect(fileDatesEqual(new Date(1491393798000), new Date(1491393798000))).toBeTruthy();
});

test('Different dates differ', () => {
expect(fileDatesEqual(new Date(1491393798834), new Date(1491393798835))).toBeFalsy();
expect(fileDatesEqual(new Date(1491393700834), new Date(1491393798834))).toBeFalsy();
expect(fileDatesEqual(new Date(1491393798000), new Date(1491393798835))).toBeFalsy();
});
});
describe('win32', () => {
beforeAll(() => {
process.platform = 'win32';
});

afterAll(() => {
process.platform = realPlatform;
});

test('Same dates equal', () => {
expect(fileDatesEqual(new Date(1491393798834), new Date(1491393798834))).toBeTruthy();
});

test('Different dates differ', () => {
expect(fileDatesEqual(new Date(1491393798834), new Date(1491393798835))).toBeFalsy();
expect(fileDatesEqual(new Date(1491393700834), new Date(1491393798834))).toBeFalsy();
});

test('Milliseconds are ignored when one date has zero milliseconds', () => {
expect(fileDatesEqual(new Date(1491393798000), new Date(1491393798835))).toBeTruthy();
expect(fileDatesEqual(new Date(1491393798834), new Date(1491393798000))).toBeTruthy();
});
});
});
39 changes: 19 additions & 20 deletions src/util/fs.js
Expand Up @@ -80,28 +80,27 @@ type CopyOptions = {
artifactFiles: Array<string>,
};

let fileDatesEqual = (a: Date, b: Date) => {
return a.getTime() === b.getTime();
};

if (os.platform() === 'win32') {
fileDatesEqual = (a: Date, b: Date) => {
const aTime = a.getTime();
const bTime = b.getTime();
const aTimeSec = Math.floor(aTime / 1000);
const bTimeSec = Math.floor(bTime / 1000);

// See https://github.com/nodejs/node/issues/2069
// Some versions of Node on windows zero the milliseconds when utime is used
// So if any of the time has a milliseconds part of zero we suspect that the
// bug is present and compare only seconds.
if ((aTime - aTimeSec * 1000 === 0) || (bTime - bTimeSec * 1000 === 0)) {
return aTimeSec === bTimeSec;
}
export const fileDatesEqual = (a: Date, b: Date) => {
const aTime = a.getTime();
const bTime = b.getTime();

if (process.platform !== 'win32') {
return aTime === bTime;
};
}
}

const aTimeSec = Math.floor(aTime / 1000);
const bTimeSec = Math.floor(bTime / 1000);

// See https://github.com/nodejs/node/issues/2069
// Some versions of Node on windows zero the milliseconds when utime is used
// So if any of the time has a milliseconds part of zero we suspect that the
// bug is present and compare only seconds.
if ((aTime - aTimeSec * 1000 === 0) || (bTime - bTimeSec * 1000 === 0)) {
return aTimeSec === bTimeSec;
}

return aTime === bTime;
};

async function buildActionsForCopy(
queue: CopyQueue,
Expand Down

0 comments on commit 47cbff3

Please sign in to comment.