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 23, 2023
1 parent a10c989 commit 922bc91
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 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
6 changes: 4 additions & 2 deletions test-tap/integration/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {gunzipSync} from 'node:zlib';

import {execa} from 'execa';
import {test} from 'tap';
import {temporaryDirectory} from 'tempy';

import {extractCompressedSnapshot} from '../../lib/snapshot-manager.js';
import {execCli} from '../helper/cli.js';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
Expand Down Expand Up @@ -265,9 +267,9 @@ test('snapshots are identical on different platforms', t => {
t.ok(fs.existsSync(snapPath));

const reportContents = fs.readFileSync(reportPath);
const snapContents = fs.readFileSync(snapPath);
const snapContents = gunzipSync(extractCompressedSnapshot(fs.readFileSync(snapPath)).compressed);
const expectedReportContents = fs.readFileSync(expectedReportPath);
const expectedSnapContents = fs.readFileSync(expectedSnapPath);
const expectedSnapContents = gunzipSync(extractCompressedSnapshot(fs.readFileSync(expectedSnapPath)).compressed);

t.ok(reportContents.equals(expectedReportContents), 'report file contents matches snapshot');
t.ok(snapContents.equals(expectedSnapContents), 'snap file contents matches snapshot');
Expand Down
Binary file modified test/snapshot-order/fixtures/randomness/test.js.snap
Binary file not shown.
5 changes: 4 additions & 1 deletion test/snapshot-order/randomness.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import {gunzipSync} from 'node:zlib';

import test from '@ava/test';

import {extractCompressedSnapshot} from '../../lib/snapshot-manager.js';
import {cwd, fixture} from '../helpers/exec.js';

import getSnapshotIds from './helpers/get-snapshot-ids.js';
Expand All @@ -23,8 +25,9 @@ test('deterministic and sorted over a large, random test case', async t => {

// Assert snapshot is unchanged
const snapshot = fs.readFileSync(snapshotPath);
const {compressed} = extractCompressedSnapshot(snapshot, snapshotPath);

t.snapshot(snapshot, 'resulting snapshot in binary encoding');
t.snapshot(gunzipSync(compressed), 'resulting snapshot in binary encoding');

// Assert report is sorted
const report = fs.readFileSync(reportPath);
Expand Down
32 changes: 22 additions & 10 deletions test/snapshot-order/snapshots/randomness.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ Generated by [AVA](https://avajs.dev).
> resulting snapshot in binary encoding
Buffer @Uint8Array [
41564120 536e6170 73686f74 2076330a 03008ba7 6ff7a1a2 251e5fa1 afbf3fa8
fdf7ca69 3e5b2af2 b0734c95 46791417 ae201f8b 08000000 00000003 5b989694
939f9c5d dcba28b5 24b32427 b542ca49 41572125 353927b1 2855a138 3f1748e4
25161467 e4971467 c259cd0b 5352124b 12c39819 d8194080 91954190 9123332f
25b5c24a c160111e 49c3d49c c4a4d49c dc8cc462 854485dc d4e2e2c4 f4547c1a
8c201a8a 12738af3 15507541 5d2cef88 eee2dcfc 22acce6e c4e76c13 9879aace
38434021 330f687f 4951a586 265260e0 73be29c4 f9a9f9a5 25a945f8 149a4115
66e6e5e1 57680e51 980d3651 21313d31 330fee72 5790cb33 8b93138b 52887179
133e7b2c 305d1e8e a2901316 a506385d 54e40274 10aed868 5a88cf50 23bcb2c6
00d6e9f1 35b80200 00
a166626c 6f636b73 85a26574 69746c65 781a4220 2d206465 636c6172 6520736f
6d652073 6e617073 686f7473 69736e61 7073686f 747383a1 64646174 61560300
07000000 00000105 00110108 696e6465 783a2030 a2646461 74615603 00070000
00000001 05001101 08696e64 65783a20 31656c61 62656c6d 68617320 61206d65
73736167 65a26464 61746156 03000700 00000000 01050011 0108696e 6465783a
2032656c 6162656c 72616c73 6f206861 73206120 6d657373 616765a2 65746974
6c65781f 41202d20 6465636c 61726520 736f6d65 206d6f72 6520736e 61707368
6f747369 736e6170 73686f74 7381a164 64617461 56030007 00000000 00010500
11010869 6e646578 3a2034a2 65746974 6c657825 43202d20 6465636c 61726520
736f6d65 20736e61 7073686f 74732069 6e206120 74727928 2969736e 61707368
6f747383 a2646461 74615603 00070000 00000001 05001101 08696e64 65783a20
35656c61 62656c65 6f757465 72a26464 61746156 03000700 00000000 01050011
0108696e 6465783a 2036656c 6162656c 65696e6e 6572a264 64617461 56030007
00000000 00010500 11010869 6e646578 3a203765 6c616265 6c6b6f75 74657220
61676169 6ea26574 69746c65 78254520 2d206469 73636172 6420736f 6d652073
6e617073 686f7473 20696e20 61207472 79282969 736e6170 73686f74 7382a264
64617461 56030007 00000000 00010500 11010869 6e646578 3a203865 6c616265
6c656f75 746572a2 64646174 61570300 07000000 00000105 00110109 696e6465
783a2031 30656c61 62656c6b 6f757465 72206167 61696ea2 65746974 6c657244
202d206d 6f726520 736e6170 73686f74 7369736e 61707368 6f747382 a1646461
74615703 00070000 00000001 05001101 09696e64 65783a20 3132a164 64617461
57030007 00000000 00010500 11010969 6e646578 3a203133
]
Binary file modified test/snapshot-order/snapshots/randomness.js.snap
Binary file not shown.
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 922bc91

Please sign in to comment.