Skip to content
Closed
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
test: additional tests for fs/promises fileHandle
Updated and added tests for fs/promises fileHandle. Changes
include updates to chmod and appendFile. New tests were created
for the read, readFile, write, and writeFile methods.
  • Loading branch information
willhayslett committed Apr 4, 2018
commit 5183962d73474ab9994fee01e86b2dcbbb5d5240
50 changes: 23 additions & 27 deletions test/parallel/test-fs-promises-file-handle-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,35 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateAppendBuffer({ filePath,
fileHandle,
bufferToAppend }) {
await fileHandle.appendFile(bufferToAppend);
const appendedFileData = fs.readFileSync(filePath);
assert.deepStrictEqual(appendedFileData, bufferToAppend);
}
async function validateAppendBuffer() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
const fileHandle = await open(filePath, 'a');
const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');

async function validateAppendString({ filePath,
fileHandle,
stringToAppend,
bufferToAppend }) {
await fileHandle.appendFile(stringToAppend);
const stringAsBuffer = Buffer.from(stringToAppend, 'utf8');
await fileHandle.appendFile(buffer);
const appendedFileData = fs.readFileSync(filePath);
const combinedBuffer = Buffer.concat([bufferToAppend, stringAsBuffer]);
assert.deepStrictEqual(appendedFileData, combinedBuffer);
assert.deepStrictEqual(appendedFileData, buffer);
}

async function executeTests() {
tmpdir.refresh();
async function validateAppendString() {
//don't refresh the directory
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-append-file.txt');
const appendFileDetails = {
filePath,
fileHandle: await open(filePath, 'a'),
bufferToAppend: Buffer.from('a&Dp'.repeat(100), 'utf8'),
stringToAppend: 'x~yz'.repeat(100)
};
const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
const fileHandle = await open(filePath, 'a');
const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');
const string = 'x~yz'.repeat(100);

await validateAppendBuffer(appendFileDetails);
await validateAppendString(appendFileDetails);
await fileHandle.appendFile(string);
const stringAsBuffer = Buffer.from(string, 'utf8');
const appendedFileData = fs.readFileSync(filePath);
const combinedBuffer = Buffer.concat([buffer, stringAsBuffer]);
assert.deepStrictEqual(appendedFileData, combinedBuffer);
}

executeTests().then(common.mustCall());
validateAppendBuffer()
.then(validateAppendString)
.then(common.mustCall());
33 changes: 10 additions & 23 deletions test/parallel/test-fs-promises-file-handle-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,19 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateFilePermission({ filePath, fileHandle }) {
//file created with r/w, should not have execute
fs.access(filePath, fs.constants.X_OK, common.mustCall(async (err) => {
//err should an instance of Error
assert.deepStrictEqual(err instanceof Error, true);
//add execute permissions
await fileHandle.chmod(0o765);

fs.access(filePath, fs.constants.X_OK, common.mustCall((err) => {
//err should be undefined or null
assert.deepStrictEqual(!err, true);
}));
}));
}

async function executeTests() {
async function validateFilePermission() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
const chmodFileDetails = {
filePath,
fileHandle: await open(filePath, 'w+', 0o666)
};

await validateFilePermission(chmodFileDetails);
const fileHandle = await open(filePath, 'w+', 0o644);
//file created with r/w 644
const statsBeforeMod = fs.statSync(filePath);
assert.deepStrictEqual(statsBeforeMod.mode & 0o644, 0o644);
//change the permissions to 765
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And here.

await fileHandle.chmod(0o765);
const statsAfterMod = fs.statSync(filePath);
assert.deepStrictEqual(statsAfterMod.mode & 0o765, 0o765);
}

executeTests().then(common.mustCall());
validateFilePermission().then(common.mustCall());
57 changes: 57 additions & 0 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'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;

async function validateRead() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('Hello world', 'utf8');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
//use async read to get the buffer that was read into
const cb = common.mustCall(async (err, bytesRead, readBuffer) => {
assert.ifError(err);
fs.closeSync(fd);

const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(bytesRead,
readAsyncHandle.bytesRead);
assert.deepStrictEqual(readBuffer.toString('utf8'),
readAsyncHandle.buffer.toString('utf8'));
});
fs.read(fd, Buffer.alloc(11), 0, 11, 0, cb);
}

async function validateEmptyRead() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('', 'utf8');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
const bytesRead = fs.readFileSync(filePath, 'utf8');
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(bytesRead.length, readAsyncHandle.bytesRead);
}

validateRead()
.then(validateEmptyRead)
.then(common.mustCall());
33 changes: 33 additions & 0 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

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

// The following tests validate base functionality for the fs/promises
// FileHandle.readFile 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;

async function validateReadFile() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
const readSyncData = fs.readFileSync(filePath);
fs.closeSync(fd);

const readAsyncData = await fileHandle.readFile();
assert.deepStrictEqual(readSyncData, readAsyncData);
}

validateReadFile()
.then(common.mustCall());
56 changes: 56 additions & 0 deletions test/parallel/test-fs-promises-file-handle-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'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;

async function validateWrite() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePathForSync = path.resolve(tmpDir, 'tmp-write-file1.txt');
const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');

const fd = fs.openSync(filePathForSync, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);

const bytesRead = fs.readFileSync(filePathForSync);
await fileHandle.write(buffer, 0, buffer.length);
const bytesReadHandle = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(bytesRead, bytesReadHandle);
}

async function validateEmptyWrite() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePathForSync = path.resolve(tmpDir, 'tmp-write-file1.txt');
const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt');
const fileHandle = await open(filePathForHandle, 'w+');
//empty buffer
const buffer = Buffer.from('');

const fd = fs.openSync(filePathForSync, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);

const bytesRead = fs.readFileSync(filePathForSync);
await fileHandle.write(buffer, 0, buffer.length);
const bytesReadHandle = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(bytesRead, bytesReadHandle);
}

validateWrite()
.then(validateEmptyWrite)
.then(common.mustCall());
33 changes: 33 additions & 0 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

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

// The following tests validate base functionality for the fs/promises
// FileHandle.readFile 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;

async function validateWriteFile() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePathForSync = path.resolve(tmpDir, 'tmp-write-file1.txt');
const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');

fs.writeFileSync(filePathForSync, buffer);
const bytesRead = fs.readFileSync(filePathForSync);

await fileHandle.writeFile(buffer);
const bytesReadHandle = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(bytesRead, bytesReadHandle);
}

validateWriteFile()
.then(common.mustCall());