Skip to content
Draft
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
31 changes: 31 additions & 0 deletions plugins/promptfoo/src/agent/loop-install.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

const execFileSyncMock = vi.fn();

vi.mock('node:child_process', () => ({
execFileSync: execFileSyncMock,
execSync: vi.fn(),
}));

describe('installProviderDependencies', () => {
beforeEach(() => {
execFileSyncMock.mockReset();
});

it('invokes npm without building a shell string', async () => {
const { installProviderDependencies } = await import('./loop.js');

installProviderDependencies('/tmp/provider with spaces');

expect(execFileSyncMock).toHaveBeenCalledWith(
'npm',
['install', '--silent'],
expect.objectContaining({
cwd: '/tmp/provider with spaces',
timeout: 60000,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'pipe'],
})
);
});
});
16 changes: 11 additions & 5 deletions plugins/promptfoo/src/agent/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { LLMProvider, Message, ToolCall, ChatResponse } from './providers.j
import type { DiscoveryResult } from '../types.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { execSync } from 'node:child_process';
import { execFileSync, execSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';

export interface AgentOptions {
Expand Down Expand Up @@ -193,6 +193,15 @@ Steps:
};
}

export function installProviderDependencies(outputDir: string): void {
execFileSync('npm', ['install', '--silent'], {
cwd: outputDir,
timeout: 60000,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'pipe'],
});
}

/**
* Execute a single tool call
*/
Expand Down Expand Up @@ -287,10 +296,7 @@ async function executeTool(
const packageJsonPath = path.join(outputDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
try {
execSync(`cd "${outputDir}" && npm install --silent 2>&1`, {
timeout: 60000,
encoding: 'utf-8',
});
installProviderDependencies(outputDir);
} catch {
// Ignore install errors, will surface in import
}
Expand Down
Loading