0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-fs-promises-file-handle-write.js
willhayslett 126b03e2f9 test: add tests for fs/promises.js fileHandle methods
Working to increase test code coverage for fs/promises.js.
Added tests for fileHandle.appendFile and fileHandle.chmod.

PR-URL: https://github.com/nodejs/node/pull/19605
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2018-04-04 19:05:29 -07:00

41 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
// The following tests validate base functionality for the fs/promises
// FileHandle.read method.
const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;
tmpdir.refresh();
common.crashOnUnhandledRejection();
async function validateWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}
async function validateEmptyWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from(''); // empty buffer
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}
validateWrite()
.then(validateEmptyWrite)
.then(common.mustCall());