Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
squash: improve tests
  • Loading branch information
LiviaMedeiros committed Apr 18, 2022
commit 982630982498f4fc14fc5fb07c2c884d491b98e4
10 changes: 2 additions & 8 deletions test/parallel/test-fs-read-offset-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ let filehandle = null;
);
})()
.then(common.mustCall())
.finally(async () => {
if (filehandle)
await filehandle.close();
});
.finally(() => filehandle?.close());

(async () => {
filehandle = await fsPromises.open(filepath, 'r');
Expand All @@ -61,7 +58,4 @@ let filehandle = null;
assert.strictEqual(readObject.buffer[0], 120);
})()
.then(common.mustCall())
.finally(async () => {
if (filehandle)
await filehandle.close();
});
.finally(() => filehandle?.close());
59 changes: 23 additions & 36 deletions test/parallel/test-fs-read-optional-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,36 @@ const fixtures = require('../common/fixtures');
const fs = require('fs');
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);
const bufferAsOption = Buffer.allocUnsafe(expected.byteLength);

// Test not passing in any options object
fs.read(fd, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength);
}));
fs.read(fd, bufferAsOption, { position: 0 }, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
}));
function testValid(message, ...options) {
const paramsMsg = `${message} (as params)`;
const paramsFilehandle = fs.openSync(filepath, 'r');
fs.read(paramsFilehandle, ...options, common.mustCall((err, bytesRead, buffer) => {
if (err) throw err;
assert.strictEqual(bytesRead, expected.byteLength, paramsMsg);
assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength, paramsMsg);
fs.closeSync(paramsFilehandle);
}));

// Test passing in an empty options object
fs.read(fd, { position: 0 }, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength);
}));
fs.read(fd, bufferAsOption, { position: 0 }, common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
}));
const optionsMsg = `${message} (as options)`;
const optionsFilehandle = fs.openSync(filepath, 'r');
fs.read(optionsFilehandle, bufferAsOption, ...options, common.mustCall((err, bytesRead, buffer) => {
if (err) throw err;
assert.strictEqual(bytesRead, expected.byteLength, optionsMsg);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength, optionsMsg);
fs.closeSync(optionsFilehandle);
}));
}

// Test passing in options
fs.read(fd, {
buffer: bufferAsOption,
testValid('Not passing in any object');
testValid('Passing in a null', null);
testValid('Passing in an empty object', {});
testValid('Passing in an object', {
offset: 0,
length: bufferAsOption.byteLength,
position: 0
},
common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
}));
fs.read(fd, bufferAsOption, {
offset: 0,
length: bufferAsOption.byteLength,
position: 0
},
common.mustCall((err, bytesRead, buffer) => {
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
}));
});