forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: make sure that file is truncated in writeFile
Simple Reproduction of the Bug ```js 'use strict'; const assert = require('assert'); const fs = require('fs'); const fileName = 'test.txt'; fs.writeFileSync(fileName, '123'); fs.writeFileSync(fileName, '0'); const result1 = fs.readFileSync(fileName, 'utf8'); fs.unlinkSync(fileName); const fd = fs.openSync(fileName, 'w'); fs.writeFileSync(fd, '123'); fs.writeFileSync(fd, '0'); fs.closeSync(fd); const result2 = fs.readFileSync(fileName, 'utf8'); fs.unlinkSync(fileName); assert.deepStrictEqual(result2, result1); // + expected - actual // //- '023' //+ '0' ``` Fixes: nodejs#22554
- Loading branch information
1 parent
aba7677
commit 301482b
Showing
3 changed files
with
104 additions
and
4 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
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
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,76 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fsPromises = require('fs').promises; | ||
const tmpdir = require('../common/tmpdir'); | ||
const tmpDir = tmpdir.path; | ||
tmpdir.refresh(); | ||
|
||
// Length of First should be greater than the length of Second. When | ||
// the file is written the second time, it should clear out all the | ||
// contents of the file instead of starting at the end of the file or | ||
// at the beginning of the file replacing characters in the existing | ||
// content. | ||
const First = 'Callbacks'; | ||
const Second = 'Promises'; | ||
|
||
{ | ||
const fileName = path.resolve(tmpDir, 'first.txt'); | ||
fs.writeFileSync(fileName, First); | ||
fs.writeFileSync(fileName, Second); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
} | ||
|
||
{ | ||
const fileName = path.resolve(tmpDir, 'second.txt'); | ||
const fd = fs.openSync(fileName, 'w'); | ||
fs.writeFileSync(fd, First); | ||
fs.writeFileSync(fd, Second); | ||
fs.closeSync(fd); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
} | ||
|
||
(async function() { | ||
const fileName = path.resolve(tmpDir, 'third.txt'); | ||
await fsPromises.writeFile(fileName, First); | ||
await fsPromises.writeFile(fileName, Second); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
})().then(common.mustCall()); | ||
|
||
(async function() { | ||
const fileName = path.resolve(tmpDir, 'fourth.txt'); | ||
const fileHandle = await fsPromises.open(fileName, 'w'); | ||
await fileHandle.writeFile(First); | ||
await fileHandle.writeFile(Second); | ||
await fileHandle.close(); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
})().then(common.mustCall()); | ||
|
||
{ | ||
const fileName = path.resolve(tmpDir, 'fifth.txt'); | ||
fs.writeFile(fileName, First, common.mustCall(function(err) { | ||
assert.ifError(err); | ||
fs.writeFile(fileName, Second, common.mustCall(function(err) { | ||
assert.ifError(err); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
const fileName = path.resolve(tmpDir, 'sixth.txt'); | ||
fs.open(fileName, 'w', common.mustCall(function(err, handle) { | ||
assert.ifError(err); | ||
fs.writeFile(handle, First, common.mustCall(function(err) { | ||
assert.ifError(err); | ||
fs.writeFile(handle, Second, common.mustCall(function(err) { | ||
assert.ifError(err); | ||
fs.closeSync(handle); | ||
assert.deepStrictEqual(fs.readFileSync(fileName, 'utf8'), Second); | ||
})); | ||
})); | ||
})); | ||
} |