Skip to content
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

test: increase fs promise coverage #36813

Closed
Show file tree
Hide file tree
Changes from 3 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
69 changes: 67 additions & 2 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const common = require('../common');
// FileHandle.readFile method.

const fs = require('fs');
const { open } = fs.promises;
const { open, readFile, writeFile } = fs.promises;
emilsivervik marked this conversation as resolved.
Show resolved Hide resolved
const path = require('path');
const tmpdir = require('../common/tmpdir');
const tick = require('../common/tick');
const assert = require('assert');
const tmpDir = tmpdir.path;

Expand Down Expand Up @@ -45,6 +46,70 @@ async function validateReadFileProc() {
assert.ok(hostname.length > 0);
}

async function doReadAndCancel() {
// Signal aborted from the start
{
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const controller = new AbortController();
const { signal } = controller;
controller.abort();
await assert.rejects(readFile(fileHandle, { signal }), {
name: 'AbortError'
});
}

// Signal aborted on first tick
{
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const controller = new AbortController();
const { signal } = controller;
tick(1, () => controller.abort());
await assert.rejects(readFile(fileHandle, { signal }), {
name: 'AbortError'
});
}

// Signal aborted right before buffer read
{
const newFile = path.resolve(tmpDir, 'dogs-running2.txt');
const buffer = Buffer.from('Dogs running'.repeat(1000), 'utf8');
fs.writeFileSync(newFile, buffer);

const fileHandle = await open(newFile, 'r');

const controller = new AbortController();
const { signal } = controller;
tick(2, () => controller.abort());
await assert.rejects(fileHandle.readFile({ signal, encoding: 'utf8' }), {
name: 'AbortError'
});
}

// Validate file size is within range for reading
{
// Variable taken from https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L5
const kIoMaxLength = 2 ** 31 - 1;

const newFile = path.resolve(tmpDir, 'dogs-running3.txt');
const buffer = Buffer.alloc(kIoMaxLength + 1);
await writeFile(newFile, buffer);
emilsivervik marked this conversation as resolved.
Show resolved Hide resolved

const fileHandle = await open(newFile, 'r');

await assert.rejects(fileHandle.readFile(), {
name: 'RangeError',
code: 'ERR_FS_FILE_TOO_LARGE'
});
}
}

validateReadFile()
.then(() => validateReadFileProc())
.then(validateReadFileProc)
.then(doReadAndCancel)
.then(common.mustCall());
18 changes: 16 additions & 2 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const common = require('../common');

// The following tests validate base functionality for the fs.promises
// FileHandle.readFile method.
// FileHandle.writeFile method.

const fs = require('fs');
const { open } = fs.promises;
const { open, writeFile } = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
Expand All @@ -26,5 +26,19 @@ async function validateWriteFile() {
await fileHandle.close();
}

// Signal aborted while writing file
async function doWriteAndCancel() {
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8');
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
name: 'AbortError'
});
}

validateWriteFile()
.then(doWriteAndCancel)
.then(common.mustCall());
2 changes: 1 addition & 1 deletion test/parallel/test-fs-promises-writefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function doWriteWithCancel() {
}

async function doAppend() {
await fsPromises.appendFile(dest, buffer2);
await fsPromises.appendFile(dest, buffer2, { flag: null });
const data = fs.readFileSync(dest);
const buf = Buffer.concat([buffer, buffer2]);
assert.deepStrictEqual(buf, data);
Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
link,
lchmod,
lstat,
lutimes,
mkdir,
mkdtemp,
open,
Expand Down Expand Up @@ -140,6 +141,13 @@ async function getHandle(dest) {
await handle.close();
}

// Use fallback buffer allocation when input not buffer
{
const handle = await getHandle(dest);
const ret = await handle.read(0, 0, 0, 0);
assert.strictEqual(ret.buffer.length, 16384);
}

// Bytes written to file match buffer
{
const handle = await getHandle(dest);
Expand Down Expand Up @@ -226,6 +234,19 @@ async function getHandle(dest) {
await handle.close();
}

// Set modification times with lutimes
{
const a_time = new Date();
a_time.setMinutes(a_time.getMinutes() - 1);
const m_time = new Date();
m_time.setHours(m_time.getHours() - 1);
await lutimes(dest, a_time, m_time);
const stats = await stat(dest);

assert.strictEqual(a_time.toString(), stats.atime.toString());
assert.strictEqual(m_time.toString(), stats.mtime.toString());
}

// create symlink
{
const newPath = path.resolve(tmpDir, 'baz2.js');
Expand Down Expand Up @@ -270,6 +291,15 @@ async function getHandle(dest) {
}
}

// specify symlink type
{
const dir = path.join(tmpDir, nextdir());
await symlink(tmpDir, dir, 'dir');
const stats = await lstat(dir);
assert.strictEqual(stats.isSymbolicLink(), true);
await unlink(dir);
}

// create hard link
{
const newPath = path.resolve(tmpDir, 'baz2.js');
Expand All @@ -296,6 +326,14 @@ async function getHandle(dest) {
await unlink(newFile);
}

// Use fallback encoding when input is null
{
const newFile = path.resolve(tmpDir, 'dogs_running.js');
await writeFile(newFile, 'dogs running', { encoding: null });
const fileExists = fs.existsSync(newFile);
assert.strictEqual(fileExists, true);
}

// `mkdir` when options is number.
{
const dir = path.join(tmpDir, nextdir());
Expand Down