Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/cli.subprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
* and runs `auth whoami` against the mock."
*/

import { execFileSync, spawn } from 'node:child_process';
import { spawn } from 'node:child_process';
import { existsSync, mkdtempSync, rmSync, statSync } from 'node:fs';
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { runNpmScript } from './helpers/npm.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = resolve(__dirname, '..');
Expand All @@ -37,7 +38,7 @@ beforeAll(async () => {
// existsSync skip we used to do here let `dist` rot under
// refactors and gave false-green on `project list` once
// already.
execFileSync('npm', ['run', 'build'], { cwd: REPO_ROOT, stdio: 'pipe' });
runNpmScript('build', REPO_ROOT);
server = createServer((req: IncomingMessage, res: ServerResponse) => {
const url = req.url ?? '/';
if (url.startsWith('/api/cli/v1/projects/')) {
Expand Down
3 changes: 2 additions & 1 deletion test/help.snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { execFileSync } from 'node:child_process';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { beforeAll, describe, expect, it } from 'vitest';
import { runNpmScript } from './helpers/npm.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = resolve(__dirname, '..');
Expand Down Expand Up @@ -47,7 +48,7 @@ const cases: Array<[string, string[]]> = [

describe('--help snapshots', () => {
beforeAll(() => {
execFileSync('npm', ['run', 'build'], { cwd: REPO_ROOT, stdio: 'pipe' });
runNpmScript('build', REPO_ROOT);
});

for (const [name, args] of cases) {
Expand Down
13 changes: 13 additions & 0 deletions test/helpers/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { execFileSync } from 'node:child_process';

export function runNpmScript(script: string, cwd: string): void {
const npmExecPath = process.env.npm_execpath;
if (npmExecPath) {
execFileSync(process.execPath, [npmExecPath, 'run', script], { cwd, stdio: 'pipe' });
return;
}
execFileSync(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['run', script], {
cwd,
stdio: 'pipe',
});
Comment on lines +9 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
node <<'NODE'
const { execFileSync } = require('node:child_process');

if (process.platform === 'win32') {
  execFileSync(process.env.ComSpec || 'cmd.exe', ['/d', '/s', '/c', 'npm.cmd', '--version'], {
    stdio: 'inherit',
  });
}
NODE

Repository: TestSprite/testsprite-cli

Length of output: 163


Use cmd.exe for the Windows fallback

execFileSync('npm.cmd', ...) won’t launch the batch shim directly on Windows. If npm_execpath is unset here, this branch fails; use cmd.exe /d /s /c npm.cmd run <script> instead.

🧰 Tools
🪛 ast-grep (0.44.1)

[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { execFileSync } from 'node:child_process';
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/helpers/npm.ts` around lines 9 - 12, Update the Windows command
selection in the execFileSync invocation to run npm.cmd through cmd.exe with /d
/s /c and preserve the existing npm run script arguments; keep the non-Windows
npm command path unchanged.

Source: MCP tools

}
Loading