Repository navigation
fix: filter input-only flags for binding test children #667
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
ktz03
wants to merge
2
commits into
withcatai:master
Choose a base branch
from
ktz03:fix/binding-test-input-type
base: master
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.
+89
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| export function getForkExecArgv(execArgv: readonly string[] = process.execArgv): string[] { | ||
| const result: string[] = []; | ||
|
|
||
| for (let i = 0; i < execArgv.length; i++) { | ||
| const arg = execArgv[i]!; | ||
|
|
||
| // Explicit execArgv also bypasses fork's removal of the parent eval arguments. | ||
| if (arg === "--input-type" || arg === "-e" || arg === "--eval" || arg === "-pe") { | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| if (arg === "-p" || arg === "--print") { | ||
| if (execArgv[i + 1] != null && !execArgv[i + 1]!.startsWith("-")) | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| if (arg.startsWith("--input-type=") || arg.startsWith("--eval=") || arg.startsWith("--print=")) | ||
| continue; | ||
|
|
||
| result.push(arg); | ||
| } | ||
|
|
||
| return result; | ||
| } |
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,61 @@ | ||
| import {fork} from "node:child_process"; | ||
| import {mkdtemp, rm, writeFile} from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import {describe, expect, test} from "vitest"; | ||
| import {getForkExecArgv} from "../../../src/bindings/utils/getForkExecArgv.js"; | ||
|
|
||
|
|
||
| describe("getForkExecArgv", () => { | ||
| test("preserves file-compatible arguments without changing the input", () => { | ||
| const args = ["--trace-warnings", "--conditions", "custom", "--max-old-space-size=256"]; | ||
| expect(getForkExecArgv(args)).to.eql(args); | ||
| expect(args).to.eql(["--trace-warnings", "--conditions", "custom", "--max-old-space-size=256"]); | ||
| }); | ||
|
|
||
| test.each([ | ||
| {args: ["--trace-warnings", "--input-type=module", "-e", "throw new Error('parent eval ran')"], expected: ["--trace-warnings"]}, | ||
| {args: ["--trace-warnings", "--input-type", "module", "--eval", "throw new Error('parent eval ran')"], expected: ["--trace-warnings"]}, | ||
| {args: ["--trace-warnings", "--input-type=commonjs", "--eval=throw new Error('parent eval ran')"], expected: ["--trace-warnings"]}, | ||
| {args: ["--trace-warnings", "--input-type", "commonjs", "-p", "'parent print ran'"], expected: ["--trace-warnings"]}, | ||
| {args: ["--trace-warnings", "--input-type=commonjs", "-pe", "'parent print ran'"], expected: ["--trace-warnings"]}, | ||
| {args: ["--trace-warnings", "--input-type=commonjs", "--print='parent print ran'"], expected: ["--trace-warnings"]}, | ||
| {args: ["-p", "--conditions", "custom", "-e", "'parent eval ran'"], expected: ["--conditions", "custom"]}, | ||
| {args: ["--print", "--trace-warnings", "--eval", "'parent eval ran'"], expected: ["--trace-warnings"]} | ||
| ])("starts a file child with input arguments $args", async ({args, expected}) => { | ||
| const directory = await mkdtemp(path.join(os.tmpdir(), "node-llama-cpp-fork-")); | ||
| const childPath = path.join(directory, "child.mjs"); | ||
| await writeFile(childPath, "process.send({execArgv: process.execArgv}); process.disconnect();\n"); | ||
|
|
||
| try { | ||
| const childArgs = getForkExecArgv(args); | ||
| const result = await new Promise<{execArgv: string[]}>((resolve, reject) => { | ||
| const child = fork(childPath, [], { | ||
| execArgv: childArgs, | ||
| stdio: ["ignore", "ignore", "pipe", "ipc"] | ||
| }); | ||
| let stderr = ""; | ||
| let message: {execArgv: string[]} | undefined; | ||
| const timeout = setTimeout(() => { | ||
| child.kill(); | ||
| reject(new Error("File child did not finish")); | ||
| }, 5000); | ||
|
|
||
| child.stderr?.on("data", (data) => stderr += data); | ||
| child.on("message", (value: {execArgv: string[]}) => message = value); | ||
| child.on("error", reject); | ||
| child.on("exit", (code) => { | ||
| clearTimeout(timeout); | ||
| if (code !== 0 || message == null) | ||
| reject(new Error(`File child failed: ${code}: ${stderr}`)); | ||
| else | ||
| resolve(message); | ||
| }); | ||
| }); | ||
|
|
||
| expect(result.execArgv).to.eql(expected); | ||
| } finally { | ||
| await rm(directory, {recursive: true, force: true}); | ||
| } | ||
| }); | ||
| }); | ||
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.
Please use the
getTempTestFilePathutil:(
test/onTestFinishedcomes from the test callback's arguments)See this snippet for an example:
node-llama-cpp/test/modelDependent/llama3.2/sequenceState.test.ts
Lines 48 to 51 in 10e58c7