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
|
/**
* @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 'fs';
import * as path from 'path';
import * as childProcess from 'child_process';
import { isGitRepository } from '@google/gemini-cli-core';
vi.mock('@google/gemini-cli-core', () => ({
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);
});
afterEach(() => {
process.argv = originalArgv;
});
it('should return UNKNOWN when cliPath is not available', () => {
process.argv[1] = '';
const info = getInstallationInfo(projectRoot, false);
expect(info.packageManager).toBe(PackageManager.UNKNOWN);
});
it('should return UNKNOWN and log error if realpathSync fails', () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
process.argv[1] = '/path/to/cli';
const error = new Error('realpath failed');
mockedRealPathSync.mockImplementation(() => {
throw error;
});
const info = getInstallationInfo(projectRoot, false);
expect(info.packageManager).toBe(PackageManager.UNKNOWN);
expect(consoleSpy).toHaveBeenCalledWith(error);
consoleSpy.mockRestore();
});
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, false);
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, false);
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, false);
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, false);
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',
});
const cliPath = '/usr/local/bin/gemini';
process.argv[1] = cliPath;
mockedRealPathSync.mockReturnValue(cliPath);
mockedExecSync.mockReturnValue(Buffer.from('gemini-cli')); // Simulate successful command
const info = getInstallationInfo(projectRoot, false);
expect(mockedExecSync).toHaveBeenCalledWith(
'brew list -1 | grep -q "^gemini-cli$"',
{ stdio: 'ignore' },
);
expect(info.packageManager).toBe(PackageManager.HOMEBREW);
expect(info.isGlobal).toBe(true);
expect(info.updateMessage).toContain('brew upgrade');
});
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, false);
expect(mockedExecSync).toHaveBeenCalledWith(
'brew list -1 | grep -q "^gemini-cli$"',
{ stdio: 'ignore' },
);
// 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');
});
const info = getInstallationInfo(projectRoot, false);
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');
const infoDisabled = getInstallationInfo(projectRoot, true);
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');
});
const info = getInstallationInfo(projectRoot, false);
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');
const infoDisabled = getInstallationInfo(projectRoot, true);
expect(infoDisabled.updateMessage).toContain('Please run yarn global add');
});
it('should detect global bun installation', () => {
const bunPath = `/Users/test/.bun/bin/gemini`;
process.argv[1] = bunPath;
mockedRealPathSync.mockReturnValue(bunPath);
mockedExecSync.mockImplementation(() => {
throw new Error('Command failed');
});
const info = getInstallationInfo(projectRoot, false);
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');
const infoDisabled = getInstallationInfo(projectRoot, true);
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, false);
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, false);
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, false);
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, false);
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');
});
const info = getInstallationInfo(projectRoot, false);
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');
const infoDisabled = getInstallationInfo(projectRoot, true);
expect(infoDisabled.updateMessage).toContain('Please run npm install');
});
});
|