From 6319ea61838a660fc8e93e29598375f49fe16493 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 27 Nov 2023 15:22:28 +0100 Subject: [PATCH] test: test syncrhnous methods of child_process in snapshot These currently work in snapshot builder scripts. Asynchronous methods are not supported yet. PR-URL: https://github.com/nodejs/node/pull/50943 Refs: https://github.com/nodejs/node/issues/50924 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- test/fixtures/snapshot/child-process-sync.js | 25 +++++++++ .../test-snapshot-child-process-sync.js | 53 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/fixtures/snapshot/child-process-sync.js create mode 100644 test/parallel/test-snapshot-child-process-sync.js diff --git a/test/fixtures/snapshot/child-process-sync.js b/test/fixtures/snapshot/child-process-sync.js new file mode 100644 index 00000000000000..5ffacb05357df6 --- /dev/null +++ b/test/fixtures/snapshot/child-process-sync.js @@ -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(); + }); +} diff --git a/test/parallel/test-snapshot-child-process-sync.js b/test/parallel/test-snapshot-child-process-sync.js new file mode 100644 index 00000000000000..3ec7b2952853ba --- /dev/null +++ b/test/parallel/test-snapshot-child-process-sync.js @@ -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; + } + }); +}