From 9a42d6491122b9e8d81ceddc7c29ad3a61171317 Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:26:38 +0300 Subject: [PATCH] add sync tests variation --- test/flush.test.js | 103 +++++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/test/flush.test.js b/test/flush.test.js index 0f18535..f886b0d 100644 --- a/test/flush.test.js +++ b/test/flush.test.js @@ -134,7 +134,7 @@ function buildTests (test, sync) { const fd = fs.openSync(dest, 'w') const stream = new SonicBoom({ fd, - sync: false, + sync, fsync: true, minLength: 4096 }) @@ -151,10 +151,18 @@ function buildTests (test, sync) { t.pass('fake fsyncSync called') } - fakeFs.write = function (...args) { - t.pass('fake fs.write called') - fakeFs.write = fs.write - return fakeFs.write(...args) + function successOnAsyncOrSyncFn (isSync, originalFn) { + return function (...args) { + t.pass(`fake fs.${originalFn.name} called`) + fakeFs[originalFn.name] = originalFn + return fakeFs[originalFn.name](...args) + } + } + + if (sync) { + fakeFs.writeSync = successOnAsyncOrSyncFn(true, fs.writeSync) + } else { + fakeFs.write = successOnAsyncOrSyncFn(false, fs.write) } t.ok(stream.write('hello world\n')) @@ -181,7 +189,7 @@ function buildTests (test, sync) { const fd = fs.openSync(dest, 'w') const stream = new SonicBoom({ fd, - sync: false, + sync, minLength: 4096 }) @@ -191,17 +199,33 @@ function buildTests (test, sync) { const err = new Error('other') err.code = 'other' - fakeFs.fsync = function (fd, cb) { - fakeFs.fsync = fs.fsync - Error.captureStackTrace(err) - t.pass('fake fs.fsync called') - cb(err) + + function onFsyncOnFsyncSync (isSync, originalFn) { + return function (...args) { + Error.captureStackTrace(err) + t.pass(`fake fs.${originalFn.name} called`) + fakeFs[originalFn.name] = originalFn + const cb = args[args.length - 1] + + cb(err) + } } - fakeFs.write = function (...args) { - t.pass('fake fs.write called') - fakeFs.write = fs.write - return fakeFs.write(...args) + // only one is called depends on sync + fakeFs.fsync = onFsyncOnFsyncSync(false, fs.fsync) + + function successOnAsyncOrSyncFn (isSync, originalFn) { + return function (...args) { + t.pass(`fake fs.${originalFn.name} called`) + fakeFs[originalFn.name] = originalFn + return fakeFs[originalFn.name](...args) + } + } + + if (sync) { + fakeFs.writeSync = successOnAsyncOrSyncFn(true, fs.writeSync) + } else { + fakeFs.write = successOnAsyncOrSyncFn(false, fs.write) } t.ok(stream.write('hello world\n')) @@ -267,7 +291,7 @@ function buildTests (test, sync) { const fd = fs.openSync(dest, 'w') const stream = new SonicBoom({ fd, - sync: false, + sync, minLength: 4096 }) @@ -277,13 +301,24 @@ function buildTests (test, sync) { const err = new Error('other') err.code = 'other' - fakeFs.write = function (fd, buf, enc, cb) { - fakeFs.write = fs.write - Error.captureStackTrace(err) - t.pass('fake fs.write called') - cb(err) + + function onWriteOrWriteSync (isSync, originalFn) { + return function (...args) { + Error.captureStackTrace(err) + t.pass(`fake fs.${originalFn.name} called`) + fakeFs[originalFn.name] = originalFn + + if (isSync) throw err + const cb = args[args.length - 1] + + cb(err) + } } + // only one is called depends on sync + fakeFs.write = onWriteOrWriteSync(false, fs.write) + fakeFs.writeSync = onWriteOrWriteSync(true, fs.writeSync) + t.ok(stream.write('hello world\n')) stream.flush((err) => { if (err) t.equal(err.code, 'other') @@ -309,7 +344,7 @@ function buildTests (test, sync) { const fd = fs.openSync(dest, 'w') const stream = new SonicBoom({ fd, - sync: false, + sync, // to trigger write without calling flush minLength: 1 @@ -319,17 +354,23 @@ function buildTests (test, sync) { t.pass('ready emitted') }) - fakeFs.write = function (...args) { - stream.flush((err) => { - if (err) t.fail(err) - else t.pass('flush cb called') - }) - - t.pass('fake fs.write called') - fakeFs.write = fs.write - return fakeFs.write(...args) + function onWriteOrWriteSync (originalFn) { + return function (...args) { + stream.flush((err) => { + if (err) t.fail(err) + else t.pass('flush cb called') + }) + + t.pass(`fake fs.${originalFn.name} called`) + fakeFs[originalFn.name] = originalFn + return originalFn(...args) + } } + // only one is called depends on sync + fakeFs.write = onWriteOrWriteSync(fs.write) + fakeFs.writeSync = onWriteOrWriteSync(fs.writeSync) + t.ok(stream.write('hello world\n')) }) }