Permalink
Browse files

test: refactor test-fs-utimes

* favor `===` over `==`
* `var` -> `const`/`let`
* use `common.mustCall()`

PR-URL: #9290
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
jun-oka authored and Trott committed Oct 26, 2016
1 parent 4e5cf85 commit 377ea285fb92c8f969c19d8d6ecce7f46a86fb54
Showing with 22 additions and 21 deletions.
  1. +22 −21 test/parallel/test-fs-utimes.js
@@ -4,11 +4,11 @@ const assert = require('assert');
const util = require('util');
const fs = require('fs');
var tests_ok = 0;
var tests_run = 0;
let tests_ok = 0;
let tests_run = 0;
function stat_resource(resource) {
if (typeof resource == 'string') {
if (typeof resource === 'string') {
return fs.statSync(resource);
} else {
// ensure mtime has been written to disk
@@ -19,8 +19,8 @@ function stat_resource(resource) {
function check_mtime(resource, mtime) {
mtime = fs._toUnixTimestamp(mtime);
var stats = stat_resource(resource);
var real_mtime = fs._toUnixTimestamp(stats.mtime);
const stats = stat_resource(resource);
const real_mtime = fs._toUnixTimestamp(stats.mtime);
// check up to single-second precision
// sub-second precision is OS and fs dependant
return mtime - real_mtime < 2;
@@ -46,9 +46,9 @@ function expect_ok(syscall, resource, err, atime, mtime) {
// the tests assume that __filename belongs to the user running the tests
// this should be a fairly safe assumption; testing against a temp file
// would be even better though (node doesn't have such functionality yet)
function runTest(atime, mtime, callback) {
function testIt(atime, mtime, callback) {
var fd;
let fd;
//
// test synchronized code paths, these functions throw on failure
//
@@ -67,8 +67,7 @@ function runTest(atime, mtime, callback) {
expect_errno('futimesSync', fd, ex, 'ENOSYS');
}
var err;
err = undefined;
let err = undefined;
try {
fs.utimesSync('foobarbaz', atime, mtime);
} catch (ex) {
@@ -90,10 +89,10 @@ function runTest(atime, mtime, callback) {
//
// test async code paths
//
fs.utimes(__filename, atime, mtime, function(err) {
fs.utimes(__filename, atime, mtime, common.mustCall(function(err) {
expect_ok('utimes', __filename, err, atime, mtime);
fs.utimes('foobarbaz', atime, mtime, function(err) {
fs.utimes('foobarbaz', atime, mtime, common.mustCall(function(err) {
expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
// don't close this fd
@@ -103,34 +102,36 @@ function runTest(atime, mtime, callback) {
fd = fs.openSync(__filename, 'r');
}
fs.futimes(fd, atime, mtime, function(err) {
fs.futimes(fd, atime, mtime, common.mustCall(function(err) {
expect_ok('futimes', fd, err, atime, mtime);
fs.futimes(-1, atime, mtime, function(err) {
fs.futimes(-1, atime, mtime, common.mustCall(function(err) {
expect_errno('futimes', -1, err, 'EBADF');
syncTests();
callback();
});
}));
tests_run++;
});
}));
tests_run++;
});
}));
tests_run++;
});
}));
tests_run++;
}
var stats = fs.statSync(__filename);
const stats = fs.statSync(__filename);
// run tests
const runTest = common.mustCall(testIt, 6);
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
runTest(new Date(), new Date(), function() {
runTest(123456.789, 123456.789, function() {
runTest(stats.mtime, stats.mtime, function() {
runTest(NaN, Infinity, function() {
runTest('123456', -1, function() {
runTest('123456', -1, common.mustCall(function() {
// done
});
}));
});
});
});
@@ -140,5 +141,5 @@ runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
process.on('exit', function() {
console.log('Tests run / ok:', tests_run, '/', tests_ok);
assert.equal(tests_ok, tests_run);
assert.strictEqual(tests_ok, tests_run);
});

0 comments on commit 377ea28

Please sign in to comment.