Upgrade test to work on windows. (#4815)

This commit is contained in:
Tommaso Sciortino 2025-07-25 00:15:41 -07:00 committed by GitHub
parent 1d7eb0d250
commit 5d4b02ca85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 78 deletions

View File

@ -4,39 +4,43 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { GitIgnoreParser } from './gitIgnoreParser.js';
import * as fs from 'fs';
import * as fs from 'fs/promises';
import * as path from 'path';
import { isGitRepository } from './gitUtils.js';
// Mock fs module
vi.mock('fs');
// Mock gitUtils module
vi.mock('./gitUtils.js');
import * as os from 'os';
describe('GitIgnoreParser', () => {
let parser: GitIgnoreParser;
const mockProjectRoot = '/test/project';
let projectRoot: string;
beforeEach(() => {
parser = new GitIgnoreParser(mockProjectRoot);
// Reset mocks before each test
vi.mocked(fs.readFileSync).mockClear();
vi.mocked(isGitRepository).mockReturnValue(true);
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);
}
async function setupGitRepo() {
await fs.mkdir(path.join(projectRoot, '.git'), { recursive: true });
}
beforeEach(async () => {
projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'gitignore-test-'));
parser = new GitIgnoreParser(projectRoot);
});
afterEach(() => {
vi.restoreAllMocks();
afterEach(async () => {
await fs.rm(projectRoot, { recursive: true, force: true });
});
describe('initialization', () => {
it('should initialize without errors when no .gitignore exists', () => {
it('should initialize without errors when no .gitignore exists', async () => {
await setupGitRepo();
expect(() => parser.loadGitRepoPatterns()).not.toThrow();
});
it('should load .gitignore patterns when file exists', () => {
it('should load .gitignore patterns when file exists', async () => {
await setupGitRepo();
const gitignoreContent = `
# Comment
node_modules/
@ -44,7 +48,7 @@ node_modules/
/dist
.env
`;
vi.mocked(fs.readFileSync).mockReturnValueOnce(gitignoreContent);
await createTestFile('.gitignore', gitignoreContent);
parser.loadGitRepoPatterns();
@ -55,41 +59,35 @@ node_modules/
'/dist',
'.env',
]);
expect(parser.isIgnored('node_modules/some-lib')).toBe(true);
expect(parser.isIgnored('src/app.log')).toBe(true);
expect(parser.isIgnored('dist/index.js')).toBe(true);
expect(parser.isIgnored(path.join('node_modules', 'some-lib'))).toBe(
true,
);
expect(parser.isIgnored(path.join('src', 'app.log'))).toBe(true);
expect(parser.isIgnored(path.join('dist', 'index.js'))).toBe(true);
expect(parser.isIgnored('.env')).toBe(true);
});
it('should handle git exclude file', () => {
vi.mocked(fs.readFileSync).mockImplementation((filePath) => {
if (
filePath === path.join(mockProjectRoot, '.git', 'info', 'exclude')
) {
return 'temp/\n*.tmp';
}
throw new Error('ENOENT');
});
it('should handle git exclude file', async () => {
await setupGitRepo();
await createTestFile(
path.join('.git', 'info', 'exclude'),
'temp/\n*.tmp',
);
parser.loadGitRepoPatterns();
expect(parser.getPatterns()).toEqual(['.git', 'temp/', '*.tmp']);
expect(parser.isIgnored('temp/file.txt')).toBe(true);
expect(parser.isIgnored('src/file.tmp')).toBe(true);
expect(parser.isIgnored(path.join('temp', 'file.txt'))).toBe(true);
expect(parser.isIgnored(path.join('src', 'file.tmp'))).toBe(true);
});
it('should handle custom patterns file name', () => {
vi.mocked(isGitRepository).mockReturnValue(false);
vi.mocked(fs.readFileSync).mockImplementation((filePath) => {
if (filePath === path.join(mockProjectRoot, '.geminiignore')) {
return 'temp/\n*.tmp';
}
throw new Error('ENOENT');
});
it('should handle custom patterns file name', async () => {
// No .git directory for this test
await createTestFile('.geminiignore', 'temp/\n*.tmp');
parser.loadPatterns('.geminiignore');
expect(parser.getPatterns()).toEqual(['temp/', '*.tmp']);
expect(parser.isIgnored('temp/file.txt')).toBe(true);
expect(parser.isIgnored('src/file.tmp')).toBe(true);
expect(parser.isIgnored(path.join('temp', 'file.txt'))).toBe(true);
expect(parser.isIgnored(path.join('src', 'file.tmp'))).toBe(true);
});
it('should initialize without errors when no .geminiignore exists', () => {
@ -98,7 +96,8 @@ node_modules/
});
describe('isIgnored', () => {
beforeEach(() => {
beforeEach(async () => {
await setupGitRepo();
const gitignoreContent = `
node_modules/
*.log
@ -107,59 +106,65 @@ node_modules/
src/*.tmp
!src/important.tmp
`;
vi.mocked(fs.readFileSync).mockReturnValueOnce(gitignoreContent);
await createTestFile('.gitignore', gitignoreContent);
parser.loadGitRepoPatterns();
});
it('should always ignore .git directory', () => {
expect(parser.isIgnored('.git')).toBe(true);
expect(parser.isIgnored('.git/config')).toBe(true);
expect(parser.isIgnored(path.join(mockProjectRoot, '.git', 'HEAD'))).toBe(
expect(parser.isIgnored(path.join('.git', 'config'))).toBe(true);
expect(parser.isIgnored(path.join(projectRoot, '.git', 'HEAD'))).toBe(
true,
);
});
it('should ignore files matching patterns', () => {
expect(parser.isIgnored('node_modules/package/index.js')).toBe(true);
expect(
parser.isIgnored(path.join('node_modules', 'package', 'index.js')),
).toBe(true);
expect(parser.isIgnored('app.log')).toBe(true);
expect(parser.isIgnored('logs/app.log')).toBe(true);
expect(parser.isIgnored('dist/bundle.js')).toBe(true);
expect(parser.isIgnored(path.join('logs', 'app.log'))).toBe(true);
expect(parser.isIgnored(path.join('dist', 'bundle.js'))).toBe(true);
expect(parser.isIgnored('.env')).toBe(true);
expect(parser.isIgnored('config/.env')).toBe(false); // .env is anchored to root
expect(parser.isIgnored(path.join('config', '.env'))).toBe(false); // .env is anchored to root
});
it('should ignore files with path-specific patterns', () => {
expect(parser.isIgnored('src/temp.tmp')).toBe(true);
expect(parser.isIgnored('other/temp.tmp')).toBe(false);
expect(parser.isIgnored(path.join('src', 'temp.tmp'))).toBe(true);
expect(parser.isIgnored(path.join('other', 'temp.tmp'))).toBe(false);
});
it('should handle negation patterns', () => {
expect(parser.isIgnored('src/important.tmp')).toBe(false);
expect(parser.isIgnored(path.join('src', 'important.tmp'))).toBe(false);
});
it('should not ignore files that do not match patterns', () => {
expect(parser.isIgnored('src/index.ts')).toBe(false);
expect(parser.isIgnored(path.join('src', 'index.ts'))).toBe(false);
expect(parser.isIgnored('README.md')).toBe(false);
});
it('should handle absolute paths correctly', () => {
const absolutePath = path.join(mockProjectRoot, 'node_modules', 'lib');
const absolutePath = path.join(projectRoot, 'node_modules', 'lib');
expect(parser.isIgnored(absolutePath)).toBe(true);
});
it('should handle paths outside project root by not ignoring them', () => {
const outsidePath = path.resolve(mockProjectRoot, '../other/file.txt');
const outsidePath = path.resolve(projectRoot, '..', 'other', 'file.txt');
expect(parser.isIgnored(outsidePath)).toBe(false);
});
it('should handle relative paths correctly', () => {
expect(parser.isIgnored('node_modules/some-package')).toBe(true);
expect(parser.isIgnored('../some/other/file.txt')).toBe(false);
expect(parser.isIgnored(path.join('node_modules', 'some-package'))).toBe(
true,
);
expect(
parser.isIgnored(path.join('..', 'some', 'other', 'file.txt')),
).toBe(false);
});
it('should normalize path separators on Windows', () => {
expect(parser.isIgnored('node_modules\\package')).toBe(true);
expect(parser.isIgnored('src\\temp.tmp')).toBe(true);
expect(parser.isIgnored(path.join('node_modules', 'package'))).toBe(true);
expect(parser.isIgnored(path.join('src', 'temp.tmp'))).toBe(true);
});
it('should handle root path "/" without throwing error', () => {
@ -179,9 +184,10 @@ src/*.tmp
});
describe('getIgnoredPatterns', () => {
it('should return the raw patterns added', () => {
it('should return the raw patterns added', async () => {
await setupGitRepo();
const gitignoreContent = '*.log\n!important.log';
vi.mocked(fs.readFileSync).mockReturnValueOnce(gitignoreContent);
await createTestFile('.gitignore', gitignoreContent);
parser.loadGitRepoPatterns();
expect(parser.getPatterns()).toEqual(['.git', '*.log', '!important.log']);

View File

@ -57,24 +57,15 @@ export class GitIgnoreParser implements GitIgnoreFilter {
}
isIgnored(filePath: string): boolean {
const relativePath = path.isAbsolute(filePath)
? path.relative(this.projectRoot, filePath)
: filePath;
const resolved = path.resolve(this.projectRoot, filePath);
const relativePath = path.relative(this.projectRoot, resolved);
if (
relativePath === '' ||
relativePath.startsWith('..') ||
relativePath === '/' ||
relativePath.startsWith('/')
) {
if (relativePath === '' || relativePath.startsWith('..')) {
return false;
}
let normalizedPath = relativePath.replace(/\\/g, '/');
if (normalizedPath.startsWith('./')) {
normalizedPath = normalizedPath.substring(2);
}
// Even in windows, Ignore expects forward slashes.
const normalizedPath = relativePath.replace(/\\/g, '/');
return this.ig.ignores(normalizedPath);
}