diff --git a/integration-tests/ctrl-c-exit.test.ts b/integration-tests/ctrl-c-exit.test.ts index 2e65441ab1..bc89d0459a 100644 --- a/integration-tests/ctrl-c-exit.test.ts +++ b/integration-tests/ctrl-c-exit.test.ts @@ -6,6 +6,8 @@ import { describe, it, expect } from 'vitest'; import { TestRig } from './test-helper.js'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; describe('Ctrl+C exit', () => { // (#9782) Temporarily disabling on windows because it is failing on main and every @@ -27,7 +29,7 @@ describe('Ctrl+C exit', () => { await rig.poll(() => output.includes('▶'), 5000, 100); // Send first Ctrl+C - ptyProcess.write('\x03'); + ptyProcess.write(String.fromCharCode(3)); // Wait for the exit prompt await rig.poll( @@ -37,7 +39,7 @@ describe('Ctrl+C exit', () => { ); // Send second Ctrl+C - ptyProcess.write('\x03'); + ptyProcess.write(String.fromCharCode(3)); const result = await promise; @@ -56,4 +58,72 @@ describe('Ctrl+C exit', () => { expect(cleanOutput).toContain(quittingMessage); }, ); + + it.skipIf(process.platform === 'win32')( + 'should exit gracefully on second Ctrl+C when calling a tool', + async () => { + const rig = new TestRig(); + await rig.setup( + 'should exit gracefully on second Ctrl+C when calling a tool', + ); + + const childProcessFile = 'child_process_file.txt'; + rig.createFile( + 'wait.js', + `setTimeout(() => require('fs').writeFileSync('${childProcessFile}', 'done'), 5000)`, + ); + + const { ptyProcess, promise } = rig.runInteractive(); + + let output = ''; + ptyProcess.onData((data) => { + output += data; + }); + + // Wait for the app to be ready by looking for the initial prompt indicator + await rig.poll(() => output.includes('▶'), 5000, 100); + + ptyProcess.write('use the tool to run "node -e wait.js"\n'); + + await rig.poll(() => output.includes('Shell'), 5000, 100); + + // Send first Ctrl+C + ptyProcess.write(String.fromCharCode(3)); + + // Wait for the exit prompt + await rig.poll( + () => output.includes('Press Ctrl+C again to exit'), + 1500, + 50, + ); + + // Send second Ctrl+C + ptyProcess.write(String.fromCharCode(3)); + + const result = await promise; + + // Expect a graceful exit (code 0) + expect( + result.exitCode, + `Process exited with code ${result.exitCode}. Output: ${result.output}`, + ).toBe(0); + + // Check that the quitting message is displayed + const quittingMessage = 'Agent powering down. Goodbye!'; + // The regex below is intentionally matching the ESC control character (\x1b) + // to strip ANSI color codes from the terminal output. + // eslint-disable-next-line no-control-regex + const cleanOutput = output.replace(/\x1b\[[0-9;]*m/g, ''); + expect(cleanOutput).toContain(quittingMessage); + + // Check that the child process was terminated and did not create the file. + const childProcessFileExists = fs.existsSync( + path.join(rig.testDir!, childProcessFile), + ); + expect( + childProcessFileExists, + 'Child process file should not exist', + ).toBe(false); + }, + ); }); diff --git a/integration-tests/run_shell_command.test.ts b/integration-tests/run_shell_command.test.ts index a1aa08aebf..cba8cb72ad 100644 --- a/integration-tests/run_shell_command.test.ts +++ b/integration-tests/run_shell_command.test.ts @@ -67,4 +67,64 @@ describe('run_shell_command', () => { // Validate model output - will throw if no output, warn if missing expected content validateModelOutput(result, 'test-stdin', 'Shell command stdin test'); }); + + it('should propagate environment variables to the child process', async () => { + const rig = new TestRig(); + await rig.setup('should propagate environment variables'); + + const varName = 'GEMINI_CLI_TEST_VAR'; + const varValue = `test-value-${Math.random().toString(36).substring(7)}`; + process.env[varName] = varValue; + + try { + const prompt = `Use echo to learn the value of the environment variable named ${varName} and tell me what it is.`; + const result = await rig.run(prompt); + + const foundToolCall = await rig.waitForToolCall('run_shell_command'); + + if (!foundToolCall || !result.includes(varValue)) { + printDebugInfo(rig, result, { + 'Found tool call': foundToolCall, + 'Contains varValue': result.includes(varValue), + }); + } + + expect( + foundToolCall, + 'Expected to find a run_shell_command tool call', + ).toBeTruthy(); + validateModelOutput(result, varValue, 'Env var propagation test'); + expect(result).toContain(varValue); + } finally { + delete process.env[varName]; + } + }); + + it('should run a platform-specific file listing command', async () => { + const rig = new TestRig(); + await rig.setup('should run platform-specific file listing'); + const fileName = `test-file-${Math.random().toString(36).substring(7)}.txt`; + rig.createFile(fileName, 'test content'); + + const prompt = `Run a shell command to list the files in the current directory and tell me what they are.`; + const result = await rig.run(prompt); + + const foundToolCall = await rig.waitForToolCall('run_shell_command'); + + // Debugging info + if (!foundToolCall || !result.includes(fileName)) { + printDebugInfo(rig, result, { + 'Found tool call': foundToolCall, + 'Contains fileName': result.includes(fileName), + }); + } + + expect( + foundToolCall, + 'Expected to find a run_shell_command tool call', + ).toBeTruthy(); + + validateModelOutput(result, fileName, 'Platform-specific listing test'); + expect(result).toContain(fileName); + }); }); diff --git a/integration-tests/shell-service.test.ts b/integration-tests/shell-service.test.ts deleted file mode 100644 index b4c1ada0c5..0000000000 --- a/integration-tests/shell-service.test.ts +++ /dev/null @@ -1,156 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import { describe, it, expect, beforeAll } from 'vitest'; -import { ShellExecutionService } from '../packages/core/src/services/shellExecutionService.js'; -import * as fs from 'node:fs/promises'; -import * as path from 'node:path'; -import { vi } from 'vitest'; - -describe('ShellExecutionService programmatic integration tests', () => { - let testDir: string; - - beforeAll(async () => { - // Create a dedicated directory for this test suite to avoid conflicts. - testDir = path.join( - process.env['INTEGRATION_TEST_FILE_DIR']!, - 'shell-service-tests', - ); - await fs.mkdir(testDir, { recursive: true }); - }); - - it('should execute a simple cross-platform command (echo)', async () => { - const command = 'echo "hello from the service"'; - const onOutputEvent = vi.fn(); - const abortController = new AbortController(); - - const handle = await ShellExecutionService.execute( - command, - testDir, - onOutputEvent, - abortController.signal, - false, - ); - - const result = await handle.result; - - expect(result.error).toBeNull(); - expect(result.exitCode).toBe(0); - // Output can vary slightly between shells (e.g., quotes), so check for inclusion. - expect(result.output).toContain('hello from the service'); - }); - - it.runIf(process.platform === 'win32')( - 'should execute "dir" on Windows', - async () => { - const testFile = 'test-file-windows.txt'; - await fs.writeFile(path.join(testDir, testFile), 'windows test'); - - const command = 'dir'; - const onOutputEvent = vi.fn(); - const abortController = new AbortController(); - - const handle = await ShellExecutionService.execute( - command, - testDir, - onOutputEvent, - abortController.signal, - false, - ); - - const result = await handle.result; - - expect(result.error).toBeNull(); - expect(result.exitCode).toBe(0); - expect(result.output).toContain(testFile); - }, - ); - - it.skipIf(process.platform === 'win32')( - 'should execute "ls -l" on Unix', - async () => { - const testFile = 'test-file-unix.txt'; - await fs.writeFile(path.join(testDir, testFile), 'unix test'); - - const command = 'ls -l'; - const onOutputEvent = vi.fn(); - const abortController = new AbortController(); - - const handle = await ShellExecutionService.execute( - command, - testDir, - onOutputEvent, - abortController.signal, - false, - ); - - const result = await handle.result; - - expect(result.error).toBeNull(); - expect(result.exitCode).toBe(0); - expect(result.output).toContain(testFile); - }, - ); - - it('should abort a running process', async () => { - // A command that runs for a bit. - const command = 'node -e "setTimeout(() => {}, 20000)"'; - const onOutputEvent = vi.fn(); - const abortController = new AbortController(); - - const handle = await ShellExecutionService.execute( - command, - testDir, - onOutputEvent, - abortController.signal, - false, - ); - - // Abort shortly after starting - setTimeout(() => abortController.abort(), 50); - - const result = await handle.result; - - // For debugging the flaky test. - console.log('Abort test result:', result); - - expect(result.aborted).toBe(true); - // A clean exit is exitCode 0 and no signal. If the process was truly - // aborted, it should not have exited cleanly. - const exitedCleanly = result.exitCode === 0 && result.signal === null; - expect(exitedCleanly, 'Process should not have exited cleanly').toBe(false); - }); - - it('should propagate environment variables to the child process', async () => { - const varName = 'GEMINI_CLI_TEST_VAR'; - const varValue = `test-value`; - process.env[varName] = varValue; - - try { - const command = - process.platform === 'win32' ? `echo %${varName}%` : `echo $${varName}`; - const onOutputEvent = vi.fn(); - const abortController = new AbortController(); - - const handle = await ShellExecutionService.execute( - command, - testDir, - onOutputEvent, - abortController.signal, - false, - ); - - const result = await handle.result; - - expect(result.error).toBeNull(); - expect(result.exitCode).toBe(0); - expect(result.output).toContain(varValue); - } finally { - // Clean up the env var to prevent side-effects on other tests. - delete process.env[varName]; - } - }); -}); diff --git a/integration-tests/test-helper.ts b/integration-tests/test-helper.ts index 149aadca20..a894be28b1 100644 --- a/integration-tests/test-helper.ts +++ b/integration-tests/test-helper.ts @@ -213,6 +213,7 @@ export class TestRig { const child = spawn('node', commandArgs, { cwd: this.testDir!, stdio: 'pipe', + env: process.env, }); let stdout = ''; diff --git a/packages/cli/src/utils/sandbox.ts b/packages/cli/src/utils/sandbox.ts index a8ad57d7ca..93a3ee4cc1 100644 --- a/packages/cli/src/utils/sandbox.ts +++ b/packages/cli/src/utils/sandbox.ts @@ -584,6 +584,14 @@ export async function start_sandbox( } args.push('--name', containerName, '--hostname', containerName); + // copy GEMINI_CLI_TEST_VAR for integration tests + if (process.env['GEMINI_CLI_TEST_VAR']) { + args.push( + '--env', + `GEMINI_CLI_TEST_VAR=${process.env['GEMINI_CLI_TEST_VAR']}`, + ); + } + // copy GEMINI_API_KEY(s) if (process.env['GEMINI_API_KEY']) { args.push('--env', `GEMINI_API_KEY=${process.env['GEMINI_API_KEY']}`);