-
-
Notifications
You must be signed in to change notification settings - Fork 32k
fs: make writeFile consistent with readFile wrt fd #23709
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
/* | ||
* This test makes sure that `readFile()` always reads from the current | ||
* position of the file, instead of reading from the beginning of the file. | ||
*/ | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { writeFileSync } = require('fs'); | ||
const { open } = require('fs').promises; | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const fn = path.join(tmpdir.path, 'test.txt'); | ||
writeFileSync(fn, 'Hello World'); | ||
|
||
async function readFileTest() { | ||
const handle = await open(fn, 'r'); | ||
|
||
/* Read only five bytes, so that the position moves to five. */ | ||
const buf = Buffer.alloc(5); | ||
const { bytesRead } = await handle.read(buf, 0, 5, null); | ||
assert.strictEqual(bytesRead, 5); | ||
assert.deepStrictEqual(buf.toString(), 'Hello'); | ||
|
||
/* readFile() should read from position five, instead of zero. */ | ||
assert.deepStrictEqual((await handle.readFile()).toString(), ' World'); | ||
} | ||
|
||
|
||
readFileTest() | ||
.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,36 @@ | ||
'use strict'; | ||
|
||
/* | ||
* This test makes sure that `writeFile()` always writes from the current | ||
* position of the file, instead of truncating the file. | ||
*/ | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { readFileSync } = require('fs'); | ||
const { open } = require('fs').promises; | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const fn = path.join(tmpdir.path, 'test.txt'); | ||
|
||
async function writeFileTest() { | ||
const handle = await open(fn, 'w'); | ||
|
||
/* Write only five bytes, so that the position moves to five. */ | ||
const buf = Buffer.from('Hello'); | ||
const { bytesWritten } = await handle.write(buf, 0, 5, null); | ||
assert.strictEqual(bytesWritten, 5); | ||
|
||
/* Write some more with writeFile(). */ | ||
await handle.writeFile('World'); | ||
|
||
/* New content should be written at position five, instead of zero. */ | ||
assert.deepStrictEqual(readFileSync(fn).toString(), 'HelloWorld'); | ||
} | ||
|
||
|
||
writeFileTest() | ||
.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
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,58 @@ | ||
'use strict'; | ||
|
||
/* | ||
* This test makes sure that `writeFile()` always writes from the current | ||
* position of the file, instead of truncating the file, when used with file | ||
* descriptors. | ||
*/ | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const join = require('path').join; | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
{ | ||
/* writeFileSync() test. */ | ||
const filename = join(tmpdir.path, 'test.txt'); | ||
|
||
/* Open the file descriptor. */ | ||
const fd = fs.openSync(filename, 'w'); | ||
|
||
/* Write only five characters, so that the position moves to five. */ | ||
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5); | ||
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello'); | ||
|
||
/* Write some more with writeFileSync(). */ | ||
fs.writeFileSync(fd, 'World'); | ||
|
||
/* New content should be written at position five, instead of zero. */ | ||
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld'); | ||
} | ||
|
||
{ | ||
/* writeFile() test. */ | ||
const file = join(tmpdir.path, 'test1.txt'); | ||
|
||
/* Open the file descriptor. */ | ||
fs.open(file, 'w', common.mustCall((err, fd) => { | ||
assert.ifError(err); | ||
|
||
/* Write only five characters, so that the position moves to five. */ | ||
fs.write(fd, 'Hello', common.mustCall((err, bytes) => { | ||
assert.ifError(err); | ||
assert.strictEqual(bytes, 5); | ||
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'Hello'); | ||
|
||
/* Write some more with writeFile(). */ | ||
fs.writeFile(fd, 'World', common.mustCall((err) => { | ||
assert.ifError(err); | ||
|
||
/* New content should be written at position five, instead of zero. */ | ||
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'HelloWorld'); | ||
})); | ||
})); | ||
})); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.