Skip to content

Commit

Permalink
squash: add test for fs.write()
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Apr 21, 2022
1 parent 61d4139 commit d114840
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions test/parallel/test-fs-write-optional-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

const common = require('../common');

// This test ensures that fs.write accepts "named parameters" object
// and doesn't interpret objects as strings

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

const destInvalid = path.resolve(tmpdir.path, 'rwopt_invalid');
const buffer = Buffer.from('zyx');

async function testInvalid(fd, expectedCode, ...params) {
assert.throws(
() => fs.write(fd, ...params, common.mustNotCall()),
{ code: expectedCode }
);
}

function testValid([ buffer, options ], index) {
const dest = path.resolve(tmpdir.path, `rwopt_valid_${index}`);
fs.open(dest, 'w+', common.mustSucceed((fd) => {
fs.write(fd, buffer, options, common.mustSucceed((bytesWritten, bufferWritten) => {
const writeBufCopy = Uint8Array.prototype.slice.call(bufferWritten);

// TODO: replace this with fs.read(fd, buffer, options, cb) if it is supported
fs.read(fd, { buffer, ...options }, common.mustSucceed((bytesRead, bufferRead) => {
const readBufCopy = Uint8Array.prototype.slice.call(bufferRead);

assert.ok(bytesWritten >= bytesRead);
if (options.length !== undefined && options.length !== null) {
assert.strictEqual(bytesWritten, options.length);
}
if (options.offset === undefined || options.offset === 0) {
assert.deepStrictEqual(writeBufCopy, readBufCopy);
}
assert.deepStrictEqual(bufferWritten, bufferRead);
fs.close(fd, common.mustSucceed());
}));
}));
}));
}

fs.open(destInvalid, 'w+', common.mustSucceed((fd) => {
// Test if first argument is not wrongly interpreted as ArrayBufferView|string
for (const badBuffer of [
undefined, null, true, 42, 42n, Symbol('42'), NaN, [], () => {},
Promise.resolve(new Uint8Array(1)),
{},
{ buffer: 'amNotParam' },
{ string: 'amNotParam' },
{ buffer: new Uint8Array(1).buffer },
new Date(),
new String('notPrimitive'),
{ [Symbol.toPrimitive]: (hint) => 'amObject' },

// TODO: add the following after DEP0162 EOL
// { toString() { return 'amObject'; } },
]) {
testInvalid(fd, 'ERR_INVALID_ARG_TYPE', badBuffer, {});
}

// First argument (buffer or string) is mandatory
testInvalid(fd, 'ERR_INVALID_ARG_TYPE');

// Various invalid options
testInvalid(fd, 'ERR_OUT_OF_RANGE', buffer, { length: 5 });
testInvalid(fd, 'ERR_OUT_OF_RANGE', buffer, { offset: 5 });
testInvalid(fd, 'ERR_OUT_OF_RANGE', buffer, { length: 1, offset: 3 });
testInvalid(fd, 'ERR_OUT_OF_RANGE', buffer, { length: -1 });
testInvalid(fd, 'ERR_OUT_OF_RANGE', buffer, { offset: -1 });
testInvalid(fd, 'ERR_INVALID_ARG_TYPE', buffer, { offset: false });
testInvalid(fd, 'ERR_INVALID_ARG_TYPE', buffer, { offset: true });

// Test compatibility with fs.read counterpart
[
[ buffer, {} ],
[ buffer, { length: 1 } ],
[ buffer, { position: 5 } ],
[ buffer, { length: 1, position: 5 } ],
[ buffer, { length: 1, position: -1, offset: 2 } ],
[ buffer, { length: null } ],
[ buffer, { position: null } ],
[ buffer, { offset: 1 } ],
].forEach(testValid);
}));

0 comments on commit d114840

Please sign in to comment.