mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
feat: add GEMINI_CLI environment variable to spawned shell commands (#4791)
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { vi } from 'vitest';
|
||||
import { spawn } from 'child_process';
|
||||
import type { ChildProcessWithoutNullStreams } from 'child_process';
|
||||
import { useShellCommandProcessor } from './shellCommandProcessor';
|
||||
import { Config, GeminiClient } from '@google/gemini-cli-core';
|
||||
import * as fs from 'fs';
|
||||
@@ -39,12 +41,13 @@ describe('useShellCommandProcessor', () => {
|
||||
let configMock: Config;
|
||||
let geminiClientMock: GeminiClient;
|
||||
|
||||
beforeEach(async () => {
|
||||
const { spawn } = await import('child_process');
|
||||
beforeEach(() => {
|
||||
spawnEmitter = new EventEmitter();
|
||||
spawnEmitter.stdout = new EventEmitter();
|
||||
spawnEmitter.stderr = new EventEmitter();
|
||||
(spawn as vi.Mock).mockReturnValue(spawnEmitter);
|
||||
vi.mocked(spawn).mockReturnValue(
|
||||
spawnEmitter as ChildProcessWithoutNullStreams,
|
||||
);
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
|
||||
vi.spyOn(fs, 'readFileSync').mockReturnValue('');
|
||||
@@ -88,6 +91,16 @@ describe('useShellCommandProcessor', () => {
|
||||
result.current.handleShellCommand('ls -l', abortController.signal);
|
||||
});
|
||||
|
||||
expect(spawn).toHaveBeenCalledWith(
|
||||
'bash',
|
||||
['-c', expect.any(String)],
|
||||
expect.objectContaining({
|
||||
env: expect.objectContaining({
|
||||
GEMINI_CLI: '1',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(onExecMock).toHaveBeenCalledTimes(1);
|
||||
const execPromise = onExecMock.mock.calls[0][0];
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@ function executeShellCommand(
|
||||
cwd,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
detached: !isWindows, // Use process groups on non-Windows for robust killing
|
||||
env: {
|
||||
...process.env,
|
||||
GEMINI_CLI: '1',
|
||||
},
|
||||
});
|
||||
|
||||
// Use decoders to handle multi-byte characters safely (for streaming output).
|
||||
|
||||
@@ -514,4 +514,24 @@ describe('ShellTool Bug Reproduction', () => {
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it('should pass GEMINI_CLI environment variable to executed commands', async () => {
|
||||
config = {
|
||||
getCoreTools: () => undefined,
|
||||
getExcludeTools: () => undefined,
|
||||
getDebugMode: () => false,
|
||||
getGeminiClient: () => ({}) as GeminiClient,
|
||||
getTargetDir: () => '.',
|
||||
getSummarizeToolOutputConfig: () => ({}),
|
||||
} as unknown as Config;
|
||||
shellTool = new ShellTool(config);
|
||||
|
||||
const abortSignal = new AbortController().signal;
|
||||
const result = await shellTool.execute(
|
||||
{ command: 'echo "$GEMINI_CLI"' },
|
||||
abortSignal,
|
||||
);
|
||||
|
||||
expect(result.returnDisplay).toBe('1\n');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -322,11 +322,19 @@ Process Group PGID: Process group started or \`(none)\``,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
// detached: true, // ensure subprocess starts its own process group (esp. in Linux)
|
||||
cwd: path.resolve(this.config.getTargetDir(), params.directory || ''),
|
||||
env: {
|
||||
...process.env,
|
||||
GEMINI_CLI: '1',
|
||||
},
|
||||
})
|
||||
: spawn('bash', ['-c', command], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
detached: true, // ensure subprocess starts its own process group (esp. in Linux)
|
||||
cwd: path.resolve(this.config.getTargetDir(), params.directory || ''),
|
||||
env: {
|
||||
...process.env,
|
||||
GEMINI_CLI: '1',
|
||||
},
|
||||
});
|
||||
|
||||
let exited = false;
|
||||
|
||||
Reference in New Issue
Block a user