From 92512c876b766867a7b5c0ecaf22de1c96a9d3fa Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 22 Oct 2023 22:15:14 +0200 Subject: [PATCH] Change snapshot tests to compare decompressed bitstreams The snapshots are compressed differently in Node.js 21. The compression has been stable for many versions but that is not a guarantee, see https://github.com/nodejs/node/issues/50138 for background. Change tests to compare the decompressed data instead. --- lib/snapshot-manager.js | 8 +++++++- test/snapshot-workflow/helpers/macros.js | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index 0a52cd76a..9a9abaa92 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -198,7 +198,7 @@ async function encodeSnapshots(snapshotData) { ], READABLE_PREFIX.byteLength + VERSION_HEADER.byteLength + SHA_256_HASH_LENGTH + compressed.byteLength); } -function decodeSnapshots(buffer, snapPath) { +export function extractCompressedSnapshot(buffer, snapPath) { if (isLegacySnapshot(buffer)) { throw new LegacyError(snapPath); } @@ -220,6 +220,12 @@ function decodeSnapshots(buffer, snapPath) { const compressedOffset = sha256sumOffset + SHA_256_HASH_LENGTH; const compressed = buffer.slice(compressedOffset); + return {version, compressed, sha256sumOffset, compressedOffset}; +} + +function decodeSnapshots(buffer, snapPath) { + const {compressed, sha256sumOffset, compressedOffset} = extractCompressedSnapshot(buffer, snapPath); + const sha256sum = crypto.createHash('sha256').update(compressed).digest(); const expectedSum = buffer.slice(sha256sumOffset, compressedOffset); if (!sha256sum.equals(expectedSum)) { diff --git a/test/snapshot-workflow/helpers/macros.js b/test/snapshot-workflow/helpers/macros.js index 835316231..00deecf53 100644 --- a/test/snapshot-workflow/helpers/macros.js +++ b/test/snapshot-workflow/helpers/macros.js @@ -1,9 +1,11 @@ import {promises as fs} from 'node:fs'; import path from 'node:path'; +import {gunzipSync} from 'node:zlib'; import concordance from 'concordance'; +import {extractCompressedSnapshot} from '../../../lib/snapshot-manager.js'; import {fixture} from '../../helpers/exec.js'; import {withTemporaryFixture} from '../../helpers/with-temporary-fixture.js'; @@ -63,9 +65,12 @@ export async function beforeAndAfter(t, { } async function readSnapshots(cwd) { + const snapPath = path.join(cwd, 'test.js.snap'); const [snapshot, report] = await Promise.all([ - fs.readFile(path.join(cwd, 'test.js.snap')), + fs.readFile(snapPath), fs.readFile(path.join(cwd, 'test.js.md'), 'utf8'), ]); - return {snapshot, report}; + + const {version, compressed} = extractCompressedSnapshot(snapshot, snapPath); + return {snapshot: {version, decompressed: gunzipSync(compressed)}, report}; }