Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions packages/cli/src/utils/userStartupWarnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ vi.mock('os', async (importOriginal) => {

vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
await importOriginal<typeof import('@google-gemini-cli-core')>();
return {
...actual,
homedir: () => os.homedir(),
getCompatibilityWarnings: vi.fn().mockReturnValue([]),
isHeadlessMode: vi.fn().mockReturnValue(false),
WarningPriority: {
Expand Down Expand Up @@ -108,6 +107,60 @@ describe('getUserStartupWarnings', () => {
const warnings = await getUserStartupWarnings({}, homeDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
});

it('should not return a warning when running in a subdirectory of home', async () => {
const subDir = path.join(homeDir, 'projects', 'my-app');
await fs.mkdir(subDir, { recursive: true });
const warnings = await getUserStartupWarnings({}, subDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
});

it('should not return a warning when home directory is a symlink and running in a subdirectory', async () => {
const realHome = path.join(testRootDir, 'real-home');
await fs.mkdir(realHome, { recursive: true });
const symlinkedHome = path.join(testRootDir, 'symlinked-home');
await fs.symlink(realHome, symlinkedHome);
vi.mocked(os.homedir).mockReturnValue(symlinkedHome);

const subDir = path.join(symlinkedHome, 'projects');
await fs.mkdir(subDir, { recursive: true });
const warnings = await getUserStartupWarnings({}, subDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
});

it('should return a warning when home directory is a symlink and running in it', async () => {
const realHome = path.join(testRootDir, 'real-home2');
await fs.mkdir(realHome, { recursive: true });
const symlinkedHome = path.join(testRootDir, 'symlinked-home2');
await fs.symlink(realHome, symlinkedHome);
vi.mocked(os.homedir).mockReturnValue(symlinkedHome);

const warnings = await getUserStartupWarnings({}, symlinkedHome);
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'home-directory',
message: expect.stringContaining(
'Warning you are running Gemini CLI in your home directory',
),
priority: WarningPriority.Low,
}),
);
});

it('should not return a warning when GEMINI_CLI_HOME differs from os.homedir', async () => {
const projectDir = path.join(testRootDir, 'project');
await fs.mkdir(projectDir, { recursive: true });
vi.stubEnv('GEMINI_CLI_HOME', projectDir);

const warnings = await getUserStartupWarnings({}, projectDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
});
});

afterEach(async () => {
await fs.rm(testRootDir, { recursive: true, force: true });
vi.unstubAllEnvs();
vi.restoreAllMocks();
});

describe('root directory check', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/userStartupWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

import fs from 'node:fs/promises';
import { homedir as osHomedir } from 'node:os';
import path from 'node:path';
import process from 'node:process';
import {
homedir,
getCompatibilityWarnings,
WarningPriority,
type StartupWarning,
Expand Down Expand Up @@ -39,7 +39,7 @@ const homeDirectoryCheck: WarningCheck = {
try {
const [workspaceRealPath, homeRealPath] = await Promise.all([
fs.realpath(workspaceRoot),
fs.realpath(homedir()),
fs.realpath(osHomedir()),
]);

if (workspaceRealPath === homeRealPath) {
Expand Down