Skip to content

Commit

Permalink
test: test syncrhnous methods of child_process in snapshot
Browse files Browse the repository at this point in the history
These currently work in snapshot builder scripts. Asynchronous
methods are not supported yet.

PR-URL: #50943
Refs: #50924
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Dec 14, 2023
1 parent 4d49aff commit 5fd5c1e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/fixtures/snapshot/child-process-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const {
setDeserializeMainFunction,
isBuildingSnapshot
} = require('v8').startupSnapshot;

function spawn() {
const { spawnSync, execFileSync, execSync } = require('child_process');
spawnSync(process.execPath, [ __filename, 'spawnSync' ], { stdio: 'inherit' });
execSync(`"${process.execPath}" "${__filename}" "execSync"`, { stdio: 'inherit' });
execFileSync(process.execPath, [ __filename, 'execFileSync' ], { stdio: 'inherit' });
}

if (process.argv[2] !== undefined) {
console.log('From child process', process.argv[2]);
} else {
spawn();
}

if (isBuildingSnapshot()) {
setDeserializeMainFunction(() => {
spawn();
});
}
53 changes: 53 additions & 0 deletions test/parallel/test-snapshot-child-process-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

// This tests that process.cwd() is accurate when
// restoring state from a snapshot

require('../common');
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const assert = require('assert');

tmpdir.refresh();
const blobPath = tmpdir.resolve('snapshot.blob');
const file = fixtures.path('snapshot', 'child-process-sync.js');
const expected = [
'From child process spawnSync',
'From child process execSync',
'From child process execFileSync',
];

{
// Create the snapshot.
spawnSyncAndExitWithoutError(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
file,
], {
cwd: tmpdir.path,
}, {
trim: true,
stdout(output) {
assert.deepStrictEqual(output.split('\n'), expected);
return true;
}
});
}

{
spawnSyncAndExitWithoutError(process.execPath, [
'--snapshot-blob',
blobPath,
file,
], {
cwd: tmpdir.path,
}, {
trim: true,
stdout(output) {
assert.deepStrictEqual(output.split('\n'), expected);
return true;
}
});
}

0 comments on commit 5fd5c1e

Please sign in to comment.