-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
test: adding tests for fs/promises.js fileHandle methods #19605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
willhayslett
wants to merge
6
commits into
nodejs:master
from
willhayslett:test-fs-promises-file-handle
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f98fc81
test: adding tests for fs/promises.js fileHandle methods
willhayslett 5183962
test: additional tests for fs/promises fileHandle
willhayslett 74b6ba1
test: updated tests for fs/promises fileHandle
willhayslett 8377667
test: updated fs/promises tests to conform to new linting rules
willhayslett eb0955b
test: resolving cross platform support issues
willhayslett 5310f92
test: refactored use of common methods in fs/promises tests
willhayslett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
commit 5183962d73474ab9994fee01e86b2dcbbb5d5240
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,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()); |
This file contains hidden or 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,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()); |
This file contains hidden or 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,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()); |
This file contains hidden or 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,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()); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.