mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-10 14:10:37 -07:00
Fix missing slash commands when Gemini CLI is in a project with a package.json that doesn't follow semantic versioning (#17561)
This commit is contained in:
65
packages/core/src/utils/package.test.ts
Normal file
65
packages/core/src/utils/package.test.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { getPackageJson } from './package.js';
|
||||
import { readPackageUp } from 'read-package-up';
|
||||
|
||||
vi.mock('read-package-up', () => ({
|
||||
readPackageUp: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('getPackageJson', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should return packageJson when found', async () => {
|
||||
const expectedPackageJsonResult = { name: 'test-pkg', version: '1.2.3' };
|
||||
vi.mocked(readPackageUp).mockResolvedValue({
|
||||
packageJson: expectedPackageJsonResult,
|
||||
path: '/path/to/package.json',
|
||||
});
|
||||
|
||||
const result = await getPackageJson('/some/path');
|
||||
expect(result).toEqual(expectedPackageJsonResult);
|
||||
expect(readPackageUp).toHaveBeenCalledWith({
|
||||
cwd: '/some/path',
|
||||
normalize: false,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
description: 'no package.json is found',
|
||||
setup: () => vi.mocked(readPackageUp).mockResolvedValue(undefined),
|
||||
expected: undefined,
|
||||
},
|
||||
{
|
||||
description: 'non-semver versions (when normalize is false)',
|
||||
setup: () =>
|
||||
vi.mocked(readPackageUp).mockResolvedValue({
|
||||
packageJson: { name: 'test-pkg', version: '2024.60' },
|
||||
path: '/path/to/package.json',
|
||||
}),
|
||||
expected: { name: 'test-pkg', version: '2024.60' },
|
||||
},
|
||||
{
|
||||
description: 'readPackageUp throws',
|
||||
setup: () =>
|
||||
vi.mocked(readPackageUp).mockRejectedValue(new Error('Read error')),
|
||||
expected: undefined,
|
||||
},
|
||||
])('should handle $description', async ({ setup, expected }) => {
|
||||
setup();
|
||||
const result = await getPackageJson('/some/path');
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
readPackageUp,
|
||||
type PackageJson as BasePackageJson,
|
||||
} from 'read-package-up';
|
||||
import { debugLogger } from './debugLogger.js';
|
||||
|
||||
export type PackageJson = BasePackageJson & {
|
||||
config?: {
|
||||
@@ -32,10 +33,15 @@ export type PackageJson = BasePackageJson & {
|
||||
export async function getPackageJson(
|
||||
cwd: string,
|
||||
): Promise<PackageJson | undefined> {
|
||||
const result = await readPackageUp({ cwd });
|
||||
if (!result) {
|
||||
try {
|
||||
const result = await readPackageUp({ cwd, normalize: false });
|
||||
if (!result) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result.packageJson;
|
||||
} catch (error) {
|
||||
debugLogger.error('Error occurred while reading package.json', error);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result.packageJson;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user