Skip to content
Permalink
Browse files

test: refactor test-fs-stat.js

General code cleanup.

PR-URL: #28929
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information...
Trott committed Aug 1, 2019
1 parent 1592d0a commit e6c205265a8812b426d0a3c8f3d28c7bf0db224d
Showing with 18 additions and 36 deletions.
  1. +18 −36 test/parallel/test-fs-stat.js
@@ -28,17 +28,14 @@ const fs = require('fs');
fs.stat('.', common.mustCall(function(err, stats) {
assert.ifError(err);
assert.ok(stats.mtime instanceof Date);
assert.ok(stats.hasOwnProperty('blksize'));
assert.ok(stats.hasOwnProperty('blocks'));
// Confirm that we are not running in the context of the internal binding
// layer.
// Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
assert.strictEqual(this, undefined);
}));

fs.stat('.', common.mustCall(function(err, stats) {
assert.ok(stats.hasOwnProperty('blksize'));
assert.ok(stats.hasOwnProperty('blocks'));
}));

fs.lstat('.', common.mustCall(function(err, stats) {
assert.ifError(err);
assert.ok(stats.mtime instanceof Date);
@@ -71,16 +68,9 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {

// fstatSync
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
let stats;
try {
stats = fs.fstatSync(fd);
} catch (err) {
assert.fail(err);
}
if (stats) {
assert.ok(stats.mtime instanceof Date);
}
fs.close(fd, assert.ifError);
const stats = fs.fstatSync(fd);
assert.ok(stats.mtime instanceof Date);
fs.close(fd, common.mustCall(assert.ifError));
}));

fs.stat(__filename, common.mustCall(function(err, s) {
@@ -92,38 +82,30 @@ fs.stat(__filename, common.mustCall(function(err, s) {
assert.strictEqual(s.isCharacterDevice(), false);
assert.strictEqual(s.isFIFO(), false);
assert.strictEqual(s.isSymbolicLink(), false);
const keys = [

const jsonString = JSON.stringify(s);
const parsed = JSON.parse(jsonString);
[
'dev', 'mode', 'nlink', 'uid',
'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks',
'atime', 'mtime', 'ctime', 'birthtime',
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs'
];
const numberFields = [
'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size',
'blocks', 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs'
];
const dateFields = ['atime', 'mtime', 'ctime', 'birthtime'];
keys.forEach(function(k) {
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs',
].forEach(function(k) {
assert.ok(k in s, `${k} should be in Stats`);
assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`);
assert.notStrictEqual(s[k], null, `${k} should not be null`);
});
numberFields.forEach((k) => {
assert.strictEqual(typeof s[k], 'number', `${k} should be a number`);
});
dateFields.forEach((k) => {
assert.ok(s[k] instanceof Date, `${k} should be a Date`);
});
const jsonString = JSON.stringify(s);
const parsed = JSON.parse(jsonString);
keys.forEach(function(k) {
assert.notStrictEqual(parsed[k], undefined, `${k} should not be undefined`);
assert.notStrictEqual(parsed[k], null, `${k} should not be null`);
});
numberFields.forEach((k) => {
[
'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size',
'blocks', 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs',
].forEach((k) => {
assert.strictEqual(typeof s[k], 'number', `${k} should be a number`);
assert.strictEqual(typeof parsed[k], 'number', `${k} should be a number`);
});
dateFields.forEach((k) => {
['atime', 'mtime', 'ctime', 'birthtime'].forEach((k) => {
assert.ok(s[k] instanceof Date, `${k} should be a Date`);
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
});
}));

0 comments on commit e6c2052

Please sign in to comment.
You can’t perform that action at this time.