From c384e9a0ba2c7c25ee94e8600602d3984b0465b6 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sat, 8 Jan 2022 15:12:09 +0100 Subject: [PATCH 1/2] Fix encoding of large snapshot data * Encode snapshot data asynchronously, fixes #2932 * Async I/O when saving snapshots --- lib/runner.js | 4 ++-- lib/snapshot-manager.js | 16 +++++++++------- lib/worker/base.js | 2 +- test-tap/assert.js | 4 ++-- test-tap/try-snapshot.js | 2 +- test/snapshot-regenerate-report/test.js | 2 +- test/snapshot-tests/fixtures/large/package.json | 1 + test/snapshot-tests/fixtures/large/test.js | 9 +++++++++ test/snapshot-tests/large.js | 15 +++++++++++++++ 9 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 test/snapshot-tests/fixtures/large/package.json create mode 100644 test/snapshot-tests/fixtures/large/test.js create mode 100644 test/snapshot-tests/large.js diff --git a/lib/runner.js b/lib/runner.js index e2773b2e1..11539446e 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -224,8 +224,8 @@ export default class Runner extends Emittery { return this.snapshots.skipSnapshot(options); } - saveSnapshotState() { - return {touchedFiles: this.snapshots.save()}; + async saveSnapshotState() { + return {touchedFiles: await this.snapshots.save()}; } onRun(runnable) { diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index c7a907810..3e3b4b89d 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -182,8 +182,8 @@ function sortBlocks(blocksByTitle, blockIndices) { ); } -function encodeSnapshots(snapshotData) { - const encoded = cbor.encodeOne(snapshotData, { +async function encodeSnapshots(snapshotData) { + const encoded = await cbor.encodeAsync(snapshotData, { omitUndefinedProperties: true, canonical: true, }); @@ -351,7 +351,7 @@ class Manager { this.recordSerialized({belongsTo, index, ...snapshot}); } - save() { + async save() { const {dir, relFile, snapFile, snapPath, reportPath} = this; if (this.updating && this.newBlocksByTitle.size === 0) { @@ -371,15 +371,17 @@ class Manager { ), }; - const buffer = encodeSnapshots(snapshots); + const buffer = await encodeSnapshots(snapshots); const reportBuffer = generateReport(relFile, snapFile, snapshots); - fs.mkdirSync(dir, {recursive: true}); + await fs.promises.mkdir(dir, {recursive: true}); const temporaryFiles = []; const tmpfileCreated = file => temporaryFiles.push(file); - writeFileAtomic.sync(snapPath, buffer, {tmpfileCreated}); - writeFileAtomic.sync(reportPath, reportBuffer, {tmpfileCreated}); + await Promise.all([ + writeFileAtomic(snapPath, buffer, {tmpfileCreated}), + writeFileAtomic(reportPath, reportBuffer, {tmpfileCreated}), + ]); return { changedFiles: [snapPath, reportPath], temporaryFiles, diff --git a/lib/worker/base.js b/lib/worker/base.js index 20e36e33d..e62e30eb4 100644 --- a/lib/worker/base.js +++ b/lib/worker/base.js @@ -81,7 +81,7 @@ const run = async options => { runner.on('finish', async () => { try { - const {touchedFiles} = runner.saveSnapshotState(); + const {touchedFiles} = await runner.saveSnapshotState(); if (touchedFiles) { channel.send({type: 'touched-files', files: touchedFiles}); } diff --git a/test-tap/assert.js b/test-tap/assert.js index edd817c01..fe86cb279 100644 --- a/test-tap/assert.js +++ b/test-tap/assert.js @@ -1303,7 +1303,7 @@ test('.notThrowsAsync() fails if passed a bad value', t => { t.end(); }); -test('.snapshot()', t => { +test('.snapshot()', async t => { // Set to `true` to update the snapshot, then run: // npx tap test-tap/assert.js // @@ -1411,7 +1411,7 @@ test('.snapshot()', t => { }); } - manager.save(); + await manager.save(); t.end(); }); diff --git a/test-tap/try-snapshot.js b/test-tap/try-snapshot.js index 2bd1097ba..ff8830d22 100644 --- a/test-tap/try-snapshot.js +++ b/test-tap/try-snapshot.js @@ -84,5 +84,5 @@ test(async t => { t.equal(result.error.name, 'Error'); }); - manager.save(); + await manager.save(); }); diff --git a/test/snapshot-regenerate-report/test.js b/test/snapshot-regenerate-report/test.js index e2704984a..2d0a4ca39 100644 --- a/test/snapshot-regenerate-report/test.js +++ b/test/snapshot-regenerate-report/test.js @@ -38,7 +38,7 @@ test('snapshot report can be regenerated from .snap file', async t => { // Regenerate report snapshots.hasChanges = true; // Force. - snapshots.save(); + await snapshots.save(); // Assert that reports match t.is(await fs.readFile(reportPath, 'utf8'), report); diff --git a/test/snapshot-tests/fixtures/large/package.json b/test/snapshot-tests/fixtures/large/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-tests/fixtures/large/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-tests/fixtures/large/test.js b/test/snapshot-tests/fixtures/large/test.js new file mode 100644 index 000000000..cc86fec06 --- /dev/null +++ b/test/snapshot-tests/fixtures/large/test.js @@ -0,0 +1,9 @@ +const {Buffer} = require('buffer'); + +const test = require(process.env.TEST_AVA_IMPORT_FROM); + +for (let i = 0; i < 2; i++) { + test(`large snapshot ${i}`, t => { + t.snapshot(Buffer.alloc(1024 * 16)); + }); +} diff --git a/test/snapshot-tests/large.js b/test/snapshot-tests/large.js new file mode 100644 index 000000000..ccdc5dbbb --- /dev/null +++ b/test/snapshot-tests/large.js @@ -0,0 +1,15 @@ +import test from '@ava/test'; + +import {cwd, fixture} from '../helpers/exec.js'; +import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js'; + +// Reproduction for https://github.com/avajs/ava/issues/2932. +test('can encode and decode large snapshots', async t => { + await withTemporaryFixture(cwd('large'), async cwd => { + const env = { + AVA_FORCE_CI: 'not-ci', + }; + await fixture(['--update-snapshots'], {cwd, env}); + await t.notThrowsAsync(fixture([], {cwd, env})); + }); +}); From 6b63b1bc14a1aaa7d4ee4f2201f901624419c677 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sat, 8 Jan 2022 15:14:12 +0100 Subject: [PATCH 2/2] 4.0.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ecbc08c0..5b7e97d4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ava", - "version": "4.0.0", + "version": "4.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ava", - "version": "4.0.0", + "version": "4.0.1", "license": "MIT", "dependencies": { "acorn": "^8.7.0", diff --git a/package.json b/package.json index b16cc95b9..cb2c85406 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ava", - "version": "4.0.0", + "version": "4.0.1", "description": "Node.js test runner that lets you develop with confidence.", "license": "MIT", "repository": "avajs/ava", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy