Fix tests to work in windows (#4754)
This commit is contained in:
parent
d7a304bcff
commit
e21b5c95aa
|
@ -4,177 +4,129 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
|
||||||
import * as fsPromises from 'fs/promises';
|
import * as fsPromises from 'fs/promises';
|
||||||
import * as gitUtils from './gitUtils.js';
|
import * as path from 'path';
|
||||||
|
import * as os from 'os';
|
||||||
import { bfsFileSearch } from './bfsFileSearch.js';
|
import { bfsFileSearch } from './bfsFileSearch.js';
|
||||||
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
||||||
|
|
||||||
vi.mock('fs');
|
|
||||||
vi.mock('fs/promises');
|
|
||||||
vi.mock('./gitUtils.js');
|
|
||||||
|
|
||||||
const createMockDirent = (name: string, isFile: boolean): fs.Dirent => {
|
|
||||||
const dirent = new fs.Dirent();
|
|
||||||
dirent.name = name;
|
|
||||||
dirent.isFile = () => isFile;
|
|
||||||
dirent.isDirectory = () => !isFile;
|
|
||||||
return dirent;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Type for the specific overload we're using
|
|
||||||
type ReaddirWithFileTypes = (
|
|
||||||
path: fs.PathLike,
|
|
||||||
options: { withFileTypes: true },
|
|
||||||
) => Promise<fs.Dirent[]>;
|
|
||||||
|
|
||||||
describe('bfsFileSearch', () => {
|
describe('bfsFileSearch', () => {
|
||||||
beforeEach(() => {
|
let testRootDir: string;
|
||||||
vi.resetAllMocks();
|
|
||||||
|
async function createEmptyDir(...pathSegments: string[]) {
|
||||||
|
const fullPath = path.join(testRootDir, ...pathSegments);
|
||||||
|
await fsPromises.mkdir(fullPath, { recursive: true });
|
||||||
|
return fullPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createTestFile(content: string, ...pathSegments: string[]) {
|
||||||
|
const fullPath = path.join(testRootDir, ...pathSegments);
|
||||||
|
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true });
|
||||||
|
await fsPromises.writeFile(fullPath, content);
|
||||||
|
return fullPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
testRootDir = await fsPromises.mkdtemp(
|
||||||
|
path.join(os.tmpdir(), 'bfs-file-search-test-'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await fsPromises.rm(testRootDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should find a file in the root directory', async () => {
|
it('should find a file in the root directory', async () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
const targetFilePath = await createTestFile('content', 'target.txt');
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
const result = await bfsFileSearch(testRootDir, { fileName: 'target.txt' });
|
||||||
vi.mocked(mockReaddir).mockResolvedValue([
|
expect(result).toEqual([targetFilePath]);
|
||||||
createMockDirent('file1.txt', true),
|
|
||||||
createMockDirent('file2.txt', true),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const result = await bfsFileSearch('/test', { fileName: 'file1.txt' });
|
|
||||||
expect(result).toEqual(['/test/file1.txt']);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should find a file in a subdirectory', async () => {
|
it('should find a file in a nested directory', async () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
const targetFilePath = await createTestFile(
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
'content',
|
||||||
vi.mocked(mockReaddir).mockImplementation(async (dir) => {
|
'a',
|
||||||
if (dir === '/test') {
|
'b',
|
||||||
return [createMockDirent('subdir', false)];
|
'target.txt',
|
||||||
}
|
);
|
||||||
if (dir === '/test/subdir') {
|
const result = await bfsFileSearch(testRootDir, { fileName: 'target.txt' });
|
||||||
return [createMockDirent('file1.txt', true)];
|
expect(result).toEqual([targetFilePath]);
|
||||||
}
|
|
||||||
return [];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await bfsFileSearch('/test', { fileName: 'file1.txt' });
|
it('should find multiple files with the same name', async () => {
|
||||||
expect(result).toEqual(['/test/subdir/file1.txt']);
|
const targetFilePath1 = await createTestFile('content1', 'a', 'target.txt');
|
||||||
|
const targetFilePath2 = await createTestFile('content2', 'b', 'target.txt');
|
||||||
|
const result = await bfsFileSearch(testRootDir, { fileName: 'target.txt' });
|
||||||
|
result.sort();
|
||||||
|
expect(result).toEqual([targetFilePath1, targetFilePath2].sort());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore specified directories', async () => {
|
it('should return an empty array if no file is found', async () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
await createTestFile('content', 'other.txt');
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
const result = await bfsFileSearch(testRootDir, { fileName: 'target.txt' });
|
||||||
vi.mocked(mockReaddir).mockImplementation(async (dir) => {
|
expect(result).toEqual([]);
|
||||||
if (dir === '/test') {
|
|
||||||
return [
|
|
||||||
createMockDirent('subdir1', false),
|
|
||||||
createMockDirent('subdir2', false),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir1') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir2') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await bfsFileSearch('/test', {
|
it('should ignore directories specified in ignoreDirs', async () => {
|
||||||
fileName: 'file1.txt',
|
await createTestFile('content', 'ignored', 'target.txt');
|
||||||
ignoreDirs: ['subdir2'],
|
const targetFilePath = await createTestFile(
|
||||||
|
'content',
|
||||||
|
'not-ignored',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
const result = await bfsFileSearch(testRootDir, {
|
||||||
|
fileName: 'target.txt',
|
||||||
|
ignoreDirs: ['ignored'],
|
||||||
});
|
});
|
||||||
expect(result).toEqual(['/test/subdir1/file1.txt']);
|
expect(result).toEqual([targetFilePath]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect maxDirs limit', async () => {
|
it('should respect the maxDirs limit and not find the file', async () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
await createTestFile('content', 'a', 'b', 'c', 'target.txt');
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
const result = await bfsFileSearch(testRootDir, {
|
||||||
vi.mocked(mockReaddir).mockImplementation(async (dir) => {
|
fileName: 'target.txt',
|
||||||
if (dir === '/test') {
|
maxDirs: 3,
|
||||||
return [
|
});
|
||||||
createMockDirent('subdir1', false),
|
expect(result).toEqual([]);
|
||||||
createMockDirent('subdir2', false),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir1') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir2') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await bfsFileSearch('/test', {
|
it('should respect the maxDirs limit and find the file', async () => {
|
||||||
fileName: 'file1.txt',
|
const targetFilePath = await createTestFile(
|
||||||
maxDirs: 2,
|
'content',
|
||||||
|
'a',
|
||||||
|
'b',
|
||||||
|
'c',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
const result = await bfsFileSearch(testRootDir, {
|
||||||
|
fileName: 'target.txt',
|
||||||
|
maxDirs: 4,
|
||||||
});
|
});
|
||||||
expect(result).toEqual(['/test/subdir1/file1.txt']);
|
expect(result).toEqual([targetFilePath]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect .gitignore files', async () => {
|
describe('with FileDiscoveryService', () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
let projectRoot: string;
|
||||||
const mockGitUtils = vi.mocked(gitUtils);
|
|
||||||
mockGitUtils.isGitRepository.mockReturnValue(true);
|
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
|
||||||
vi.mocked(mockReaddir).mockImplementation(async (dir) => {
|
|
||||||
if (dir === '/test') {
|
|
||||||
return [
|
|
||||||
createMockDirent('.gitignore', true),
|
|
||||||
createMockDirent('subdir1', false),
|
|
||||||
createMockDirent('subdir2', false),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir1') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir2') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
});
|
|
||||||
vi.mocked(fs).readFileSync.mockReturnValue('subdir2');
|
|
||||||
|
|
||||||
const fileService = new FileDiscoveryService('/test');
|
beforeEach(async () => {
|
||||||
const result = await bfsFileSearch('/test', {
|
projectRoot = await createEmptyDir('project');
|
||||||
fileName: 'file1.txt',
|
|
||||||
fileService,
|
|
||||||
});
|
|
||||||
expect(result).toEqual(['/test/subdir1/file1.txt']);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect .geminiignore files', async () => {
|
it('should ignore gitignored files', async () => {
|
||||||
const mockFs = vi.mocked(fsPromises);
|
await createEmptyDir('project', '.git');
|
||||||
const mockGitUtils = vi.mocked(gitUtils);
|
await createTestFile('node_modules/', 'project', '.gitignore');
|
||||||
|
await createTestFile('content', 'project', 'node_modules', 'target.txt');
|
||||||
|
const targetFilePath = await createTestFile(
|
||||||
|
'content',
|
||||||
|
'project',
|
||||||
|
'not-ignored',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
|
||||||
mockGitUtils.isGitRepository.mockReturnValue(false);
|
const fileService = new FileDiscoveryService(projectRoot);
|
||||||
|
const result = await bfsFileSearch(projectRoot, {
|
||||||
const mockReaddir = mockFs.readdir as unknown as ReaddirWithFileTypes;
|
fileName: 'target.txt',
|
||||||
vi.mocked(mockReaddir).mockImplementation(async (dir) => {
|
|
||||||
if (dir === '/test') {
|
|
||||||
return [
|
|
||||||
createMockDirent('.geminiignore', true),
|
|
||||||
createMockDirent('subdir1', false),
|
|
||||||
createMockDirent('subdir2', false),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir1') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
if (dir === '/test/subdir2') {
|
|
||||||
return [createMockDirent('file1.txt', true)];
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mocked(fs).readFileSync.mockReturnValue('subdir2');
|
|
||||||
|
|
||||||
const fileService = new FileDiscoveryService('/test');
|
|
||||||
const result = await bfsFileSearch('/test', {
|
|
||||||
fileName: 'file1.txt',
|
|
||||||
fileService,
|
fileService,
|
||||||
fileFilteringOptions: {
|
fileFilteringOptions: {
|
||||||
respectGitIgnore: true,
|
respectGitIgnore: true,
|
||||||
|
@ -182,6 +134,59 @@ describe('bfsFileSearch', () => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toEqual(['/test/subdir1/file1.txt']);
|
expect(result).toEqual([targetFilePath]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ignore geminiignored files', async () => {
|
||||||
|
await createTestFile('node_modules/', 'project', '.geminiignore');
|
||||||
|
await createTestFile('content', 'project', 'node_modules', 'target.txt');
|
||||||
|
const targetFilePath = await createTestFile(
|
||||||
|
'content',
|
||||||
|
'project',
|
||||||
|
'not-ignored',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
|
||||||
|
const fileService = new FileDiscoveryService(projectRoot);
|
||||||
|
const result = await bfsFileSearch(projectRoot, {
|
||||||
|
fileName: 'target.txt',
|
||||||
|
fileService,
|
||||||
|
fileFilteringOptions: {
|
||||||
|
respectGitIgnore: false,
|
||||||
|
respectGeminiIgnore: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual([targetFilePath]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not ignore files if respect flags are false', async () => {
|
||||||
|
await createEmptyDir('project', '.git');
|
||||||
|
await createTestFile('node_modules/', 'project', '.gitignore');
|
||||||
|
const target1 = await createTestFile(
|
||||||
|
'content',
|
||||||
|
'project',
|
||||||
|
'node_modules',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
const target2 = await createTestFile(
|
||||||
|
'content',
|
||||||
|
'project',
|
||||||
|
'not-ignored',
|
||||||
|
'target.txt',
|
||||||
|
);
|
||||||
|
|
||||||
|
const fileService = new FileDiscoveryService(projectRoot);
|
||||||
|
const result = await bfsFileSearch(projectRoot, {
|
||||||
|
fileName: 'target.txt',
|
||||||
|
fileService,
|
||||||
|
fileFilteringOptions: {
|
||||||
|
respectGitIgnore: false,
|
||||||
|
respectGeminiIgnore: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.sort()).toEqual([target1, target2].sort());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -309,7 +309,7 @@ ${testRootDir}${path.sep}
|
||||||
fileService,
|
fileService,
|
||||||
});
|
});
|
||||||
expect(structure).not.toContain('ignored.txt');
|
expect(structure).not.toContain('ignored.txt');
|
||||||
expect(structure).toContain('node_modules/...');
|
expect(structure).toContain(`node_modules${path.sep}...`);
|
||||||
expect(structure).not.toContain('logs.json');
|
expect(structure).not.toContain('logs.json');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -334,7 +334,7 @@ ${testRootDir}${path.sep}
|
||||||
});
|
});
|
||||||
expect(structure).toContain('ignored.txt');
|
expect(structure).toContain('ignored.txt');
|
||||||
// node_modules is still ignored by default
|
// node_modules is still ignored by default
|
||||||
expect(structure).toContain('node_modules/...');
|
expect(structure).toContain(`node_modules${path.sep}...`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue