deno.land / std@0.166.0 / node / _tools / test / parallel / test-fs-read-type.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';const common = require('../common');const fs = require('fs');const assert = require('assert');const fixtures = require('../common/fixtures');
const filepath = fixtures.path('x.txt');const fd = fs.openSync(filepath, 'r');const expected = 'xyz\n';
// Error must be thrown with stringassert.throws( () => fs.read(fd, expected.length, 0, 'utf-8', common.mustNotCall()), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "buffer" argument must be an instance of Buffer, ' + 'TypedArray, or DataView. Received type number (4)' });
[true, null, undefined, () => {}, {}].forEach((value) => { assert.throws(() => { fs.read(value, Buffer.allocUnsafe(expected.length), 0, expected.length, 0, common.mustNotCall()); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });});
assert.throws(() => { fs.read(fd, Buffer.allocUnsafe(expected.length), -1, expected.length, 0, common.mustNotCall());}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError',});
assert.throws(() => { fs.read(fd, Buffer.allocUnsafe(expected.length), NaN, expected.length, 0, common.mustNotCall());}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "offset" is out of range. It must be an integer. ' + 'Received NaN'});
assert.throws(() => { fs.read(fd, Buffer.allocUnsafe(expected.length), 0, -1, 0, common.mustNotCall());}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "length" is out of range. ' + 'It must be >= 0. Received -1'});
[true, () => {}, {}, ''].forEach((value) => { assert.throws(() => { fs.read(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, value, common.mustNotCall()); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });});
[0.5, 2 ** 53, 2n ** 63n].forEach((value) => { assert.throws(() => { fs.read(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, value, common.mustNotCall()); }, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError' });});
fs.read(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, 0n, common.mustSucceed());
fs.read(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, 2n ** 53n - 1n, common.mustCall((err) => { if (err) { if (common.isIBMi) assert.strictEqual(err.code, 'EOVERFLOW'); else assert.strictEqual(err.code, 'EFBIG'); } }));
assert.throws( () => fs.readSync(fd, expected.length, 0, 'utf-8'), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "buffer" argument must be an instance of Buffer, ' + 'TypedArray, or DataView. Received type number (4)' });
[true, null, undefined, () => {}, {}].forEach((value) => { assert.throws(() => { fs.readSync(value, Buffer.allocUnsafe(expected.length), 0, expected.length, 0); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });});
assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), -1, expected.length, 0);}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError',});
assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), NaN, expected.length, 0);}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "offset" is out of range. It must be an integer. ' + 'Received NaN'});
assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, -1, 0);}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "length" is out of range. ' + 'It must be >= 0. Received -1'});
assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, expected.length + 1, 0);}, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "length" is out of range. ' + 'It must be <= 4. Received 5'});
[true, () => {}, {}, ''].forEach((value) => { assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, value); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });});
[0.5, 2 ** 53, 2n ** 63n].forEach((value) => { assert.throws(() => { fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, value); }, { code: 'ERR_OUT_OF_RANGE', name: 'RangeError' });});
fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, 0n);
try { fs.readSync(fd, Buffer.allocUnsafe(expected.length), 0, expected.length, 2n ** 53n - 1n);} catch (err) { // On systems where max file size is below 2^53-1, we'd expect a EFBIG error. // This is not using `assert.throws` because the above call should not raise // any error on systems that allows file of that size. if (err.code !== 'EFBIG' && !(common.isIBMi && err.code === 'EOVERFLOW')) throw err;}
Version Info