File size: 15,034 Bytes
84aa3bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { getInstallationInfo, PackageManager } from './installationInfo.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as childProcess from 'node:child_process';
import { isGitRepository, debugLogger } from '@google/gemini-cli-core';
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
isGitRepository: vi.fn(),
};
});
vi.mock('fs', async (importOriginal) => {
const actualFs = await importOriginal<typeof fs>();
return {
...actualFs,
realpathSync: vi.fn(),
existsSync: vi.fn(),
};
});
vi.mock('child_process', async (importOriginal) => {
const actual = await importOriginal<typeof import('child_process')>();
return {
...actual,
execSync: vi.fn(),
};
});
const mockedIsGitRepository = vi.mocked(isGitRepository);
const mockedRealPathSync = vi.mocked(fs.realpathSync);
const mockedExistsSync = vi.mocked(fs.existsSync);
const mockedExecSync = vi.mocked(childProcess.execSync);
describe('getInstallationInfo', () => {
const projectRoot = '/path/to/project';
let originalArgv: string[];
beforeEach(() => {
vi.resetAllMocks();
originalArgv = [...process.argv];
// Mock process.cwd() for isGitRepository
vi.spyOn(process, 'cwd').mockReturnValue(projectRoot);
vi.spyOn(debugLogger, 'log').mockImplementation(() => {});
});
afterEach(() => {
process.argv = originalArgv;
});
it('should detect running as a standalone binary', () => {
vi.stubEnv('IS_BINARY', 'true');
process.argv[1] = '/path/to/binary';
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.BINARY);
expect(info.isGlobal).toBe(true);
expect(info.updateMessage).toBe(
'Running as a standalone binary. Please update by downloading the latest version from GitHub.',
);
expect(info.updateCommand).toBeUndefined();
vi.unstubAllEnvs();
});
it('should return UNKNOWN when cliPath is not available', () => {
process.argv[1] = '';
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.UNKNOWN);
});
it('should return UNKNOWN and log error if realpathSync fails', () => {
process.argv[1] = '/path/to/cli';
const error = new Error('realpath failed');
mockedRealPathSync.mockImplementation(() => {
throw error;
});
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.UNKNOWN);
expect(debugLogger.log).toHaveBeenCalledWith(error);
});
it('should detect running from a local git clone', () => {
process.argv[1] = `${projectRoot}/packages/cli/dist/index.js`;
mockedRealPathSync.mockReturnValue(
`${projectRoot}/packages/cli/dist/index.js`,
);
mockedIsGitRepository.mockReturnValue(true);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.UNKNOWN);
expect(info.isGlobal).toBe(false);
expect(info.updateMessage).toBe(
'Running from a local git clone. Please update with "git pull".',
);
});
it('should detect running via npx', () => {
const npxPath = `/Users/test/.npm/_npx/12345/bin/gemini`;
process.argv[1] = npxPath;
mockedRealPathSync.mockReturnValue(npxPath);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.NPX);
expect(info.isGlobal).toBe(false);
expect(info.updateMessage).toBe('Running via npx, update not applicable.');
});
it('should detect running via pnpx', () => {
const pnpxPath = `/Users/test/.pnpm/_pnpx/12345/bin/gemini`;
process.argv[1] = pnpxPath;
mockedRealPathSync.mockReturnValue(pnpxPath);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.PNPX);
expect(info.isGlobal).toBe(false);
expect(info.updateMessage).toBe('Running via pnpx, update not applicable.');
});
it('should detect running via bunx', () => {
const bunxPath = `/Users/test/.bun/install/cache/12345/bin/gemini`;
process.argv[1] = bunxPath;
mockedRealPathSync.mockReturnValue(bunxPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.BUNX);
expect(info.isGlobal).toBe(false);
expect(info.updateMessage).toBe('Running via bunx, update not applicable.');
});
it('should detect Homebrew installation via execSync', () => {
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
// Use a path that matches what brew would resolve to
const cliPath = '/opt/homebrew/Cellar/gemini-cli/1.0.0/bin/gemini';
process.argv[1] = cliPath;
mockedExecSync.mockImplementation((cmd) => {
if (typeof cmd === 'string' && cmd.includes('brew --prefix gemini-cli')) {
return '/opt/homebrew/opt/gemini-cli';
}
throw new Error(`Command failed: ${cmd}`);
});
mockedRealPathSync.mockImplementation((p) => {
if (p === cliPath) return cliPath;
if (p === '/opt/homebrew/opt/gemini-cli') {
return '/opt/homebrew/Cellar/gemini-cli/1.0.0';
}
return String(p);
});
const info = getInstallationInfo(projectRoot, true);
expect(mockedExecSync).toHaveBeenCalledWith(
expect.stringContaining('brew --prefix gemini-cli'),
expect.anything(),
);
expect(info.packageManager).toBe(PackageManager.HOMEBREW);
expect(info.isGlobal).toBe(true);
expect(info.updateMessage).toBe(
'Installed via Homebrew. Please update with "brew upgrade gemini-cli".',
);
});
it('should fall through if brew command fails', () => {
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
const cliPath = '/usr/local/bin/gemini';
process.argv[1] = cliPath;
mockedRealPathSync.mockReturnValue(cliPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
const info = getInstallationInfo(projectRoot, true);
expect(mockedExecSync).toHaveBeenCalledWith(
expect.stringContaining('brew --prefix gemini-cli'),
expect.anything(),
);
// Should fall back to default global npm
expect(info.packageManager).toBe(PackageManager.NPM);
expect(info.isGlobal).toBe(true);
});
it('should detect global pnpm installation', () => {
const pnpmPath = `/Users/test/.pnpm/global/5/node_modules/.pnpm/some-hash/node_modules/@google/gemini-cli/dist/index.js`;
process.argv[1] = pnpmPath;
mockedRealPathSync.mockReturnValue(pnpmPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
// isAutoUpdateEnabled = true -> "Attempting to automatically update"
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.PNPM);
expect(info.isGlobal).toBe(true);
expect(info.updateCommand).toBe('pnpm add -g @google/gemini-cli@latest');
expect(info.updateMessage).toContain('Attempting to automatically update');
// isAutoUpdateEnabled = false -> "Please run..."
const infoDisabled = getInstallationInfo(projectRoot, false);
expect(infoDisabled.updateMessage).toContain('Please run pnpm add');
});
it('should detect global yarn installation', () => {
const yarnPath = `/Users/test/.yarn/global/node_modules/@google/gemini-cli/dist/index.js`;
process.argv[1] = yarnPath;
mockedRealPathSync.mockReturnValue(yarnPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
// isAutoUpdateEnabled = true -> "Attempting to automatically update"
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.YARN);
expect(info.isGlobal).toBe(true);
expect(info.updateCommand).toBe(
'yarn global add @google/gemini-cli@latest',
);
expect(info.updateMessage).toContain('Attempting to automatically update');
// isAutoUpdateEnabled = false -> "Please run..."
const infoDisabled = getInstallationInfo(projectRoot, false);
expect(infoDisabled.updateMessage).toContain('Please run yarn global add');
});
it('should detect global bun installation', () => {
const bunPath = `/Users/test/.bun/install/global/node_modules/@google/gemini-cli/dist/index.js`;
process.argv[1] = bunPath;
mockedRealPathSync.mockReturnValue(bunPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
// isAutoUpdateEnabled = true -> "Attempting to automatically update"
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.BUN);
expect(info.isGlobal).toBe(true);
expect(info.updateCommand).toBe('bun add -g @google/gemini-cli@latest');
expect(info.updateMessage).toContain('Attempting to automatically update');
// isAutoUpdateEnabled = false -> "Please run..."
const infoDisabled = getInstallationInfo(projectRoot, false);
expect(infoDisabled.updateMessage).toContain('Please run bun add');
});
it('should detect local installation and identify yarn from lockfile', () => {
const localPath = `${projectRoot}/node_modules/.bin/gemini`;
process.argv[1] = localPath;
mockedRealPathSync.mockReturnValue(localPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
mockedExistsSync.mockImplementation(
(p) => p === path.join(projectRoot, 'yarn.lock'),
);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.YARN);
expect(info.isGlobal).toBe(false);
expect(info.updateMessage).toContain('Locally installed');
});
it('should detect local installation and identify pnpm from lockfile', () => {
const localPath = `${projectRoot}/node_modules/.bin/gemini`;
process.argv[1] = localPath;
mockedRealPathSync.mockReturnValue(localPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
mockedExistsSync.mockImplementation(
(p) => p === path.join(projectRoot, 'pnpm-lock.yaml'),
);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.PNPM);
expect(info.isGlobal).toBe(false);
});
it('should detect local installation and identify bun from lockfile', () => {
const localPath = `${projectRoot}/node_modules/.bin/gemini`;
process.argv[1] = localPath;
mockedRealPathSync.mockReturnValue(localPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
mockedExistsSync.mockImplementation(
(p) => p === path.join(projectRoot, 'bun.lockb'),
);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.BUN);
expect(info.isGlobal).toBe(false);
});
it('should default to local npm installation if no lockfile is found', () => {
const localPath = `${projectRoot}/node_modules/.bin/gemini`;
process.argv[1] = localPath;
mockedRealPathSync.mockReturnValue(localPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
mockedExistsSync.mockReturnValue(false); // No lockfiles
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.NPM);
expect(info.isGlobal).toBe(false);
});
it('should default to global npm installation for unrecognized paths', () => {
const globalPath = `/usr/local/bin/gemini`;
process.argv[1] = globalPath;
mockedRealPathSync.mockReturnValue(globalPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
// isAutoUpdateEnabled = true -> "Attempting to automatically update"
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.NPM);
expect(info.isGlobal).toBe(true);
expect(info.updateCommand).toBe('npm install -g @google/gemini-cli@latest');
expect(info.updateMessage).toContain('Attempting to automatically update');
// isAutoUpdateEnabled = false -> "Please run..."
const infoDisabled = getInstallationInfo(projectRoot, false);
expect(infoDisabled.updateMessage).toContain('Please run npm install');
});
it('should detect Volta installation (Unix-style)', () => {
const voltaPath =
'/Users/test/.volta/tools/image/node/20.0.0/lib/node_modules/@google/gemini-cli/dist/index.js';
process.argv[1] = voltaPath;
mockedRealPathSync.mockReturnValue(voltaPath);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.VOLTA);
expect(info.updateCommand).toBe('volta install @google/gemini-cli@latest');
});
it('should detect Volta installation (Windows-style)', () => {
const voltaPath =
'C:\\Users\\test\\AppData\\Local\\Volta\\tools\\image\\node\\20.0.0\\node_modules\\@google/gemini-cli\\dist\\index.js';
process.argv[1] = voltaPath;
mockedRealPathSync.mockReturnValue(voltaPath);
const info = getInstallationInfo(projectRoot, true);
expect(info.packageManager).toBe(PackageManager.VOLTA);
expect(info.updateCommand).toBe('volta install @google/gemini-cli@latest');
});
it('should NOT detect Homebrew if gemini-cli is installed in brew but running from npm location', () => {
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
// Path looks like standard global NPM
const cliPath =
'/usr/local/lib/node_modules/@google/gemini-cli/dist/index.js';
process.argv[1] = cliPath;
// Setup mocks
mockedExecSync.mockImplementation((cmd) => {
if (typeof cmd === 'string' && cmd.includes('brew list')) {
return Buffer.from('gemini-cli\n');
}
// Future proofing for the fix:
if (typeof cmd === 'string' && cmd.includes('brew --prefix gemini-cli')) {
return '/opt/homebrew/opt/gemini-cli';
}
throw new Error(`Command failed: ${cmd}`);
});
mockedRealPathSync.mockImplementation((p) => {
if (p === cliPath) return cliPath;
// Future proofing for the fix:
if (p === '/opt/homebrew/opt/gemini-cli')
return '/opt/homebrew/Cellar/gemini-cli/1.0.0';
return String(p);
});
const info = getInstallationInfo(projectRoot, false);
expect(info.packageManager).not.toBe(PackageManager.HOMEBREW);
expect(info.packageManager).toBe(PackageManager.NPM);
});
});
|