Skip to content

Commit

Permalink
test: add spawnSyncAndAssert util
Browse files Browse the repository at this point in the history
PR-URL: #52132
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
aduh95 authored Mar 20, 2024
1 parent f69946b commit c714cda
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 72 deletions.
10 changes: 7 additions & 3 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ gathering more information about test failures coming from child processes.
* `stderr` [\<string>][<string>] The output from the child process to stderr.
* `stdout` [\<string>][<string>] The output from the child process to stdout.

### `spawnSyncAndExitWithoutError(command[, args][, spawnOptions], expectations)`
### `spawnSyncAndExitWithoutError(command[, args][, spawnOptions])`

Similar to `expectSyncExit()` with the `status` expected to be 0 and
`signal` expected to be `null`. Any other optional options are passed
into `expectSyncExit()`.
`signal` expected to be `null`.

### `spawnSyncAndAssert(command[, args][, spawnOptions], expectations)`

Similar to `spawnSyncAndExitWithoutError()`, but with an additional
`expectations` parameter.

## Common Module API

Expand Down
13 changes: 10 additions & 3 deletions test/common/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ function spawnSyncAndExit(...args) {
}

function spawnSyncAndExitWithoutError(...args) {
const spawnArgs = args.slice(0, args.length);
const expectations = args[args.length - 1];
const child = spawnSync(...spawnArgs);
return expectSyncExit(spawnSync(...args), {
status: 0,
signal: null,
});
}

function spawnSyncAndAssert(...args) {
const expectations = args.pop();
const child = spawnSync(...args);
return expectSyncExit(child, {
status: 0,
signal: null,
Expand All @@ -134,6 +140,7 @@ module.exports = {
logAfterTime,
kExpiringChildRunTime,
kExpiringParentTimer,
spawnSyncAndAssert,
spawnSyncAndExit,
spawnSyncAndExitWithoutError,
};
10 changes: 5 additions & 5 deletions test/common/sea.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow

if (process.platform === 'darwin') {
try {
spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ], {});
spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ], {});
spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ]);
spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ]);
} catch (e) {
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}`;
if (verifyWorkflow) {
Expand All @@ -104,7 +104,7 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow
console.log(`Signed ${targetExecutable}`);
} else if (process.platform === 'win32') {
try {
spawnSyncAndExitWithoutError('where', [ 'signtool' ], {});
spawnSyncAndExitWithoutError('where', [ 'signtool' ]);
} catch (e) {
const message = `Cannot find signtool: ${inspect(e)}`;
if (verifyWorkflow) {
Expand All @@ -114,8 +114,8 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow
}
let stderr;
try {
({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ], {}));
spawnSyncAndExitWithoutError('signtool', 'verify', '/pa', 'SHA256', targetExecutable, {});
({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ]));
spawnSyncAndExitWithoutError('signtool', ['verify', '/pa', 'SHA256', targetExecutable]);
} catch (e) {
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}\n${stderr}`;
if (verifyWorkflow) {
Expand Down
6 changes: 3 additions & 3 deletions test/common/wasi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Test version set to preview1
'use strict';

const { spawnSyncAndExitWithoutError } = require('./child_process');
const { spawnSyncAndAssert } = require('./child_process');
const fixtures = require('./fixtures');
const childPath = fixtures.path('wasi-preview-1.js');

Expand All @@ -15,7 +15,7 @@ function testWasiPreview1(args, spawnArgs = {}, expectations = {}) {
spawnArgs.env = newEnv;

console.log('Testing with --turbo-fast-api-calls:', ...args);
spawnSyncAndExitWithoutError(
spawnSyncAndAssert(
process.execPath, [
'--turbo-fast-api-calls',
childPath,
Expand All @@ -26,7 +26,7 @@ function testWasiPreview1(args, spawnArgs = {}, expectations = {}) {
);

console.log('Testing with --no-turbo-fast-api-calls:', ...args);
spawnSyncAndExitWithoutError(
spawnSyncAndAssert(
process.execPath,
[
'--no-turbo-fast-api-calls',
Expand Down
16 changes: 7 additions & 9 deletions test/embedding/test-embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const {
spawnSyncAndAssert,
spawnSyncAndExit,
spawnSyncAndExitWithoutError,
} = require('../common/child_process');
Expand All @@ -23,15 +24,15 @@ function resolveBuiltBinary(binary) {

const binary = resolveBuiltBinary('embedtest');

spawnSyncAndExitWithoutError(
spawnSyncAndAssert(
binary,
['console.log(42)'],
{
trim: true,
stdout: '42',
});

spawnSyncAndExitWithoutError(
spawnSyncAndAssert(
binary,
['console.log(embedVars.nön_ascıı)'],
{
Expand Down Expand Up @@ -111,9 +112,8 @@ for (const extraSnapshotArgs of [
spawnSyncAndExitWithoutError(
binary,
[ '--', ...buildSnapshotArgs ],
{ cwd: tmpdir.path },
{});
spawnSyncAndExitWithoutError(
{ cwd: tmpdir.path });
spawnSyncAndAssert(
binary,
[ '--', ...runSnapshotArgs ],
{ cwd: tmpdir.path },
Expand Down Expand Up @@ -145,11 +145,9 @@ for (const extraSnapshotArgs of [
spawnSyncAndExitWithoutError(
binary,
[ '--', ...buildSnapshotArgs ],
{ cwd: tmpdir.path },
{});
{ cwd: tmpdir.path });
spawnSyncAndExitWithoutError(
binary,
[ '--', ...runEmbeddedArgs ],
{ cwd: tmpdir.path },
{});
{ cwd: tmpdir.path });
}
3 changes: 1 addition & 2 deletions test/parallel/test-error-prepare-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ if (process.argv[2] !== 'child') {
// enabled.
spawnSyncAndExitWithoutError(
process.execPath,
['--enable-source-maps', __filename, 'child'],
{});
['--enable-source-maps', __filename, 'child']);
}
4 changes: 2 additions & 2 deletions test/parallel/test-snapshot-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require('../common');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
const { spawnSyncAndAssert, spawnSyncAndExitWithoutError } = require('../common/child_process');
const fs = require('fs');

const v8 = require('v8');
Expand Down Expand Up @@ -41,7 +41,7 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
}

{
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'book1',
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-snapshot-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const {
spawnSyncAndExitWithoutError,
spawnSyncAndAssert,
spawnSyncAndExit,
spawnSyncAndExitWithoutError,
} = require('../common/child_process');
const fs = require('fs');

Expand Down Expand Up @@ -65,7 +66,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');

{
// Check --help.
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--help',
Expand All @@ -78,7 +79,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');

{
// Check -c.
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'-c',
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-snapshot-child-process-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// restoring state from a snapshot

require('../common');
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
const { spawnSyncAndAssert } = require('../common/child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const assert = require('assert');
Expand All @@ -20,7 +20,7 @@ const expected = [

{
// Create the snapshot.
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
Expand All @@ -37,7 +37,7 @@ const expected = [
}

{
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
file,
Expand Down
9 changes: 5 additions & 4 deletions test/parallel/test-snapshot-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
require('../common');
const assert = require('assert');
const {
spawnSyncAndExitWithoutError,
spawnSyncAndAssert,
spawnSyncAndExit,
spawnSyncAndExitWithoutError,
} = require('../common/child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
Expand Down Expand Up @@ -84,7 +85,7 @@ let sizeWithCache;
configPath,
], {
cwd: tmpdir.path
}, {});
});
const stats = fs.statSync(blobPath);
assert(stats.isFile());
sizeWithCache = stats.size;
Expand Down Expand Up @@ -115,14 +116,14 @@ let sizeWithoutCache;
NODE_DEBUG_NATIVE: 'CODE_CACHE'
},
cwd: tmpdir.path
}, {});
});
const stats = fs.statSync(blobPath);
assert(stats.isFile());
sizeWithoutCache = stats.size;
assert(sizeWithoutCache < sizeWithCache,
`sizeWithoutCache = ${sizeWithoutCache} >= sizeWithCache ${sizeWithCache}`);
// Check the snapshot.
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
checkFile,
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-snapshot-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// restoring state from a snapshot

require('../common');
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
const { spawnSyncAndAssert } = require('../common/child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const fs = require('fs');
Expand All @@ -18,7 +18,7 @@ fs.mkdirSync(subdir);

{
// Create the snapshot.
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
Expand All @@ -32,7 +32,7 @@ fs.mkdirSync(subdir);
}

{
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
file,
Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-snapshot-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('../common');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
const { spawnSyncAndAssert, spawnSyncAndExitWithoutError } = require('../common/child_process');
const fs = require('fs');

const warningScript = fixtures.path('snapshot', 'warning.js');
Expand All @@ -30,7 +30,7 @@ tmpdir.refresh();
const stats = fs.statSync(blobPath);
assert(stats.isFile());

spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
Expand All @@ -49,7 +49,7 @@ tmpdir.refresh();
{
console.log('\n# Check snapshot scripts that emit ' +
'warnings and --trace-warnings hint.');
spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
Expand All @@ -68,7 +68,7 @@ tmpdir.refresh();
const stats = fs.statSync(blobPath);
assert(stats.isFile());

spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
Expand All @@ -92,7 +92,7 @@ tmpdir.refresh();
const warningFile1 = tmpdir.resolve('warnings.txt');
const warningFile2 = tmpdir.resolve('warnings2.txt');

spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
Expand Down Expand Up @@ -120,7 +120,7 @@ tmpdir.refresh();
maxRetries: 3, recursive: false, force: true
});

spawnSyncAndExitWithoutError(process.execPath, [
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
...process.env,
},
cwd: tmpdir.path
},
{});
});

assert(existsSync(seaPrepBlob));

Expand All @@ -67,6 +66,5 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
__TEST_PERSON_JPG: fixtures.path('person.jpg'),
}
},
{ }
);
}
6 changes: 3 additions & 3 deletions test/sequential/test-single-executable-application-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const tmpdir = require('../common/tmpdir');

const { copyFileSync, writeFileSync, existsSync } = require('fs');
const {
spawnSyncAndAssert,
spawnSyncAndExit,
spawnSyncAndExitWithoutError,
} = require('../common/child_process');
Expand Down Expand Up @@ -104,14 +105,13 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
...process.env,
},
cwd: tmpdir.path
},
{});
});

assert(existsSync(seaPrepBlob));

generateSEA(outputFile, process.execPath, seaPrepBlob);

spawnSyncAndExitWithoutError(
spawnSyncAndAssert(
outputFile,
{
env: {
Expand Down
Loading

0 comments on commit c714cda

Please sign in to comment.