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: updated tests for fs/promises fileHandle
Updated fs/promises fileHandle tests. Removed indirect assertions
that compared data between the old fs methods and those being tested
in favor of more direct comparisons between the tested methods'
output and the file input data.
  • Loading branch information
willhayslett committed Apr 4, 2018
commit 74b6ba1b2f63e09d30513e33d5afd5bad982d732
19 changes: 5 additions & 14 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,10 @@ async function validateRead() {

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);
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
}

async function validateEmptyRead() {
Expand All @@ -47,9 +39,8 @@ async function validateEmptyRead() {
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);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
}

validateRead()
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ async function validateReadFile() {

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);
const readFileData = await fileHandle.readFile();
assert.deepStrictEqual(buffer, readFileData);
}

validateReadFile()
Expand Down
27 changes: 7 additions & 20 deletions test/parallel/test-fs-promises-file-handle-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,26 @@ 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 filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.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);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}

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 filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.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 buffer = Buffer.from(''); //empty buffer

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

validateWrite()
Expand Down
8 changes: 2 additions & 6 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ 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);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}

validateWriteFile()
Expand Down