From 327c6734cba332c2ac19cf0cdedaca1b918e7010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriela=20Ni=C3=B1o?= Date: Fri, 21 Jun 2019 17:41:19 -0500 Subject: [PATCH] test: refactor test-fs-write-sync Refactor the code for the test-fs-write-sync to avoid code repetition and to make it simpler. PR-URL: https://github.com/nodejs/node/pull/28371 Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen Reviewed-By: Yongsheng Zhang Reviewed-By: Ruben Bridgewater --- test/parallel/test-fs-write-sync.js | 56 +++++++++-------------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/test/parallel/test-fs-write-sync.js b/test/parallel/test-fs-write-sync.js index 1873c44fb9ae33..13e8cc9fc7054a 100644 --- a/test/parallel/test-fs-write-sync.js +++ b/test/parallel/test-fs-write-sync.js @@ -29,50 +29,28 @@ const filename = path.join(tmpdir.path, 'write.txt'); tmpdir.refresh(); -// fs.writeSync with all parameters provided: { - const fd = fs.openSync(filename, 'w'); + const parameters = [Buffer.from('bár'), 0, Buffer.byteLength('bár')]; - let written = fs.writeSync(fd, ''); - assert.strictEqual(written, 0); + // The first time fs.writeSync is called with all parameters provided. + // After that, each pop in the cycle removes the final parameter. So: + // - The 2nd time fs.writeSync with a buffer, without the length parameter. + // - The 3rd time fs.writeSync with a buffer, without the offset and length + // parameters. + while (parameters.length > 0) { + const fd = fs.openSync(filename, 'w'); - fs.writeSync(fd, 'foo'); + let written = fs.writeSync(fd, ''); + assert.strictEqual(written, 0); - written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár')); - assert.ok(written > 3); - fs.closeSync(fd); + fs.writeSync(fd, 'foo'); - assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); -} - -// fs.writeSync with a buffer, without the length parameter: -{ - const fd = fs.openSync(filename, 'w'); - - let written = fs.writeSync(fd, ''); - assert.strictEqual(written, 0); - - fs.writeSync(fd, 'foo'); - - written = fs.writeSync(fd, Buffer.from('bár'), 0); - assert.ok(written > 3); - fs.closeSync(fd); - - assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); -} - -// fs.writeSync with a buffer, without the offset and length parameters: -{ - const fd = fs.openSync(filename, 'w'); - - let written = fs.writeSync(fd, ''); - assert.strictEqual(written, 0); - - fs.writeSync(fd, 'foo'); + written = fs.writeSync(fd, ...parameters); + assert.ok(written > 3); + fs.closeSync(fd); - written = fs.writeSync(fd, Buffer.from('bár')); - assert.ok(written > 3); - fs.closeSync(fd); + assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); - assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); + parameters.pop(); + } }