From 14ca687c056355d210a5c16c093fb85d96516db4 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 21 Aug 2025 17:34:13 -0700 Subject: [PATCH] test(integration-tests): isolate user memory from test runs (#6790) --- integration-tests/globalSetup.ts | 35 +++++++++++++++++++++++++++++++- integration-tests/tsconfig.json | 3 ++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/integration-tests/globalSetup.ts b/integration-tests/globalSetup.ts index 00327a91..46894f88 100644 --- a/integration-tests/globalSetup.ts +++ b/integration-tests/globalSetup.ts @@ -9,16 +9,38 @@ if (process.env.NO_COLOR !== undefined) { delete process.env.NO_COLOR; } -import { mkdir, readdir, rm } from 'fs/promises'; +import { mkdir, readdir, rm, readFile, writeFile, unlink } from 'fs/promises'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; +import * as os from 'os'; + +import { + GEMINI_CONFIG_DIR, + DEFAULT_CONTEXT_FILENAME, +} from '../packages/core/src/tools/memoryTool.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const rootDir = join(__dirname, '..'); const integrationTestsDir = join(rootDir, '.integration-tests'); let runDir = ''; // Make runDir accessible in teardown +const memoryFilePath = join( + os.homedir(), + GEMINI_CONFIG_DIR, + DEFAULT_CONTEXT_FILENAME, +); +let originalMemoryContent: string | null = null; + export async function setup() { + try { + originalMemoryContent = await readFile(memoryFilePath, 'utf-8'); + } catch (e) { + if ((e as NodeJS.ErrnoException).code !== 'ENOENT') { + throw e; + } + // File doesn't exist, which is fine. + } + runDir = join(integrationTestsDir, `${Date.now()}`); await mkdir(runDir, { recursive: true }); @@ -57,4 +79,15 @@ export async function teardown() { if (process.env.KEEP_OUTPUT !== 'true' && runDir) { await rm(runDir, { recursive: true, force: true }); } + + if (originalMemoryContent !== null) { + await mkdir(dirname(memoryFilePath), { recursive: true }); + await writeFile(memoryFilePath, originalMemoryContent, 'utf-8'); + } else { + try { + await unlink(memoryFilePath); + } catch { + // File might not exist if the test failed before creating it. + } + } } diff --git a/integration-tests/tsconfig.json b/integration-tests/tsconfig.json index 3e053d04..295741e1 100644 --- a/integration-tests/tsconfig.json +++ b/integration-tests/tsconfig.json @@ -4,5 +4,6 @@ "noEmit": true, "allowJs": true }, - "include": ["**/*.ts"] + "include": ["**/*.ts"], + "references": [{ "path": "../packages/core" }] }