From c74649db8e8a1b39afce370e9e7d3f847766a739 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 4 Aug 2023 19:58:32 +0200 Subject: [PATCH] test: use expectSyncExit{WithErrors} in snapshot tests ..and replace the similar code added for logging. --- test/parallel/test-snapshot-api.js | 17 ++--- test/parallel/test-snapshot-basic.js | 42 ++++------- test/parallel/test-snapshot-warning.js | 96 ++++++++++++-------------- 3 files changed, 68 insertions(+), 87 deletions(-) diff --git a/test/parallel/test-snapshot-api.js b/test/parallel/test-snapshot-api.js index b66a2b2a12c9ee..6e3907956d2fa8 100644 --- a/test/parallel/test-snapshot-api.js +++ b/test/parallel/test-snapshot-api.js @@ -7,6 +7,7 @@ const assert = require('assert'); const { spawnSync } = require('child_process'); const tmpdir = require('../common/tmpdir'); const fixtures = require('../common/fixtures'); +const { expectSyncExitWithoutError } = require('../common/child_process'); const path = require('path'); const fs = require('fs'); @@ -37,11 +38,7 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js'); ], { cwd: tmpdir.path }); - if (child.status !== 0) { - console.log(child.stderr.toString()); - console.log(child.stdout.toString()); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child); const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); assert(stats.isFile()); } @@ -59,9 +56,9 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js'); } }); - const stdout = child.stdout.toString().trim(); - const stderr = child.stderr.toString().trim(); - assert.strictEqual(stderr, 'Reading book1.en_US.txt'); - assert.strictEqual(stdout, 'This is book1.en_US.txt'); - assert.strictEqual(child.status, 0); + expectSyncExitWithoutError(child, { + stderr: 'Reading book1.en_US.txt', + stdout: 'This is book1.en_US.txt', + trim: true + }); } diff --git a/test/parallel/test-snapshot-basic.js b/test/parallel/test-snapshot-basic.js index 9bcf3025011315..8beac681692b1c 100644 --- a/test/parallel/test-snapshot-basic.js +++ b/test/parallel/test-snapshot-basic.js @@ -8,6 +8,7 @@ const assert = require('assert'); const { spawnSync } = require('child_process'); const tmpdir = require('../common/tmpdir'); const fixtures = require('../common/fixtures'); +const { expectSyncExitWithoutError, expectSyncExit } = require('../common/child_process'); const path = require('path'); const fs = require('fs'); @@ -16,7 +17,7 @@ tmpdir.refresh(); let snapshotScript = 'node:embedded_snapshot_main'; if (!process.config.variables.node_use_node_snapshot) { // Check that Node.js built without an embedded snapshot - // exits with 1 when node:embedded_snapshot_main is specified + // exits with 9 when node:embedded_snapshot_main is specified // as snapshot entry point. const child = spawnSync(process.execPath, [ '--build-snapshot', @@ -25,10 +26,11 @@ if (!process.config.variables.node_use_node_snapshot) { cwd: tmpdir.path }); - assert.match( - child.stderr.toString(), - /Node\.js was built without embedded snapshot/); - assert.strictEqual(child.status, 9); + expectSyncExit(child, { + status: 9, + signal: null, + stderr: /Node\.js was built without embedded snapshot/ + }); snapshotScript = fixtures.path('empty.js'); } @@ -42,12 +44,7 @@ if (!process.config.variables.node_use_node_snapshot) { ], { cwd: tmpdir.path }); - if (child.status !== 0) { - console.log(child.stderr.toString()); - console.log(child.stdout.toString()); - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child); const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); assert(stats.isFile()); } @@ -64,12 +61,7 @@ const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); ], { cwd: tmpdir.path }); - if (child.status !== 0) { - console.log(child.stderr.toString()); - console.log(child.stdout.toString()); - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child); const stats = fs.statSync(blobPath); assert(stats.isFile()); } @@ -83,13 +75,7 @@ const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); ], { cwd: tmpdir.path }); - - if (child.status !== 0) { - console.log(child.stderr.toString()); - console.log(child.stdout.toString()); - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child); assert(child.stdout.toString().includes('--help')); } @@ -106,7 +92,9 @@ const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); }); // Check that it is a noop. - assert.strictEqual(child.stdout.toString().trim(), ''); - assert.strictEqual(child.stderr.toString().trim(), ''); - assert.strictEqual(child.status, 0); + expectSyncExitWithoutError(child, { + stderr: '', + stdout: '', + trim: true + }); } diff --git a/test/parallel/test-snapshot-warning.js b/test/parallel/test-snapshot-warning.js index 357749ad8e6875..c6df7cd7cbf1ca 100644 --- a/test/parallel/test-snapshot-warning.js +++ b/test/parallel/test-snapshot-warning.js @@ -10,6 +10,7 @@ const assert = require('assert'); const { spawnSync } = require('child_process'); const tmpdir = require('../common/tmpdir'); const fixtures = require('../common/fixtures'); +const { expectSyncExitWithoutError } = require('../common/child_process'); const path = require('path'); const fs = require('fs'); @@ -28,12 +29,7 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child); const stats = fs.statSync(blobPath); assert(stats.isFile()); @@ -44,14 +40,14 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } - const match = child.stderr.toString().match(/Warning: test warning/g); - assert.strictEqual(match.length, 1); + expectSyncExitWithoutError(child, { + stderr(output) { + const match = output.match(/Warning: test warning/g); + assert.strictEqual(match.length, 1); + return true; + } + }); + } tmpdir.refresh(); @@ -66,18 +62,17 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + expectSyncExitWithoutError(child, { + stderr(output) { + let match = output.match(/Warning: test warning/g); + assert.strictEqual(match.length, 1); + match = output.match(/Use `node --trace-warnings/g); + assert.strictEqual(match.length, 1); + return true; + } + }); const stats = fs.statSync(blobPath); assert(stats.isFile()); - let match = child.stderr.toString().match(/Warning: test warning/g); - assert.strictEqual(match.length, 1); - match = child.stderr.toString().match(/Use `node --trace-warnings/g); - assert.strictEqual(match.length, 1); child = spawnSync(process.execPath, [ '--snapshot-blob', @@ -86,17 +81,17 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } - // Warnings should not be handled more than once. - match = child.stderr.toString().match(/Warning: test warning/g); - assert.strictEqual(match.length, 1); - match = child.stderr.toString().match(/Use `node --trace-warnings/g); - assert.strictEqual(match.length, 1); + + expectSyncExitWithoutError(child, { + stderr(output) { + // Warnings should not be handled more than once. + let match = output.match(/Warning: test warning/g); + assert.strictEqual(match.length, 1); + match = output.match(/Use `node --trace-warnings/g); + assert.strictEqual(match.length, 1); + return true; + } + }); } tmpdir.refresh(); @@ -115,25 +110,26 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + + expectSyncExitWithoutError(child, { + stderr(output) { + assert.doesNotMatch(output, /Warning: test warning/); + } + }); + const stats = fs.statSync(blobPath); assert(stats.isFile()); + const warnings1 = fs.readFileSync(warningFile1, 'utf8'); console.log(warningFile1, ':', warnings1); let match = warnings1.match(/Warning: test warning/g); assert.strictEqual(match.length, 1); match = warnings1.match(/Use `node --trace-warnings/g); assert.strictEqual(match.length, 1); - assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/); - fs.rmSync(warningFile1, { maxRetries: 3, recursive: false, force: true }); + child = spawnSync(process.execPath, [ '--snapshot-blob', blobPath, @@ -143,12 +139,13 @@ tmpdir.refresh(); ], { cwd: tmpdir.path }); - console.log('[stderr]:', child.stderr.toString()); - console.log('[stdout]:', child.stdout.toString()); - if (child.status !== 0) { - console.log(child.signal); - assert.strictEqual(child.status, 0); - } + + expectSyncExitWithoutError(child, { + stderr(output) { + assert.doesNotMatch(output, /Warning: test warning/); + return true; + } + }); assert(!fs.existsSync(warningFile1)); const warnings2 = fs.readFileSync(warningFile2, 'utf8'); @@ -157,5 +154,4 @@ tmpdir.refresh(); assert.strictEqual(match.length, 1); match = warnings2.match(/Use `node --trace-warnings/g); assert.strictEqual(match.length, 1); - assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/); }