From 12f536afe55614188ef94f58339420ef3f80b6bf Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Mon, 26 Oct 2020 06:38:01 -0600 Subject: [PATCH] test: Fix _flush test for Node.js 15 On Node.js 15, as a result of https://github.com/nodejs/node/pull/34101 the callback passed to #end() is not called if an error occurs in that the InflateAuto behavior matches the stream.Transform behavior. Signed-off-by: Kevin Locke --- test/inflate-auto.js | 57 +++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/test/inflate-auto.js b/test/inflate-auto.js index d50d103..5e4cd13 100644 --- a/test/inflate-auto.js +++ b/test/inflate-auto.js @@ -962,30 +962,49 @@ function defineFormatTests(format) { }); // Test handling in _flush - it(`on end with ${inspect(options)}`, (done) => { - let errInflate; - // eslint-disable-next-line no-new - try { new Decompress(options); } catch (err) { errInflate = err; } - assert(errInflate); + // + // Starting in Node 15 (c7e55c6b72fb5a7032bc12c74329fa840076dec0) + // https://github.com/nodejs/node/pull/34101 + // The .end() callback is not called on error in _flush. + // + // Test that InflateAuto behaves the same as stream.Transform when + // an error occurs in _flush. + it(`on end with ${inspect(options)}`, () => { + const transform = new stream.Transform({ + transform: (chunk, encoding, cb) => cb(), + flush: (cb) => { + try { + // eslint-disable-next-line no-new + new Decompress(options); + assert.fail('Constructor should throw for invalid options'); + } catch (err) { + cb(err); + } + }, + }); const inflateAuto = new InflateAuto({ ...options, defaultFormat: Decompress, }); - let errorEmitted = false; - inflateAuto.on('error', (errAuto) => { - assert(!errorEmitted, 'error emitted at most once'); - errorEmitted = true; - assert.deepStrictEqual(errAuto, errInflate); - }); - inflateAuto.end((errAuto) => { - assert(errorEmitted, 'error emitted at least once'); - // Errors passed to _flush callback may not be passed to end cb. - if (errAuto) { - assert.deepStrictEqual(errAuto, errInflate); - } - done(); - }); + + let errorCount = 0; + inflateAuto.on('error', () => { errorCount += 1; }); + + const result = streamCompare(inflateAuto, transform, COMPARE_OPTIONS) + .then(() => { + assert.strictEqual(errorCount, 1, 'error emitted once'); + }); + + let transformEndArgs; + transform.end((...args) => { transformEndArgs = args; }); + let inflateAutoEndArgs; + inflateAuto.end((...args) => { inflateAutoEndArgs = args; }); + + // end callback may or may not be called. Check after stream compare. + return result.then( + () => assert.deepStrictEqual(inflateAutoEndArgs, transformEndArgs), + ); }); }