Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

test(sdk): reuse existing subprocess fixtures (#1476)

mldangelo-oai22e3751b9410
diff --git a/sdk/typescript/tests-ts/api-preflight-config.test.ts b/sdk/typescript/tests-ts/api-preflight-config.test.ts
index 0f64544c5..54f687c99 100644
--- a/sdk/typescript/tests-ts/api-preflight-config.test.ts
+++ b/sdk/typescript/tests-ts/api-preflight-config.test.ts
@@ -1,5 +1,6 @@
import { pythonExecutable } from "./support/python.js";
-import { execFileSync, spawnSync } from "node:child_process";
+import { execFileSync } from "node:child_process";
+import { runNodePython } from "./support/python-probe.js";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -48,20 +49,14 @@ function runPreflight(
): { status: number | null; payload: Record<string, unknown> } {
const interpreter = pythonExecutable(false);
expect(interpreter).not.toBeNull();
- const result = spawnSync(
- interpreter!,
- [
- "-I",
- "-B",
- join(PLUGIN_ROOT, "scripts", "config_preflight.py"),
- "--profile",
- profile,
- "--config",
- config,
- ...options,
- ],
- { encoding: "utf8" },
- );
+ const result = runNodePython(interpreter!, [
+ join(PLUGIN_ROOT, "scripts", "config_preflight.py"),
+ "--profile",
+ profile,
+ "--config",
+ config,
+ ...options,
+ ]);
expect(result.error).toBeUndefined();
return {
status: result.status,
@@ -174,21 +169,16 @@ describe("CodexSecurity preflight configuration", () => {
const interpreter = pythonExecutable();
expect(interpreter).not.toBeNull();
- const result = spawnSync(
+ const result = runNodePython(
interpreter!,
[
- "-I",
- "-B",
join(PLUGIN_ROOT, "scripts", "config_preflight.py"),
"--profile",
"security_scan",
"--cwd",
repository,
],
- {
- encoding: "utf8",
- env: { PATH: process.env["PATH"], CODEX_HOME: codexHome },
- },
+ { env: { PATH: process.env["PATH"], CODEX_HOME: codexHome } },
);
expect(result.error).toBeUndefined();
const payload = JSON.parse(result.stdout) as Record<string, unknown>;
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index 21fd5f44f..573bf6e35 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -1,5 +1,5 @@
import { jsonLines } from "./support/json.js";
-import { spawnSync } from "node:child_process";
+import { runNodePython } from "./support/python-probe.js";
import * as filesystem from "node:fs/promises";
import { appendFile, writeFile } from "node:fs/promises";
import { join, parse, sep } from "node:path";
@@ -220,18 +220,12 @@ describe("scan cost", () => {
"payload = {'info': {'total_token_usage': json.loads(sys.argv[2])}}",
"print(json.dumps(workbench_scan_usage._token_snapshot(payload)))",
].join("\n");
- const result = spawnSync(
- python!,
- [
- "-I",
- "-B",
- "-c",
- probe,
- join(PLUGIN_ROOT, "scripts"),
- JSON.stringify(usage),
- ],
- { encoding: "utf8" },
- );
+ const result = runNodePython(python!, [
+ "-c",
+ probe,
+ join(PLUGIN_ROOT, "scripts"),
+ JSON.stringify(usage),
+ ]);
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(result.stdout)).toMatchObject({
diff --git a/sdk/typescript/tests-ts/diff-rank-input.test.ts b/sdk/typescript/tests-ts/diff-rank-input.test.ts
index c0fe64a59..992e0c05a 100644
--- a/sdk/typescript/tests-ts/diff-rank-input.test.ts
+++ b/sdk/typescript/tests-ts/diff-rank-input.test.ts
@@ -1,3 +1,4 @@
+import { runNodePython } from "./support/python-probe.js";
import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js";
import { pythonExecutable } from "./support/python.js";
import { git } from "./git-fixture.js";
@@ -136,24 +137,18 @@ test("preserves Unicode Git paths and legacy-encoded commit metadata", () => {
const python = pythonExecutable();
expect(python).not.toBeNull();
const output = join(root, "rank-input.jsonl");
- const rank = spawnSync(
- python!,
- [
- "-I",
- "-B",
- join(PLUGIN_ROOT, "scripts", "generate_rank_input.py"),
- "make-diff-rank-input",
- "--repo",
- repository,
- "--base",
- base,
- "--head",
- head,
- "--out",
- output,
- ],
- { encoding: "utf8" },
- );
+ const rank = runNodePython(python!, [
+ join(PLUGIN_ROOT, "scripts", "generate_rank_input.py"),
+ "make-diff-rank-input",
+ "--repo",
+ repository,
+ "--base",
+ base,
+ "--head",
+ head,
+ "--out",
+ output,
+ ]);
const probeSource = [
"import json, pathlib, sys",
"sys.path.insert(0, sys.argv[1])",
@@ -165,19 +160,13 @@ test("preserves Unicode Git paths and legacy-encoded commit metadata", () => {
"diff = db.require_diff_target(repo, 'commit', None, sys.argv[3], None)",
"print(json.dumps({'root': str(root), 'pathspec': pathspec, 'subject': metadata['commitSubject'], 'diff': diff}))",
].join("\n");
- const probe = spawnSync(
- python!,
- [
- "-I",
- "-B",
- "-c",
- probeSource,
- join(PLUGIN_ROOT, "scripts"),
- repository,
- legacyHead,
- ],
- { encoding: "utf8" },
- );
+ const probe = runNodePython(python!, [
+ "-c",
+ probeSource,
+ join(PLUGIN_ROOT, "scripts"),
+ repository,
+ legacyHead,
+ ]);
expect(rank.status, `${rank.stderr}\n${String(rank.error ?? "")}`).toBe(0);
expect(
@@ -240,11 +229,9 @@ testPosix(
);
const output = join(root, "output");
const run = (script: string, args: string[], binding = trustedGit) =>
- spawnSync(
+ runNodePython(
python!,
[
- "-I",
- "-B",
join(PLUGIN_ROOT, "scripts", script),
...args,
"--repo",
@@ -253,7 +240,6 @@ testPosix(
output,
],
{
- encoding: "utf8",
env: {
...process.env,
PATH: `${externalBin}:${process.env["PATH"] ?? ""}`,
diff --git a/sdk/typescript/tests-ts/sarif.test.ts b/sdk/typescript/tests-ts/sarif.test.ts
index b98676f40..4e56745a0 100644
--- a/sdk/typescript/tests-ts/sarif.test.ts
+++ b/sdk/typescript/tests-ts/sarif.test.ts
@@ -1,4 +1,4 @@
-import { spawnSync } from "node:child_process";
+import { runNodePython } from "./support/python-probe.js";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, test } from "bun:test";
@@ -20,11 +20,9 @@ function finding(overrides: Partial<Finding> = {}): Finding {
function buildSarif(findings: Finding[]) {
const python = Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
- const result = spawnSync(
+ const result = runNodePython(
python!,
[
- "-I",
- "-B",
"-c",
[
"import json, sys",
@@ -36,7 +34,6 @@ function buildSarif(findings: Finding[]) {
join(PLUGIN_ROOT, "scripts"),
],
{
- encoding: "utf8",
input: JSON.stringify({ manifest, findings: { ...document, findings } }),
},
);
diff --git a/sdk/typescript/tests-ts/support/usage-rollout.ts b/sdk/typescript/tests-ts/support/usage-rollout.ts
index 4e5342447..68fcf63c7 100644
--- a/sdk/typescript/tests-ts/support/usage-rollout.ts
+++ b/sdk/typescript/tests-ts/support/usage-rollout.ts
@@ -1,5 +1,5 @@
import { mkdir, writeFile } from "node:fs/promises";
-import { spawnSync } from "node:child_process";
+import { runNodePython } from "./python-probe.js";
import { join } from "node:path";
import { expect } from "bun:test";
@@ -107,20 +107,14 @@ export function readPythonRolloutUsage(
")",
"print(json.dumps({'usage': usage, 'warnings': sorted(warnings)}, sort_keys=True))",
].join("\n");
- const result = spawnSync(
- python!,
- [
- "-I",
- "-B",
- "-c",
- probe,
- join(pluginRoot, "scripts"),
- rolloutPath,
- childUuid7Thread,
- scanThreadId,
- ],
- { encoding: "utf8" },
- );
+ const result = runNodePython(python!, [
+ "-c",
+ probe,
+ join(pluginRoot, "scripts"),
+ rolloutPath,
+ childUuid7Thread,
+ scanThreadId,
+ ]);
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(0);