Skip to content

test: migrate fs read promises to test runner #55749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
62 changes: 46 additions & 16 deletions test/parallel/test-fs-read-promises-optional-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,56 @@

const common = require('../common');
const fixtures = require('../common/fixtures');
const fs = require('fs');
const read = require('util').promisify(fs.read);
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');
const fs = require('node:fs');
const fsPromises = require('node:fs/promises');
const read = require('node:util').promisify(fs.read);
const assert = require('node:assert');
const { describe, test } = require('node:test');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { describe, test } = require('node:test');
const { describe, it } = require('node:test');

"it" is the correct function for "describe", but I don't remember if it really matters under the hood

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one is an alias of the other, but changed it anyway


const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);
const bufferAsOption = Buffer.allocUnsafe(expected.byteLength);
describe('fs promises read with optional parameters', async () => {
const filepath = fixtures.path('x.txt');

read(fd, common.mustNotMutateObjectDeep({}))
.then(function({ bytesRead, buffer }) {
const expected = Buffer.from('xyz\n');
const defaultBufferAsync = Buffer.alloc(16384);
const bufferAsOption = Buffer.allocUnsafe(expected.byteLength);

test('should read the file with default buffer (promisified)', async () => {
const fd = fs.openSync(filepath, 'r');
const { bytesRead, buffer } = await read(
fd,
common.mustNotMutateObjectDeep({})
);
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength);
});

test('should read the file with buffer as option (promisified)', async () => {
const fd = fs.openSync(filepath, 'r');
const { bytesRead, buffer } = await read(
fd,
bufferAsOption,
common.mustNotMutateObjectDeep({ position: 0 })
);
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
});

test('should read the file with default buffer (fd)', async () => {
const fd = await fsPromises.open(filepath, 'r');
const { bytesRead, buffer } = await fd.read(
common.mustNotMutateObjectDeep({})
);
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength);
})
.then(common.mustCall());
});

read(fd, bufferAsOption, common.mustNotMutateObjectDeep({ position: 0 }))
.then(function({ bytesRead, buffer }) {
test('should read the file with buffer as option (fd)', async () => {
const fd = await fsPromises.open(filepath, 'r');
const { bytesRead, buffer } = await fd.read(
bufferAsOption,
common.mustNotMutateObjectDeep({ position: 0 })
);
assert.strictEqual(bytesRead, expected.byteLength);
assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength);
})
.then(common.mustCall());
});
});
Loading