diff options
Diffstat (limited to 'test')
6 files changed, 231 insertions, 0 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 new file mode 100644 index 0000000000..38336a2b43 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-append-file.js @@ -0,0 +1,41 @@ +'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; + +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'); + + await fileHandle.appendFile(buffer); + const appendedFileData = fs.readFileSync(filePath); + assert.deepStrictEqual(appendedFileData, buffer); +} + +async function validateAppendString() { + const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt'); + const fileHandle = await open(filePath, 'a'); + const string = 'x~yz'.repeat(100); + + await fileHandle.appendFile(string); + const stringAsBuffer = Buffer.from(string, 'utf8'); + const appendedFileData = fs.readFileSync(filePath); + assert.deepStrictEqual(appendedFileData, stringAsBuffer); +} + +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 new file mode 100644 index 0000000000..c2a44fba7b --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-chmod.js @@ -0,0 +1,44 @@ +'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; + +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 + const statsBeforeMod = fs.statSync(filePath); + 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 & 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 new file mode 100644 index 0000000000..5a9bc4558c --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -0,0 +1,45 @@ +'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 validateRead() { + 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); + 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() { + 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 readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0); + assert.deepStrictEqual(buffer.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 0000000000..9308c29909 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-readFile.js @@ -0,0 +1,32 @@ +'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; + +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'); + + const fd = fs.openSync(filePath, 'w+'); + fs.writeSync(fd, buffer, 0, buffer.length); + fs.closeSync(fd); + + const readFileData = await fileHandle.readFile(); + assert.deepStrictEqual(buffer, readFileData); +} + +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 0000000000..842095a214 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -0,0 +1,40 @@ +'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()); 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 0000000000..196b6f8db8 --- /dev/null +++ b/test/parallel/test-fs-promises-file-handle-writeFile.js @@ -0,0 +1,29 @@ +'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; + +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'); + + await fileHandle.writeFile(buffer); + const readFileData = fs.readFileSync(filePathForHandle); + assert.deepStrictEqual(buffer, readFileData); +} + +validateWriteFile() + .then(common.mustCall()); |