Skip to content

Commit

Permalink
test: refactor test-fs-utimes
Browse files Browse the repository at this point in the history
* 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 MylesBorins committed Mar 9, 2017
1 parent e4f7f5c commit 12746af
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions test/parallel/test-fs-utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ var assert = require('assert');
var util = require('util');
var 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
Expand All @@ -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;
Expand All @@ -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
//
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
});
}));
});
});
});
Expand All @@ -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 12746af

Please sign in to comment.