node/test/parallel/test-fs-readv-sync.js
Tobias Nießen dd52c05046 test: avoid deep comparisons with literals
Comparing any value to any non-RegExp literal or undefined using
strictEqual (or notStrictEqual) passes if and only if deepStrictEqual
(or notDeepStrictEqual, respectively) passes.

Unnecessarily using deep comparisons adds confusion.

This patch adds an ESLint rule that forbids the use of deepStrictEqual
and notDeepStrictEqual when the expected value (i.e., the second
argument) is a non-RegExp literal or undefined.

For reference, an ESTree literal is defined as follows.

    extend interface Literal <: Expression {
        type: "Literal";
        value: string | boolean | null | number | RegExp | bigint;
    }

The value `undefined` is an `Identifier` with `name: 'undefined'`.

PR-URL: https://github.com/nodejs/node/pull/40634
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com>
2021-11-02 10:11:49 +00:00

94 lines
2.2 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
const exptectedBuff = Buffer.from(expected);
const expectedLength = exptectedBuff.length;
const filename = path.join(tmpdir.path, 'readv_sync.txt');
fs.writeFileSync(filename, exptectedBuff);
const allocateEmptyBuffers = (combinedLength) => {
const bufferArr = [];
// Allocate two buffers, each half the size of exptectedBuff
bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2));
bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length);
return bufferArr;
};
// fs.readvSync with array of buffers with all parameters
{
const fd = fs.openSync(filename, 'r');
const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
let read = fs.readvSync(fd, [Buffer.from('')], 0);
assert.strictEqual(read, 0);
read = fs.readvSync(fd, bufferArr, 0);
assert.strictEqual(read, expectedLength);
fs.closeSync(fd);
assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
}
// fs.readvSync with array of buffers without position
{
const fd = fs.openSync(filename, 'r');
const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
let read = fs.readvSync(fd, [Buffer.from('')]);
assert.strictEqual(read, 0);
read = fs.readvSync(fd, bufferArr);
assert.strictEqual(read, expectedLength);
fs.closeSync(fd);
assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
}
/**
* Testing with incorrect arguments
*/
const wrongInputs = [false, 'test', {}, [{}], ['sdf'], null, undefined];
{
const fd = fs.openSync(filename, 'r');
wrongInputs.forEach((wrongInput) => {
assert.throws(
() => fs.readvSync(fd, wrongInput, null), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
}
);
});
fs.closeSync(fd);
}
{
// fs.readv with wrong fd argument
wrongInputs.forEach((wrongInput) => {
assert.throws(
() => fs.readvSync(wrongInput),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
}
);
});
}