test: refactor test-fs-stat.js
General code cleanup. PR-URL: https://github.com/nodejs/node/pull/28929 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
1592d0ab73
commit
e6c205265a
@ -28,17 +28,14 @@ const fs = require('fs');
|
|||||||
fs.stat('.', common.mustCall(function(err, stats) {
|
fs.stat('.', common.mustCall(function(err, stats) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.ok(stats.mtime instanceof Date);
|
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
|
// Confirm that we are not running in the context of the internal binding
|
||||||
// layer.
|
// layer.
|
||||||
// Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
|
// Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1
|
||||||
assert.strictEqual(this, undefined);
|
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) {
|
fs.lstat('.', common.mustCall(function(err, stats) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.ok(stats.mtime instanceof Date);
|
assert.ok(stats.mtime instanceof Date);
|
||||||
@ -71,16 +68,9 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
|
|||||||
|
|
||||||
// fstatSync
|
// fstatSync
|
||||||
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
|
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
|
||||||
let stats;
|
const stats = fs.fstatSync(fd);
|
||||||
try {
|
assert.ok(stats.mtime instanceof Date);
|
||||||
stats = fs.fstatSync(fd);
|
fs.close(fd, common.mustCall(assert.ifError));
|
||||||
} catch (err) {
|
|
||||||
assert.fail(err);
|
|
||||||
}
|
|
||||||
if (stats) {
|
|
||||||
assert.ok(stats.mtime instanceof Date);
|
|
||||||
}
|
|
||||||
fs.close(fd, assert.ifError);
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
fs.stat(__filename, common.mustCall(function(err, s) {
|
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.isCharacterDevice(), false);
|
||||||
assert.strictEqual(s.isFIFO(), false);
|
assert.strictEqual(s.isFIFO(), false);
|
||||||
assert.strictEqual(s.isSymbolicLink(), false);
|
assert.strictEqual(s.isSymbolicLink(), false);
|
||||||
const keys = [
|
|
||||||
|
const jsonString = JSON.stringify(s);
|
||||||
|
const parsed = JSON.parse(jsonString);
|
||||||
|
[
|
||||||
'dev', 'mode', 'nlink', 'uid',
|
'dev', 'mode', 'nlink', 'uid',
|
||||||
'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks',
|
'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks',
|
||||||
'atime', 'mtime', 'ctime', 'birthtime',
|
'atime', 'mtime', 'ctime', 'birthtime',
|
||||||
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs'
|
'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs',
|
||||||
];
|
].forEach(function(k) {
|
||||||
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) {
|
|
||||||
assert.ok(k in s, `${k} should be in Stats`);
|
assert.ok(k in s, `${k} should be in Stats`);
|
||||||
assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`);
|
assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`);
|
||||||
assert.notStrictEqual(s[k], null, `${k} should not be null`);
|
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], undefined, `${k} should not be undefined`);
|
||||||
assert.notStrictEqual(parsed[k], null, `${k} should not be null`);
|
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`);
|
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`);
|
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user