Skip to content

Commit

Permalink
Change snapshot tests to compare decompressed bitstreams
Browse files Browse the repository at this point in the history
The snapshots are compressed differently in Node.js 21. The compression has been stable for many versions but that is not a guarantee, see nodejs/node#50138 for background.

Change tests to compare the decompressed data instead.
  • Loading branch information
novemberborn committed Oct 22, 2023
1 parent a10c989 commit 92512c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)) {
Expand Down
9 changes: 7 additions & 2 deletions test/snapshot-workflow/helpers/macros.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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};
}

0 comments on commit 92512c8

Please sign in to comment.