Make fileDiscoveryService.test.ts win compatible (#4892)
This commit is contained in:
parent
65aabfede8
commit
4c144e616d
|
@ -4,80 +4,86 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
import type { Mocked } from 'vitest';
|
import * as fs from 'fs/promises';
|
||||||
|
import * as os from 'os';
|
||||||
|
import * as path from 'path';
|
||||||
import { FileDiscoveryService } from './fileDiscoveryService.js';
|
import { FileDiscoveryService } from './fileDiscoveryService.js';
|
||||||
import { GitIgnoreParser } from '../utils/gitIgnoreParser.js';
|
|
||||||
import * as gitUtils from '../utils/gitUtils.js';
|
|
||||||
|
|
||||||
// Mock the GitIgnoreParser
|
|
||||||
vi.mock('../utils/gitIgnoreParser.js');
|
|
||||||
|
|
||||||
// Mock gitUtils module
|
|
||||||
vi.mock('../utils/gitUtils.js');
|
|
||||||
|
|
||||||
describe('FileDiscoveryService', () => {
|
describe('FileDiscoveryService', () => {
|
||||||
let service: FileDiscoveryService;
|
let testRootDir: string;
|
||||||
let mockGitIgnoreParser: Mocked<GitIgnoreParser>;
|
let projectRoot: string;
|
||||||
const mockProjectRoot = '/test/project';
|
|
||||||
|
|
||||||
beforeEach(() => {
|
async function createTestFile(filePath: string, content = '') {
|
||||||
mockGitIgnoreParser = {
|
const fullPath = path.join(projectRoot, filePath);
|
||||||
initialize: vi.fn(),
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
||||||
isIgnored: vi.fn(),
|
await fs.writeFile(fullPath, content);
|
||||||
loadPatterns: vi.fn(),
|
return fullPath;
|
||||||
loadGitRepoPatterns: vi.fn(),
|
}
|
||||||
} as unknown as Mocked<GitIgnoreParser>;
|
|
||||||
|
|
||||||
vi.mocked(GitIgnoreParser).mockImplementation(() => mockGitIgnoreParser);
|
beforeEach(async () => {
|
||||||
vi.mocked(gitUtils.isGitRepository).mockReturnValue(true);
|
testRootDir = await fs.mkdtemp(
|
||||||
vi.mocked(gitUtils.findGitRoot).mockReturnValue('/test/project');
|
path.join(os.tmpdir(), 'file-discovery-test-'),
|
||||||
vi.clearAllMocks();
|
);
|
||||||
|
projectRoot = path.join(testRootDir, 'project');
|
||||||
|
await fs.mkdir(projectRoot, { recursive: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(async () => {
|
||||||
vi.restoreAllMocks();
|
await fs.rm(testRootDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('initialization', () => {
|
describe('initialization', () => {
|
||||||
it('should initialize git ignore parser by default', () => {
|
it('should initialize git ignore parser by default in a git repo', async () => {
|
||||||
service = new FileDiscoveryService(mockProjectRoot);
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
expect(GitIgnoreParser).toHaveBeenCalledWith(mockProjectRoot);
|
await createTestFile('.gitignore', 'node_modules/');
|
||||||
expect(GitIgnoreParser).toHaveBeenCalledTimes(2);
|
|
||||||
expect(mockGitIgnoreParser.loadGitRepoPatterns).toHaveBeenCalled();
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
expect(mockGitIgnoreParser.loadPatterns).toHaveBeenCalled();
|
// 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 initialize git ignore parser when not a git repo', () => {
|
it('should not load git repo patterns when not in a git repo', async () => {
|
||||||
vi.mocked(gitUtils.isGitRepository).mockReturnValue(false);
|
// No .git directory
|
||||||
service = new FileDiscoveryService(mockProjectRoot);
|
await createTestFile('.gitignore', 'node_modules/');
|
||||||
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
|
|
||||||
expect(GitIgnoreParser).toHaveBeenCalledOnce();
|
// .gitignore is not loaded in non-git repos
|
||||||
expect(mockGitIgnoreParser.loadGitRepoPatterns).not.toHaveBeenCalled();
|
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', () => {
|
describe('filterFiles', () => {
|
||||||
beforeEach(() => {
|
beforeEach(async () => {
|
||||||
mockGitIgnoreParser.isIgnored.mockImplementation(
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
(path: string) =>
|
await createTestFile('.gitignore', 'node_modules/\n.git/\ndist');
|
||||||
path.includes('node_modules') || path.includes('.git'),
|
await createTestFile('.geminiignore', 'logs/');
|
||||||
);
|
|
||||||
service = new FileDiscoveryService(mockProjectRoot);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should filter out git-ignored files by default', () => {
|
it('should filter out git-ignored and gemini-ignored files by default', () => {
|
||||||
const files = [
|
const files = [
|
||||||
'src/index.ts',
|
'src/index.ts',
|
||||||
'node_modules/package/index.js',
|
'node_modules/package/index.js',
|
||||||
'README.md',
|
'README.md',
|
||||||
'.git/config',
|
'.git/config',
|
||||||
'dist/bundle.js',
|
'dist/bundle.js',
|
||||||
];
|
'logs/latest.log',
|
||||||
|
].map((f) => path.join(projectRoot, f));
|
||||||
|
|
||||||
const filtered = service.filterFiles(files);
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
|
|
||||||
expect(filtered).toEqual(['src/index.ts', 'README.md', 'dist/bundle.js']);
|
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', () => {
|
it('should not filter files when respectGitIgnore is false', () => {
|
||||||
|
@ -85,48 +91,121 @@ describe('FileDiscoveryService', () => {
|
||||||
'src/index.ts',
|
'src/index.ts',
|
||||||
'node_modules/package/index.js',
|
'node_modules/package/index.js',
|
||||||
'.git/config',
|
'.git/config',
|
||||||
];
|
'logs/latest.log',
|
||||||
|
].map((f) => path.join(projectRoot, f));
|
||||||
|
|
||||||
const filtered = service.filterFiles(files, { respectGitIgnore: false });
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
|
|
||||||
expect(filtered).toEqual(files);
|
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', () => {
|
it('should handle empty file list', () => {
|
||||||
const filtered = service.filterFiles([]);
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
expect(filtered).toEqual([]);
|
|
||||||
|
expect(service.filterFiles([])).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('shouldGitIgnoreFile', () => {
|
describe('shouldGitIgnoreFile & shouldGeminiIgnoreFile', () => {
|
||||||
beforeEach(() => {
|
beforeEach(async () => {
|
||||||
mockGitIgnoreParser.isIgnored.mockImplementation((path: string) =>
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
path.includes('node_modules'),
|
await createTestFile('.gitignore', 'node_modules/');
|
||||||
);
|
await createTestFile('.geminiignore', '*.log');
|
||||||
service = new FileDiscoveryService(mockProjectRoot);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true for git-ignored files', () => {
|
it('should return true for git-ignored files', () => {
|
||||||
expect(service.shouldGitIgnoreFile('node_modules/package/index.js')).toBe(
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
true,
|
|
||||||
);
|
expect(
|
||||||
|
service.shouldGitIgnoreFile(
|
||||||
|
path.join(projectRoot, 'node_modules/package/index.js'),
|
||||||
|
),
|
||||||
|
).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false for non-ignored files', () => {
|
it('should return false for non-git-ignored files', () => {
|
||||||
expect(service.shouldGitIgnoreFile('src/index.ts')).toBe(false);
|
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', () => {
|
describe('edge cases', () => {
|
||||||
it('should handle relative project root paths', () => {
|
it('should handle relative project root paths', async () => {
|
||||||
const relativeService = new FileDiscoveryService('./relative/path');
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
expect(relativeService).toBeInstanceOf(FileDiscoveryService);
|
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', () => {
|
it('should handle filterFiles with undefined options', async () => {
|
||||||
const files = ['src/index.ts'];
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
const filtered = service.filterFiles(files, undefined);
|
await createTestFile('.gitignore', 'ignored.txt');
|
||||||
expect(filtered).toEqual(files);
|
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'),
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue