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) {
|
||||
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`);
|
||||
});
|
||||
}));
|
||||
|
Loading…
x
Reference in New Issue
Block a user