-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add spawnTest harness global #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bavulapati
wants to merge
6
commits into
nodejs:main
Choose a base branch
from
bavulapati:feat/spawn-test-global
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d80b42
feat: add spawnTest harness global
bavulapati 63a6db3
refactor: drop nodeFlags from spawnTest
bavulapati 012af85
Merge branch 'main' into feat/spawn-test-global
bavulapati 5d3b3f2
Merge remote-tracking branch 'upstream/main' into feat/spawn-test-global
bavulapati 518b3e2
refactor: make spawnTest result runtime-agnostic
bavulapati 8131d45
fix: use pathToFileURL for harness --import paths
bavulapati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export interface SpawnTestOptions { | ||
| cwd?: string; | ||
| } | ||
|
|
||
| export interface SpawnTestResult { | ||
| status: number | null; | ||
| aborted: boolean; | ||
| stdout: string; | ||
| stderr: string; | ||
| } | ||
|
|
||
| export function spawnTest( | ||
| filePath: string, | ||
| options?: SpawnTestOptions, | ||
| ): SpawnTestResult; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||
| import { spawnSync } from 'node:child_process'; | ||||
| import path from 'node:path'; | ||||
| import { pathToFileURL } from 'node:url'; | ||||
|
|
||||
| const HARNESS_MODULE_PATHS = [ | ||||
| 'features.js', | ||||
| 'assert.js', | ||||
| 'load-addon.js', | ||||
| 'gc.js', | ||||
| 'must-call.js', | ||||
| 'skip-test.js', | ||||
| 'napi-version.js', | ||||
| 'child_process.js', | ||||
| ].map((file) => path.join(import.meta.dirname, file)); | ||||
|
|
||||
| // Exit codes that signify the runtime aborted (rather than exiting cleanly with | ||||
| // a non-zero status). On POSIX an abort surfaces as a fatal signal; on Windows | ||||
| // as one of a small set of exit codes. Mirrors Node.js's | ||||
| // `common.nodeProcessAborted`. The POSIX 128+N codes and the Windows NTSTATUS | ||||
| // codes share one list because the ranges don't overlap. Keeping this here, in | ||||
| // the Node implementor, lets portable tests ask "did it abort?" via the | ||||
| // `aborted` flag without encoding any runtime-specific process semantics. | ||||
| const ABORT_EXIT_CODES = [132, 133, 134, 139, 0xc0000409, 0xc000001d]; | ||||
|
|
||||
| /** | ||||
| * Runs a test file in a fresh Node.js subprocess with the CTS harness globals | ||||
| * pre-loaded, and returns its exit status, whether it aborted, and output. | ||||
| * | ||||
| * @param {string} filePath - Path to the JS/MJS file to execute. Resolved | ||||
| * against `options.cwd` if relative. | ||||
| * @param {{ cwd?: string }} [options] | ||||
| * - `cwd`: working directory for the child; defaults to `process.cwd()`. | ||||
| * @returns {{ status: number | null, aborted: boolean, stdout: string, stderr: string }} | ||||
| */ | ||||
| export const spawnTest = (filePath, options = {}) => { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this share the setups with node-api-cts/implementors/node/tests.ts Line 39 in ee449fa
|
||||
| // --expose-gc is mandatory: gc.js (loaded below) throws at import without it. | ||||
| const args = ['--expose-gc']; | ||||
| for (const modulePath of HARNESS_MODULE_PATHS) { | ||||
| // pathToFileURL handles Windows drive letters and backslashes; a bare | ||||
| // 'file://' + path is malformed there (e.g. file://C:\...). | ||||
| args.push('--import', pathToFileURL(modulePath).href); | ||||
| } | ||||
| args.push(filePath); | ||||
|
|
||||
| const result = spawnSync(process.execPath, args, { | ||||
| cwd: options.cwd ?? process.cwd(), | ||||
| maxBuffer: 100 * 1024 * 1024, | ||||
| }); | ||||
| if (result.error) throw result.error; | ||||
| return { | ||||
| status: result.status, | ||||
| aborted: result.signal !== null || ABORT_EXIT_CODES.includes(result.status), | ||||
| stderr: result.stderr?.toString() ?? '', | ||||
| stdout: result.stdout?.toString() ?? '', | ||||
| }; | ||||
| }; | ||||
|
|
||||
| // This module is loaded in both contexts: imported by the parent test runner | ||||
| // (tests.ts) and `--import`ed into every spawned child. The side effect below | ||||
| // installs `spawnTest` on the child's globalThis so tests can call it directly. | ||||
| Object.assign(globalThis, { spawnTest }); | ||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Spawned by spawn-test.js. Throws an error with a recognizable marker so the | ||
| // parent can assert that stderr was captured and that the non-zero exit status | ||
| // is surfaced. | ||
| throw new Error('spawn-test-fail-marker'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // Spawned by spawn-test.js. Confirms harness globals are injected into the | ||
| // child process by checking `assert` exists, then exits 0. | ||
| if (typeof assert !== 'function') { | ||
| throw new Error('Expected `assert` to be a CTS harness global inside spawned children'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // spawnTest is a function | ||
| if (typeof spawnTest !== 'function') { | ||
| throw new Error('Expected a global spawnTest function'); | ||
| } | ||
|
|
||
| // Successful child: exits 0, stderr empty, and harness globals are available | ||
| // inside the child (the child checks `typeof assert === 'function'` itself). | ||
| { | ||
| const result = spawnTest('spawn-test-ok-child.mjs'); | ||
| assert.strictEqual(result.status, 0, `ok child exited with status ${result.status}; stderr:\n${result.stderr}`); | ||
| assert.strictEqual(result.aborted, false); | ||
| assert.strictEqual(result.stderr, ''); | ||
| } | ||
|
|
||
| // Failing child: non-zero status and stderr contains the thrown marker. | ||
| { | ||
| const result = spawnTest('spawn-test-fail-child.mjs'); | ||
| assert.notStrictEqual(result.status, 0, 'fail child should exit non-zero'); | ||
| // A thrown error is a clean non-zero exit, not an abnormal termination. | ||
| assert.strictEqual(result.aborted, false); | ||
| if (!result.stderr.includes('spawn-test-fail-marker')) { | ||
| throw new Error(`Expected stderr to include the failure marker, got:\n${result.stderr}`); | ||
| } | ||
| } | ||
|
|
||
| // Result shape: all four fields are present. | ||
| { | ||
| const result = spawnTest('spawn-test-ok-child.mjs'); | ||
| for (const key of ['status', 'aborted', 'stdout', 'stderr']) { | ||
| if (!(key in result)) { | ||
| throw new Error(`Expected spawnTest result to have "${key}" field`); | ||
| } | ||
| } | ||
| assert.strictEqual(typeof result.aborted, 'boolean'); | ||
| assert.strictEqual(typeof result.stdout, 'string'); | ||
| assert.strictEqual(typeof result.stderr, 'string'); | ||
| } | ||
|
|
||
| // cwd is forwarded to the child: running from the parent of tests/harness | ||
| // makes the bare child filename unresolvable. The child's stderr must name | ||
| // the specific file Node tried to load, proving cwd actually shifted. | ||
| { | ||
| const result = spawnTest('spawn-test-ok-child.mjs', { cwd: '..' }); | ||
| assert.notStrictEqual(result.status, 0, 'expected cwd ".." to make the child filename unresolvable'); | ||
| if (!result.stderr.includes('spawn-test-ok-child.mjs')) { | ||
| throw new Error(`Expected stderr to reference the unresolved child filename, got:\n${result.stderr}`); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be replaced with https://github.com/nodejs/node-api-cts/blob/main/implementors/node/harness.js.