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
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs/promises';
import * as os from 'os';
import * as path from 'path';
import { FileDiscoveryService } from './fileDiscoveryService.js';
describe('FileDiscoveryService', () => {
let testRootDir: string;
let projectRoot: string;
async function createTestFile(filePath: string, content = '') {
const fullPath = path.join(projectRoot, filePath);
await fs.mkdir(path.dirname(fullPath), { recursive: true });
await fs.writeFile(fullPath, content);
return fullPath;
}
beforeEach(async () => {
testRootDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'file-discovery-test-'),
);
projectRoot = path.join(testRootDir, 'project');
await fs.mkdir(projectRoot, { recursive: true });
});
afterEach(async () => {
await fs.rm(testRootDir, { recursive: true, force: true });
});
describe('initialization', () => {
it('should initialize git ignore parser by default in a git repo', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
const service = new FileDiscoveryService(projectRoot);
// Let's check the effect of the parser instead of mocking it.
expect(service.shouldGitIgnoreFile('node_modules/foo.js')).toBe(true);
expect(service.shouldGitIgnoreFile('src/foo.js')).toBe(false);
});
it('should not load git repo patterns when not in a git repo', async () => {
// No .git directory
await createTestFile('.gitignore', 'node_modules/');
const service = new FileDiscoveryService(projectRoot);
// .gitignore is not loaded in non-git repos
expect(service.shouldGitIgnoreFile('node_modules/foo.js')).toBe(false);
});
it('should load .geminiignore patterns even when not in a git repo', async () => {
await createTestFile('.geminiignore', 'secrets.txt');
const service = new FileDiscoveryService(projectRoot);
expect(service.shouldGeminiIgnoreFile('secrets.txt')).toBe(true);
expect(service.shouldGeminiIgnoreFile('src/index.js')).toBe(false);
});
});
describe('filterFiles', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/\n.git/\ndist');
await createTestFile('.geminiignore', 'logs/');
});
it('should filter out git-ignored and gemini-ignored files by default', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'README.md',
'.git/config',
'dist/bundle.js',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
expect(service.filterFiles(files)).toEqual(
['src/index.ts', 'README.md'].map((f) => path.join(projectRoot, f)),
);
});
it('should not filter files when respectGitIgnore is false', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'.git/config',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
const filtered = service.filterFiles(files, {
respectGitIgnore: false,
respectGeminiIgnore: true, // still respect this one
});
expect(filtered).toEqual(
['src/index.ts', 'node_modules/package/index.js', '.git/config'].map(
(f) => path.join(projectRoot, f),
),
);
});
it('should not filter files when respectGeminiIgnore is false', () => {
const files = [
'src/index.ts',
'node_modules/package/index.js',
'logs/latest.log',
].map((f) => path.join(projectRoot, f));
const service = new FileDiscoveryService(projectRoot);
const filtered = service.filterFiles(files, {
respectGitIgnore: true,
respectGeminiIgnore: false,
});
expect(filtered).toEqual(
['src/index.ts', 'logs/latest.log'].map((f) =>
path.join(projectRoot, f),
),
);
});
it('should handle empty file list', () => {
const service = new FileDiscoveryService(projectRoot);
expect(service.filterFiles([])).toEqual([]);
});
});
describe('shouldGitIgnoreFile & shouldGeminiIgnoreFile', () => {
beforeEach(async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'node_modules/');
await createTestFile('.geminiignore', '*.log');
});
it('should return true for git-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldGitIgnoreFile(
path.join(projectRoot, 'node_modules/package/index.js'),
),
).toBe(true);
});
it('should return false for non-git-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldGitIgnoreFile(path.join(projectRoot, 'src/index.ts')),
).toBe(false);
});
it('should return true for gemini-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldGeminiIgnoreFile(path.join(projectRoot, 'debug.log')),
).toBe(true);
});
it('should return false for non-gemini-ignored files', () => {
const service = new FileDiscoveryService(projectRoot);
expect(
service.shouldGeminiIgnoreFile(path.join(projectRoot, 'src/index.ts')),
).toBe(false);
});
});
describe('edge cases', () => {
it('should handle relative project root paths', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'ignored.txt');
const service = new FileDiscoveryService(
path.relative(process.cwd(), projectRoot),
);
expect(
service.shouldGitIgnoreFile(path.join(projectRoot, 'ignored.txt')),
).toBe(true);
expect(
service.shouldGitIgnoreFile(path.join(projectRoot, 'not-ignored.txt')),
).toBe(false);
});
it('should handle filterFiles with undefined options', async () => {
await fs.mkdir(path.join(projectRoot, '.git'));
await createTestFile('.gitignore', 'ignored.txt');
const service = new FileDiscoveryService(projectRoot);
const files = ['src/index.ts', 'ignored.txt'].map((f) =>
path.join(projectRoot, f),
);
expect(service.filterFiles(files, undefined)).toEqual([
path.join(projectRoot, 'src/index.ts'),
]);
});
});
});
|