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: add flush option to createWriteStream()
This commit adds a 'flush' option to the createWriteStream() family of functions. Refs: nodejs#49886 PR-URL: nodejs#50093 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
- Loading branch information
1 parent
3a34d54
commit 2d8030f
Showing
5 changed files
with
118 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
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,81 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('node:assert'); | ||
const fs = require('node:fs'); | ||
const fsp = require('node:fs/promises'); | ||
const test = require('node:test'); | ||
const data = 'foo'; | ||
let cnt = 0; | ||
|
||
function nextFile() { | ||
return tmpdir.resolve(`${cnt++}.out`); | ||
} | ||
|
||
tmpdir.refresh(); | ||
|
||
test('validation', () => { | ||
for (const flush of ['true', '', 0, 1, [], {}, Symbol()]) { | ||
assert.throws(() => { | ||
fs.createWriteStream(nextFile(), { flush }); | ||
}, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
} | ||
}); | ||
|
||
test('performs flush', (t, done) => { | ||
const spy = t.mock.method(fs, 'fsync'); | ||
const file = nextFile(); | ||
const stream = fs.createWriteStream(file, { flush: true }); | ||
|
||
stream.write(data, common.mustSucceed(() => { | ||
stream.close(common.mustSucceed(() => { | ||
const calls = spy.mock.calls; | ||
assert.strictEqual(calls.length, 1); | ||
assert.strictEqual(calls[0].result, undefined); | ||
assert.strictEqual(calls[0].error, undefined); | ||
assert.strictEqual(calls[0].arguments.length, 2); | ||
assert.strictEqual(typeof calls[0].arguments[0], 'number'); | ||
assert.strictEqual(typeof calls[0].arguments[1], 'function'); | ||
assert.strictEqual(fs.readFileSync(file, 'utf8'), data); | ||
done(); | ||
})); | ||
})); | ||
}); | ||
|
||
test('does not perform flush', (t, done) => { | ||
const values = [undefined, null, false]; | ||
const spy = t.mock.method(fs, 'fsync'); | ||
let cnt = 0; | ||
|
||
for (const flush of values) { | ||
const file = nextFile(); | ||
const stream = fs.createWriteStream(file, { flush }); | ||
|
||
stream.write(data, common.mustSucceed(() => { | ||
stream.close(common.mustSucceed(() => { | ||
assert.strictEqual(fs.readFileSync(file, 'utf8'), data); | ||
cnt++; | ||
|
||
if (cnt === values.length) { | ||
assert.strictEqual(spy.mock.calls.length, 0); | ||
done(); | ||
} | ||
})); | ||
})); | ||
} | ||
}); | ||
|
||
test('works with file handles', async () => { | ||
const file = nextFile(); | ||
const handle = await fsp.open(file, 'w'); | ||
const stream = handle.createWriteStream({ flush: true }); | ||
|
||
return new Promise((resolve) => { | ||
stream.write(data, common.mustSucceed(() => { | ||
stream.close(common.mustSucceed(() => { | ||
assert.strictEqual(fs.readFileSync(file, 'utf8'), data); | ||
resolve(); | ||
})); | ||
})); | ||
}); | ||
}); |