reuse GitIgnoreParser for loading .geminiignore (#1025)

This commit is contained in:
Anas H. Sulaiman
2025-06-13 17:17:08 -04:00
committed by GitHub
parent 2d84cf7803
commit 84c7332308
5 changed files with 82 additions and 136 deletions
@@ -42,9 +42,6 @@ describe('loadGeminiIgnorePatterns', () => {
let consoleLogSpy: Mock<
(message?: unknown, ...optionalParams: unknown[]) => void
>;
let consoleWarnSpy: Mock<
(message?: unknown, ...optionalParams: unknown[]) => void
>;
beforeAll(async () => {
actualFs = await vi.importActual<typeof import('node:fs')>('node:fs');
@@ -62,11 +59,6 @@ describe('loadGeminiIgnorePatterns', () => {
.mockImplementation(() => {}) as Mock<
(message?: unknown, ...optionalParams: unknown[]) => void
>;
consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {}) as Mock<
(message?: unknown, ...optionalParams: unknown[]) => void
>;
mockedFsReadFileSync.mockReset();
});
@@ -77,7 +69,7 @@ describe('loadGeminiIgnorePatterns', () => {
vi.restoreAllMocks();
});
it('should load and parse patterns from .geminiignore, ignoring comments and empty lines', () => {
it('should load and parse patterns from .geminiignore, ignoring comments and empty lines', async () => {
const ignoreContent = [
'# This is a comment',
'pattern1',
@@ -90,14 +82,7 @@ describe('loadGeminiIgnorePatterns', () => {
const ignoreFilePath = path.join(tempDir, '.geminiignore');
actualFs.writeFileSync(ignoreFilePath, ignoreContent);
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') return ignoreContent;
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
const patterns = await loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual([
'pattern1',
@@ -109,39 +94,19 @@ describe('loadGeminiIgnorePatterns', () => {
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Loaded 5 patterns from .geminiignore'),
);
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
it('should return an empty array and log info if .geminiignore is not found', () => {
const ignoreFilePath = path.join(tempDir, '.geminiignore');
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') {
const error = new Error('File not found') as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
}
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
it('should return an empty array and log info if .geminiignore is not found', async () => {
const patterns = await loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual([]);
expect(consoleLogSpy).not.toHaveBeenCalled();
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
it('should return an empty array if .geminiignore is empty', () => {
it('should return an empty array if .geminiignore is empty', async () => {
const ignoreFilePath = path.join(tempDir, '.geminiignore');
actualFs.writeFileSync(ignoreFilePath, '');
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') return ''; // Return string for empty file
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
const patterns = await loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual([]);
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining('Loaded 0 patterns from .geminiignore'),
@@ -149,10 +114,9 @@ describe('loadGeminiIgnorePatterns', () => {
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining('No .geminiignore file found'),
);
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
it('should return an empty array if .geminiignore contains only comments and empty lines', () => {
it('should return an empty array if .geminiignore contains only comments and empty lines', async () => {
const ignoreContent = [
'# Comment 1',
' # Comment 2 with leading spaces',
@@ -161,14 +125,8 @@ describe('loadGeminiIgnorePatterns', () => {
].join('\n');
const ignoreFilePath = path.join(tempDir, '.geminiignore');
actualFs.writeFileSync(ignoreFilePath, ignoreContent);
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') return ignoreContent;
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
const patterns = await loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual([]);
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining('Loaded 0 patterns from .geminiignore'),
@@ -176,48 +134,17 @@ describe('loadGeminiIgnorePatterns', () => {
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining('No .geminiignore file found'),
);
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
it('should handle read errors (other than ENOENT) and log a warning', () => {
const ignoreFilePath = path.join(tempDir, '.geminiignore');
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') {
const error = new Error('Test read error') as NodeJS.ErrnoException;
error.code = 'EACCES';
throw error;
}
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual([]);
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining(
`[WARN] Could not read .geminiignore file at ${ignoreFilePath}: Test read error`,
),
);
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
it('should correctly handle patterns with inline comments if not starting with #', () => {
it('should correctly handle patterns with inline comments if not starting with #', async () => {
const ignoreContent = 'src/important # but not this part';
const ignoreFilePath = path.join(tempDir, '.geminiignore');
actualFs.writeFileSync(ignoreFilePath, ignoreContent);
mockedFsReadFileSync.mockImplementation((p: string, encoding: string) => {
if (p === ignoreFilePath && encoding === 'utf-8') return ignoreContent;
throw new Error(
`Mock fs.readFileSync: Unexpected call with path: ${p}, encoding: ${encoding}`,
);
});
const patterns = loadGeminiIgnorePatterns(tempDir);
const patterns = await loadGeminiIgnorePatterns(tempDir);
expect(patterns).toEqual(['src/important # but not this part']);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Loaded 1 patterns from .geminiignore'),
);
expect(mockedFsReadFileSync).toHaveBeenCalledWith(ignoreFilePath, 'utf-8');
});
});