mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 08:47:44 +00:00
feat(core): Add .gitignore support to getFolderStructure (#865)
This commit is contained in:
@@ -4,11 +4,13 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
|
||||
import fsPromises from 'fs/promises';
|
||||
import { Dirent as FSDirent } from 'fs';
|
||||
import * as nodePath from 'path';
|
||||
import { getFolderStructure } from './getFolderStructure.js';
|
||||
import * as gitUtils from './gitUtils.js';
|
||||
|
||||
vi.mock('path', async (importOriginal) => {
|
||||
const original = (await importOriginal()) as typeof nodePath;
|
||||
@@ -20,6 +22,7 @@ vi.mock('path', async (importOriginal) => {
|
||||
});
|
||||
|
||||
vi.mock('fs/promises');
|
||||
vi.mock('./gitUtils.js');
|
||||
|
||||
// Import 'path' again here, it will be the mocked version
|
||||
import * as path from 'path';
|
||||
@@ -276,3 +279,61 @@ Showing up to 3 items (files + folders).
|
||||
expect(structure.trim()).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFolderStructure gitignore', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
(path.resolve as Mock).mockImplementation((str: string) => str);
|
||||
|
||||
(fsPromises.readdir as Mock).mockImplementation(async (p) => {
|
||||
const path = p.toString();
|
||||
if (path === '/test/project') {
|
||||
return [
|
||||
createDirent('file1.txt', 'file'),
|
||||
createDirent('node_modules', 'dir'),
|
||||
createDirent('ignored.txt', 'file'),
|
||||
createDirent('.gemini', 'dir'),
|
||||
] as any;
|
||||
}
|
||||
if (path === '/test/project/node_modules') {
|
||||
return [createDirent('some-package', 'dir')] as any;
|
||||
}
|
||||
if (path === '/test/project/.gemini') {
|
||||
return [
|
||||
createDirent('config.yaml', 'file'),
|
||||
createDirent('logs.json', 'file'),
|
||||
] as any;
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
(fsPromises.readFile as Mock).mockImplementation(async (p) => {
|
||||
const path = p.toString();
|
||||
if (path === '/test/project/.gitignore') {
|
||||
return 'ignored.txt\nnode_modules/\n.gemini/\n!/.gemini/config.yaml';
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
vi.mocked(gitUtils.isGitRepository).mockReturnValue(true);
|
||||
});
|
||||
|
||||
it('should ignore files and folders specified in .gitignore', async () => {
|
||||
const structure = await getFolderStructure('/test/project', {
|
||||
projectRoot: '/test/project',
|
||||
});
|
||||
expect(structure).not.toContain('ignored.txt');
|
||||
expect(structure).toContain('node_modules/...');
|
||||
expect(structure).not.toContain('logs.json');
|
||||
});
|
||||
|
||||
it('should not ignore files if respectGitIgnore is false', async () => {
|
||||
const structure = await getFolderStructure('/test/project', {
|
||||
projectRoot: '/test/project',
|
||||
respectGitIgnore: false,
|
||||
});
|
||||
expect(structure).toContain('ignored.txt');
|
||||
// node_modules is still ignored by default
|
||||
expect(structure).toContain('node_modules/...');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user