Skip to content

Commit efeb49d

Browse files
ratracegradtargos
authored andcommittedOct 3, 2018
test: increase test coverage for fs.promises read
PR-URL: #22800 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent c40e2dd commit efeb49d

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed
 

‎test/parallel/test-fs-promises.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ function verifyStatObject(stat) {
6464
assert.strictEqual(typeof stat.mode, 'number');
6565
}
6666

67+
async function getHandle(dest) {
68+
await copyFile(fixtures.path('baz.js'), dest);
69+
await access(dest, 'r');
70+
71+
return open(dest, 'r+');
72+
}
73+
6774
{
6875
async function doTest() {
6976
tmpdir.refresh();
@@ -93,6 +100,19 @@ function verifyStatObject(stat) {
93100
await handle.datasync();
94101
await handle.sync();
95102

103+
// test fs.read promises when length to read is zero bytes
104+
{
105+
const dest = path.resolve(tmpDir, 'test1.js');
106+
const handle = await getHandle(dest);
107+
const buf = Buffer.from('DAWGS WIN');
108+
const bufLen = buf.length;
109+
await handle.write(buf);
110+
const ret = await handle.read(Buffer.alloc(bufLen), 0, 0, 0);
111+
assert.strictEqual(ret.bytesRead, 0);
112+
113+
await unlink(dest);
114+
}
115+
96116
const buf = Buffer.from('hello fsPromises');
97117
const bufLen = buf.length;
98118
await handle.write(buf);
@@ -203,13 +223,22 @@ function verifyStatObject(stat) {
203223

204224
await unlink(newLink2);
205225

206-
const newdir = path.resolve(tmpDir, 'dir');
207-
await mkdir(newdir);
208-
stats = await stat(newdir);
209-
assert(stats.isDirectory());
210-
const list = await readdir(tmpDir);
211-
assert.deepStrictEqual(list, ['baz2.js', 'dir']);
212-
await rmdir(newdir);
226+
// testing readdir lists both files and directories
227+
{
228+
const newDir = path.resolve(tmpDir, 'dir');
229+
const newFile = path.resolve(tmpDir, 'foo.js');
230+
231+
await mkdir(newDir);
232+
await writeFile(newFile, 'DAWGS WIN!', 'utf8');
233+
234+
stats = await stat(newDir);
235+
assert(stats.isDirectory());
236+
const list = await readdir(tmpDir);
237+
assert.notStrictEqual(list.indexOf('dir'), -1);
238+
assert.notStrictEqual(list.indexOf('foo.js'), -1);
239+
await rmdir(newDir);
240+
await unlink(newFile);
241+
}
213242

214243
// mkdir when options is number.
215244
{

0 commit comments

Comments
 (0)
Please sign in to comment.