0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
191 lines
6.8 KiB
TypeScript
191 lines
6.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
import {
|
|
detectInstaller,
|
|
isRunningUnderNpx,
|
|
promptfooCommand,
|
|
} from '../../src/util/promptfooCommand';
|
|
import { mockProcessEnv } from './utils';
|
|
|
|
describe('nextCommand', () => {
|
|
let originalEnv: NodeJS.ProcessEnv;
|
|
|
|
beforeEach(() => {
|
|
originalEnv = { ...process.env };
|
|
mockProcessEnv({ ...originalEnv }, { clear: true });
|
|
});
|
|
|
|
afterEach(() => {
|
|
mockProcessEnv(originalEnv, { clear: true });
|
|
});
|
|
|
|
describe('detectInstaller', () => {
|
|
it('should detect npx from npm_execpath', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(detectInstaller()).toBe('npx');
|
|
});
|
|
|
|
it('should detect npx from npm_lifecycle_script', () => {
|
|
mockProcessEnv({ npm_lifecycle_script: 'npx some-command' });
|
|
expect(detectInstaller()).toBe('npx');
|
|
});
|
|
|
|
it('should detect npx from process.execPath', () => {
|
|
const originalExecPath = process.execPath;
|
|
Object.defineProperty(process, 'execPath', {
|
|
value: '/path/to/npx/node',
|
|
configurable: true,
|
|
});
|
|
|
|
expect(detectInstaller()).toBe('npx');
|
|
|
|
Object.defineProperty(process, 'execPath', {
|
|
value: originalExecPath,
|
|
configurable: true,
|
|
});
|
|
});
|
|
|
|
it('should detect npx from npm_config_user_agent', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'npx/10.5.0 npm/10.8.2 node/v18.20.4' });
|
|
expect(detectInstaller()).toBe('npx');
|
|
});
|
|
|
|
it('should detect brew from npm_config_prefix', () => {
|
|
mockProcessEnv({ npm_config_prefix: '/usr/local/Homebrew/Cellar/node/20.0.0/bin' });
|
|
expect(detectInstaller()).toBe('brew');
|
|
});
|
|
|
|
it('should detect brew from process.execPath', () => {
|
|
const originalExecPath = process.execPath;
|
|
Object.defineProperty(process, 'execPath', {
|
|
value: '/usr/local/Homebrew/bin/node',
|
|
configurable: true,
|
|
});
|
|
|
|
expect(detectInstaller()).toBe('brew');
|
|
|
|
Object.defineProperty(process, 'execPath', {
|
|
value: originalExecPath,
|
|
configurable: true,
|
|
});
|
|
});
|
|
|
|
it('should detect npm-global from npm_config_user_agent', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'npm/10.8.2 node/v18.20.4' });
|
|
expect(detectInstaller()).toBe('npm-global');
|
|
});
|
|
|
|
it('should return unknown for unrecognized patterns', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'yarn/1.22.0' });
|
|
expect(detectInstaller()).toBe('unknown');
|
|
});
|
|
|
|
it('should prioritize npm_execpath over user agent', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
mockProcessEnv({ npm_config_user_agent: 'npm/10.8.2 node/v18.20.4' });
|
|
expect(detectInstaller()).toBe('npx');
|
|
});
|
|
|
|
it('should prioritize npm_lifecycle_script over user agent', () => {
|
|
mockProcessEnv({ npm_lifecycle_script: 'npx some-command' });
|
|
mockProcessEnv({ npm_config_user_agent: 'npm/10.8.2 node/v18.20.4' });
|
|
expect(detectInstaller()).toBe('npx');
|
|
});
|
|
|
|
it('should handle empty environment variables', () => {
|
|
mockProcessEnv({ npm_execpath: undefined });
|
|
mockProcessEnv({ npm_lifecycle_script: undefined });
|
|
mockProcessEnv({ npm_config_prefix: undefined });
|
|
mockProcessEnv({ npm_config_user_agent: undefined });
|
|
expect(detectInstaller()).toBe('unknown');
|
|
});
|
|
|
|
it('should handle case insensitive homebrew detection', () => {
|
|
mockProcessEnv({ npm_config_prefix: '/usr/local/homebrew/cellar/node/20.0.0/bin' });
|
|
expect(detectInstaller()).toBe('brew');
|
|
});
|
|
|
|
it('should detect brew with Windows-style paths', () => {
|
|
mockProcessEnv({ npm_config_prefix: 'C:\\Users\\user\\homebrew\\Cellar\\node\\20.0.0\\bin' });
|
|
expect(detectInstaller()).toBe('brew');
|
|
});
|
|
});
|
|
|
|
describe('promptfooCommand', () => {
|
|
it('should return npx command for npx installer', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(promptfooCommand('eval')).toBe('npx promptfoo@latest eval');
|
|
});
|
|
|
|
it('should return regular command for brew installer', () => {
|
|
mockProcessEnv({ npm_config_prefix: '/usr/local/Homebrew/Cellar/node/20.0.0/bin' });
|
|
expect(promptfooCommand('eval')).toBe('promptfoo eval');
|
|
});
|
|
|
|
it('should return regular command for npm-global installer', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'npm/10.8.2 node/v18.20.4' });
|
|
expect(promptfooCommand('eval')).toBe('promptfoo eval');
|
|
});
|
|
|
|
it('should return regular command for unknown installer', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'yarn/1.22.0' });
|
|
expect(promptfooCommand('eval')).toBe('promptfoo eval');
|
|
});
|
|
|
|
it('should handle complex subcommands', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(promptfooCommand('redteam init')).toBe('npx promptfoo@latest redteam init');
|
|
});
|
|
|
|
it('should handle subcommands with flags', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(promptfooCommand('eval -c config.yaml')).toBe(
|
|
'npx promptfoo@latest eval -c config.yaml',
|
|
);
|
|
});
|
|
|
|
it('should handle empty subcommand for npx', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(promptfooCommand('')).toBe('npx promptfoo@latest');
|
|
});
|
|
|
|
it('should handle empty subcommand for non-npx', () => {
|
|
mockProcessEnv({ npm_config_prefix: '/usr/local/Homebrew/Cellar/node/20.0.0/bin' });
|
|
expect(promptfooCommand('')).toBe('promptfoo');
|
|
});
|
|
});
|
|
|
|
describe('isRunningUnderNpx (legacy compatibility)', () => {
|
|
it('should return true for npx installer', () => {
|
|
mockProcessEnv({ npm_execpath: '/path/to/npx' });
|
|
expect(isRunningUnderNpx()).toBe(true);
|
|
});
|
|
|
|
it('should return false for non-npx installers', () => {
|
|
mockProcessEnv({ npm_config_prefix: '/usr/local/Homebrew/Cellar/node/20.0.0/bin' });
|
|
expect(isRunningUnderNpx()).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('edge cases', () => {
|
|
it('should handle partial matches in strings', () => {
|
|
mockProcessEnv({ npm_execpath: '/some/path/with-npx-in-middle/but-not-npx' });
|
|
expect(detectInstaller()).toBe('npx'); // Should still match because contains 'npx'
|
|
});
|
|
|
|
it('should handle npm_config_user_agent with multiple patterns', () => {
|
|
mockProcessEnv({ npm_config_user_agent: 'npx/10.5.0 npm/10.8.2 node/v18.20.4' });
|
|
expect(detectInstaller()).toBe('npx'); // Should detect npx, not npm
|
|
});
|
|
|
|
it('should handle undefined environment variables gracefully', () => {
|
|
mockProcessEnv({ npm_execpath: undefined });
|
|
mockProcessEnv({ npm_lifecycle_script: undefined });
|
|
mockProcessEnv({ npm_config_prefix: undefined });
|
|
mockProcessEnv({ npm_config_user_agent: undefined });
|
|
|
|
expect(() => detectInstaller()).not.toThrow();
|
|
expect(detectInstaller()).toBe('unknown');
|
|
});
|
|
});
|
|
});
|