forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nodejs#19605 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
1 parent
0876a03
commit 126b03e
Showing
6 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |