From f98fc81653619cc1b5006014ab916aebd1811003 Mon Sep 17 00:00:00 2001 From: willhayslett Date: Sun, 25 Mar 2018 21:56:06 -0500 Subject: [PATCH 1/6] test: adding 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. --- ...est-fs-promises-file-handle-append-file.js | 50 +++++++++++++++++++ .../test-fs-promises-file-handle-chmod.js | 43 ++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 test/parallel/test-fs-promises-file-handle-append-file.js create mode 100644 test/parallel/test-fs-promises-file-handle-chmod.js diff --git a/test/parallel/test-fs-promises-file-handle-append-file.js b/test/parallel/test-fs-promises-file-handle-append-file.js new file mode 100644 index 00000000000000..99476a19298dab --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-append-file.js @@ -0,0 +1,50 @@ +'use strict'; + +const common = require('../common'); + +// The following tests validate base functionality for the fs/promises +// FileHandle.appendFile 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 validateAppendBuffer({ filePath, + fileHandle, + bufferToAppend }) { + await fileHandle.appendFile(bufferToAppend); + const appendedFileData = fs.readFileSync(filePath); + assert.deepStrictEqual(appendedFileData, bufferToAppend); +} + +async function validateAppendString({ filePath, + fileHandle, + stringToAppend, + bufferToAppend }) { + await fileHandle.appendFile(stringToAppend); + const stringAsBuffer = Buffer.from(stringToAppend, 'utf8'); + const appendedFileData = fs.readFileSync(filePath); + const combinedBuffer = Buffer.concat([bufferToAppend, stringAsBuffer]); + assert.deepStrictEqual(appendedFileData, combinedBuffer); +} + +async function executeTests() { + tmpdir.refresh(); + 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) + }; + + await validateAppendBuffer(appendFileDetails); + await validateAppendString(appendFileDetails); +} + +executeTests().then(common.mustCall()); diff --git a/test/parallel/test-fs-promises-file-handle-chmod.js b/test/parallel/test-fs-promises-file-handle-chmod.js new file mode 100644 index 00000000000000..c49b9021cc061c --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -0,0 +1,43 @@ +'use strict'; + +const common = require('../common'); + +// The following tests validate base functionality for the fs/promises +// FileHandle.chmod 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 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() { + tmpdir.refresh(); + common.crashOnUnhandledRejection(); + + const filePath = path.resolve(tmpDir, 'tmp-chmod.txt'); + const chmodFileDetails = { + filePath, + fileHandle: await open(filePath, 'w+', 0o666) + }; + + await validateFilePermission(chmodFileDetails); +} + +executeTests().then(common.mustCall()); From 5183962d73474ab9994fee01e86b2dcbbb5d5240 Mon Sep 17 00:00:00 2001 From: willhayslett Date: Wed, 28 Mar 2018 01:41:38 -0500 Subject: [PATCH 2/6] 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. --- ...est-fs-promises-file-handle-append-file.js | 50 ++++++++-------- .../test-fs-promises-file-handle-chmod.js | 33 ++++------- .../test-fs-promises-file-handle-read.js | 57 +++++++++++++++++++ .../test-fs-promises-file-handle-readFile.js | 33 +++++++++++ .../test-fs-promises-file-handle-write.js | 56 ++++++++++++++++++ .../test-fs-promises-file-handle-writeFile.js | 33 +++++++++++ 6 files changed, 212 insertions(+), 50 deletions(-) create mode 100644 test/parallel/test-fs-promises-file-handle-read.js create mode 100644 test/parallel/test-fs-promises-file-handle-readFile.js create mode 100644 test/parallel/test-fs-promises-file-handle-write.js create mode 100644 test/parallel/test-fs-promises-file-handle-writeFile.js diff --git a/test/parallel/test-fs-promises-file-handle-append-file.js b/test/parallel/test-fs-promises-file-handle-append-file.js index 99476a19298dab..77b6f8de812bb8 100644 --- a/test/parallel/test-fs-promises-file-handle-append-file.js +++ b/test/parallel/test-fs-promises-file-handle-append-file.js @@ -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()); diff --git a/test/parallel/test-fs-promises-file-handle-chmod.js b/test/parallel/test-fs-promises-file-handle-chmod.js index c49b9021cc061c..a4c9a27c53a0cd 100644 --- a/test/parallel/test-fs-promises-file-handle-chmod.js +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -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 + await fileHandle.chmod(0o765); + const statsAfterMod = fs.statSync(filePath); + assert.deepStrictEqual(statsAfterMod.mode & 0o765, 0o765); } -executeTests().then(common.mustCall()); +validateFilePermission().then(common.mustCall()); diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js new file mode 100644 index 00000000000000..d83c2d56a85e6e --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -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()); diff --git a/test/parallel/test-fs-promises-file-handle-readFile.js b/test/parallel/test-fs-promises-file-handle-readFile.js new file mode 100644 index 00000000000000..87e115a11780db --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-readFile.js @@ -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()); diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js new file mode 100644 index 00000000000000..b509956317ee82 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -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()); diff --git a/test/parallel/test-fs-promises-file-handle-writeFile.js b/test/parallel/test-fs-promises-file-handle-writeFile.js new file mode 100644 index 00000000000000..d24b86b6330cd1 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-writeFile.js @@ -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()); From 74b6ba1b2f63e09d30513e33d5afd5bad982d732 Mon Sep 17 00:00:00 2001 From: willhayslett Date: Thu, 29 Mar 2018 22:13:31 -0500 Subject: [PATCH 3/6] 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. --- .../test-fs-promises-file-handle-read.js | 19 ++++--------- .../test-fs-promises-file-handle-readFile.js | 5 ++-- .../test-fs-promises-file-handle-write.js | 27 +++++-------------- .../test-fs-promises-file-handle-writeFile.js | 8 ++---- 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index d83c2d56a85e6e..303812ea0de7a3 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -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() { @@ -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() diff --git a/test/parallel/test-fs-promises-file-handle-readFile.js b/test/parallel/test-fs-promises-file-handle-readFile.js index 87e115a11780db..f13921bf3c1f38 100644 --- a/test/parallel/test-fs-promises-file-handle-readFile.js +++ b/test/parallel/test-fs-promises-file-handle-readFile.js @@ -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() diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js index b509956317ee82..1b90f18bb5cfd0 100644 --- a/test/parallel/test-fs-promises-file-handle-write.js +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -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() diff --git a/test/parallel/test-fs-promises-file-handle-writeFile.js b/test/parallel/test-fs-promises-file-handle-writeFile.js index d24b86b6330cd1..a347a415a26497 100644 --- a/test/parallel/test-fs-promises-file-handle-writeFile.js +++ b/test/parallel/test-fs-promises-file-handle-writeFile.js @@ -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() From 83776672bf4f4ec943033c0e810bf0da7eadb0c0 Mon Sep 17 00:00:00 2001 From: willhayslett Date: Tue, 3 Apr 2018 00:44:53 -0500 Subject: [PATCH 4/6] test: updated fs/promises tests to conform to new linting rules Tests were updated to conform to the new linting rules that were landed. --- test/parallel/test-fs-promises-file-handle-append-file.js | 2 +- test/parallel/test-fs-promises-file-handle-chmod.js | 4 ++-- test/parallel/test-fs-promises-file-handle-write.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-fs-promises-file-handle-append-file.js b/test/parallel/test-fs-promises-file-handle-append-file.js index 77b6f8de812bb8..064a962a4efa25 100644 --- a/test/parallel/test-fs-promises-file-handle-append-file.js +++ b/test/parallel/test-fs-promises-file-handle-append-file.js @@ -26,7 +26,7 @@ async function validateAppendBuffer() { } async function validateAppendString() { - //don't refresh the directory + // don't refresh the directory common.crashOnUnhandledRejection(); const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt'); diff --git a/test/parallel/test-fs-promises-file-handle-chmod.js b/test/parallel/test-fs-promises-file-handle-chmod.js index a4c9a27c53a0cd..48e63b5a357246 100644 --- a/test/parallel/test-fs-promises-file-handle-chmod.js +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -18,10 +18,10 @@ async function validateFilePermission() { const filePath = path.resolve(tmpDir, 'tmp-chmod.txt'); const fileHandle = await open(filePath, 'w+', 0o644); - //file created with r/w 644 + // file created with r/w 644 const statsBeforeMod = fs.statSync(filePath); assert.deepStrictEqual(statsBeforeMod.mode & 0o644, 0o644); - //change the permissions to 765 + // change the permissions to 765 await fileHandle.chmod(0o765); const statsAfterMod = fs.statSync(filePath); assert.deepStrictEqual(statsAfterMod.mode & 0o765, 0o765); diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js index 1b90f18bb5cfd0..e90eaca03dfa7c 100644 --- a/test/parallel/test-fs-promises-file-handle-write.js +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -31,7 +31,7 @@ async function validateEmptyWrite() { const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.txt'); const fileHandle = await open(filePathForHandle, 'w+'); - const buffer = Buffer.from(''); //empty buffer + const buffer = Buffer.from(''); // empty buffer await fileHandle.write(buffer, 0, buffer.length); const readFileData = fs.readFileSync(filePathForHandle); From eb0955be6d59c6c83be9963e9601b7ee7999e7ce Mon Sep 17 00:00:00 2001 From: willhayslett Date: Wed, 4 Apr 2018 01:18:37 -0500 Subject: [PATCH 5/6] test: resolving cross platform support issues Updated fs/promises fileHandle tests to resolve CI build test errors being returned from Windows and Rasberry Pi. --- .../test-fs-promises-file-handle-chmod.js | 26 ++++++++++++++----- .../test-fs-promises-file-handle-read.js | 1 - 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-fs-promises-file-handle-chmod.js b/test/parallel/test-fs-promises-file-handle-chmod.js index 48e63b5a357246..5cbdcf06b9f4ed 100644 --- a/test/parallel/test-fs-promises-file-handle-chmod.js +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -17,14 +17,28 @@ async function validateFilePermission() { common.crashOnUnhandledRejection(); const filePath = path.resolve(tmpDir, 'tmp-chmod.txt'); - const fileHandle = await open(filePath, 'w+', 0o644); - // file created with r/w 644 + const fileHandle = await open(filePath, 'w+', 0o444); + // file created with r--r--r-- 444 const statsBeforeMod = fs.statSync(filePath); - assert.deepStrictEqual(statsBeforeMod.mode & 0o644, 0o644); - // change the permissions to 765 - await fileHandle.chmod(0o765); + assert.deepStrictEqual(statsBeforeMod.mode & 0o444, 0o444); + + let expectedAccess; + const newPermissions = 0o765; + + if (common.isWindows) { + // chmod in Windows will only toggle read only/write access. the + // fs.Stats.mode in Windows is computed using read/write + // bits (not exec). read only at best returns 444; r/w 666. + // refer: /deps/uv/src/win/fs.cfs; + expectedAccess = 0o664; + } else { + expectedAccess = newPermissions; + } + + //change the permissions to rwxr--r-x + await fileHandle.chmod(newPermissions); const statsAfterMod = fs.statSync(filePath); - assert.deepStrictEqual(statsAfterMod.mode & 0o765, 0o765); + assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess); } validateFilePermission().then(common.mustCall()); diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index 303812ea0de7a3..d2e0b67cb90f16 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -29,7 +29,6 @@ async function validateRead() { } async function validateEmptyRead() { - tmpdir.refresh(); common.crashOnUnhandledRejection(); const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt'); From 5310f921d34e2625a18bb9b2fc1d6ff2d5082a2b Mon Sep 17 00:00:00 2001 From: willhayslett Date: Wed, 4 Apr 2018 06:51:35 -0500 Subject: [PATCH 6/6] test: refactored use of common methods in fs/promises tests Updated tests to ensure common.crashOnUnhandledRejection() and the common/tmpdir refresh method and are only being called once per file. --- .../test-fs-promises-file-handle-append-file.js | 15 +++++---------- .../test-fs-promises-file-handle-chmod.js | 8 ++++---- .../parallel/test-fs-promises-file-handle-read.js | 8 +++----- .../test-fs-promises-file-handle-readFile.js | 6 +++--- .../test-fs-promises-file-handle-write.js | 13 +++++-------- .../test-fs-promises-file-handle-writeFile.js | 6 +++--- 6 files changed, 23 insertions(+), 33 deletions(-) diff --git a/test/parallel/test-fs-promises-file-handle-append-file.js b/test/parallel/test-fs-promises-file-handle-append-file.js index 064a962a4efa25..38336a2b43a57e 100644 --- a/test/parallel/test-fs-promises-file-handle-append-file.js +++ b/test/parallel/test-fs-promises-file-handle-append-file.js @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateAppendBuffer() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); +async function validateAppendBuffer() { 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'); @@ -26,19 +26,14 @@ async function validateAppendBuffer() { } async function validateAppendString() { - // don't refresh the directory - common.crashOnUnhandledRejection(); - - const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt'); + const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt'); const fileHandle = await open(filePath, 'a'); - const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8'); const string = 'x~yz'.repeat(100); 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); + assert.deepStrictEqual(appendedFileData, stringAsBuffer); } validateAppendBuffer() diff --git a/test/parallel/test-fs-promises-file-handle-chmod.js b/test/parallel/test-fs-promises-file-handle-chmod.js index 5cbdcf06b9f4ed..c2a44fba7bd32c 100644 --- a/test/parallel/test-fs-promises-file-handle-chmod.js +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateFilePermission() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); +async function validateFilePermission() { const filePath = path.resolve(tmpDir, 'tmp-chmod.txt'); const fileHandle = await open(filePath, 'w+', 0o444); // file created with r--r--r-- 444 @@ -35,7 +35,7 @@ async function validateFilePermission() { expectedAccess = newPermissions; } - //change the permissions to rwxr--r-x + // change the permissions to rwxr--r-x await fileHandle.chmod(newPermissions); const statsAfterMod = fs.statSync(filePath); assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess); diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index d2e0b67cb90f16..5a9bc4558cf15f 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateRead() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); +async function validateRead() { const filePath = path.resolve(tmpDir, 'tmp-read-file.txt'); const fileHandle = await open(filePath, 'w+'); const buffer = Buffer.from('Hello world', 'utf8'); @@ -29,8 +29,6 @@ async function validateRead() { } async function validateEmptyRead() { - common.crashOnUnhandledRejection(); - const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt'); const fileHandle = await open(filePath, 'w+'); const buffer = Buffer.from('', 'utf8'); diff --git a/test/parallel/test-fs-promises-file-handle-readFile.js b/test/parallel/test-fs-promises-file-handle-readFile.js index f13921bf3c1f38..9308c299092714 100644 --- a/test/parallel/test-fs-promises-file-handle-readFile.js +++ b/test/parallel/test-fs-promises-file-handle-readFile.js @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateReadFile() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); +async function validateReadFile() { const filePath = path.resolve(tmpDir, 'tmp-read-file.txt'); const fileHandle = await open(filePath, 'w+'); const buffer = Buffer.from('Hello world'.repeat(100), 'utf8'); diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js index e90eaca03dfa7c..842095a214c2ef 100644 --- a/test/parallel/test-fs-promises-file-handle-write.js +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -12,11 +12,11 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateWrite() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); - const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.txt'); +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'); @@ -26,10 +26,7 @@ async function validateWrite() { } async function validateEmptyWrite() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); - - const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.txt'); + const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt'); const fileHandle = await open(filePathForHandle, 'w+'); const buffer = Buffer.from(''); // empty buffer diff --git a/test/parallel/test-fs-promises-file-handle-writeFile.js b/test/parallel/test-fs-promises-file-handle-writeFile.js index a347a415a26497..196b6f8db8cd58 100644 --- a/test/parallel/test-fs-promises-file-handle-writeFile.js +++ b/test/parallel/test-fs-promises-file-handle-writeFile.js @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const tmpDir = tmpdir.path; -async function validateWriteFile() { - tmpdir.refresh(); - common.crashOnUnhandledRejection(); +tmpdir.refresh(); +common.crashOnUnhandledRejection(); +async function validateWriteFile() { const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt'); const fileHandle = await open(filePathForHandle, 'w+'); const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');