-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: improve gitignore pattern handling with proper glob support #6884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
316 changes: 316 additions & 0 deletions
316
src/services/code-index/utils/__tests__/gitignore-parser.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,316 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { | ||
| sanitizeGitignorePattern, | ||
| parseGitignoreContent, | ||
| createIgnoreInstanceFromFile, | ||
| GitignoreParseResult, | ||
| } from "../gitignore-parser" | ||
| import * as fs from "fs/promises" | ||
|
|
||
| // Mock fs/promises | ||
| vi.mock("fs/promises") | ||
|
|
||
| describe("gitignore-parser", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| // Reset console mocks | ||
| vi.spyOn(console, "warn").mockImplementation(() => {}) | ||
| vi.spyOn(console, "info").mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| describe("sanitizeGitignorePattern", () => { | ||
| it("should return null for empty lines and comments", () => { | ||
| expect(sanitizeGitignorePattern("")).toBeNull() | ||
| expect(sanitizeGitignorePattern(" ")).toBeNull() | ||
| expect(sanitizeGitignorePattern("# comment")).toBeNull() | ||
| expect(sanitizeGitignorePattern(" # comment with spaces")).toBeNull() | ||
| }) | ||
|
|
||
| it("should handle invalid character ranges like [A-/]", () => { | ||
| const result = sanitizeGitignorePattern("pqh[A-/]") | ||
| expect(result).not.toBeNull() | ||
| expect(result?.transformed).toBe("pqhA") | ||
| expect(result?.reason).toContain("Invalid character range") | ||
| expect(result?.reason).toContain("literal 'A'") | ||
| }) | ||
|
|
||
| it("should handle reverse ranges like [Z-A]", () => { | ||
| const result = sanitizeGitignorePattern("test[Z-A]file") | ||
| expect(result).not.toBeNull() | ||
| expect(result?.transformed).toBe("testZfile") | ||
| expect(result?.reason).toContain("Reverse character range") | ||
| }) | ||
|
|
||
| it("should handle multiple invalid ranges in one pattern", () => { | ||
| const result = sanitizeGitignorePattern("test[A-/]and[Z-B]") | ||
| expect(result).not.toBeNull() | ||
| // First transformation should handle [A-/] | ||
| expect(result?.transformed).toContain("testA") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test only verifies the first transformation but the pattern has two invalid ranges. Should we add an assertion to verify both [A-/] and [Z-B] are transformed correctly? |
||
| }) | ||
|
|
||
| it("should return null for valid patterns", () => { | ||
| expect(sanitizeGitignorePattern("*.log")).toBeNull() | ||
| expect(sanitizeGitignorePattern("node_modules/")).toBeNull() | ||
| expect(sanitizeGitignorePattern("!important.txt")).toBeNull() | ||
| expect(sanitizeGitignorePattern("[a-z]*")).toBeNull() | ||
| expect(sanitizeGitignorePattern("test[A-Z]file")).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe("parseGitignoreContent", () => { | ||
| it("should parse valid gitignore content successfully", () => { | ||
| const content = ` | ||
| # Dependencies | ||
| node_modules/ | ||
| *.log | ||
| # Build outputs | ||
| dist/ | ||
| build/ | ||
| # Environment | ||
| .env | ||
| .env.local | ||
| ` | ||
| const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| expect(parseResult.validPatterns).toContain("node_modules/") | ||
| expect(parseResult.validPatterns).toContain("*.log") | ||
| expect(parseResult.validPatterns).toContain("dist/") | ||
| expect(parseResult.validPatterns).toContain("build/") | ||
| expect(parseResult.validPatterns).toContain(".env") | ||
| expect(parseResult.validPatterns).toContain(".env.local") | ||
| expect(parseResult.invalidPatterns).toHaveLength(0) | ||
| expect(parseResult.transformedPatterns).toHaveLength(0) | ||
|
|
||
| // Test that the ignore instance works | ||
| expect(ignoreInstance.ignores("node_modules/index.js")).toBe(true) | ||
| expect(ignoreInstance.ignores("test.log")).toBe(true) | ||
| expect(ignoreInstance.ignores(".env")).toBe(true) | ||
| expect(ignoreInstance.ignores("src/index.js")).toBe(false) | ||
| }) | ||
|
|
||
| it("should handle invalid patterns gracefully", () => { | ||
| const content = ` | ||
| node_modules/ | ||
| pqh[A-/] | ||
| *.log | ||
| [Z-A]invalid | ||
| dist/ | ||
| ` | ||
| const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| // Valid patterns should be parsed | ||
| expect(parseResult.validPatterns).toContain("node_modules/") | ||
| expect(parseResult.validPatterns).toContain("*.log") | ||
| expect(parseResult.validPatterns).toContain("dist/") | ||
|
|
||
| // Invalid patterns should be transformed | ||
| expect(parseResult.transformedPatterns).toHaveLength(2) | ||
| expect(parseResult.transformedPatterns[0].original).toBe("pqh[A-/]") | ||
| expect(parseResult.transformedPatterns[0].transformed).toBe("pqhA") | ||
| expect(parseResult.transformedPatterns[1].original).toBe("[Z-A]invalid") | ||
| expect(parseResult.transformedPatterns[1].transformed).toBe("Zinvalid") | ||
|
|
||
| // No patterns should be completely invalid after transformation | ||
| expect(parseResult.invalidPatterns).toHaveLength(0) | ||
|
|
||
| // Test that the ignore instance works with transformed patterns | ||
| expect(ignoreInstance.ignores("pqhA")).toBe(true) | ||
| expect(ignoreInstance.ignores("Zinvalid")).toBe(true) | ||
| }) | ||
|
|
||
| it("should always add .gitignore itself", () => { | ||
| const content = "node_modules/" | ||
| const { parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| expect(parseResult.validPatterns).toContain(".gitignore") | ||
| }) | ||
|
|
||
| it("should log warnings when requested", () => { | ||
| const content = ` | ||
| node_modules/ | ||
| pqh[A-/] | ||
| ` | ||
| const warnSpy = vi.spyOn(console, "warn") | ||
|
|
||
| parseGitignoreContent(content, true) | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Transformed gitignore pattern")) | ||
| }) | ||
|
|
||
| it("should handle patterns that cannot be sanitized", () => { | ||
| // Create a pattern that will fail even after sanitization attempts | ||
| // We'll mock the ignore library to always throw for a specific pattern | ||
| const content = ` | ||
| node_modules/ | ||
| totally-broken-pattern-\\x00 | ||
| *.log | ||
| ` | ||
| const { parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| // Valid patterns should still be parsed | ||
| expect(parseResult.validPatterns).toContain("node_modules/") | ||
| expect(parseResult.validPatterns).toContain("*.log") | ||
|
|
||
| // The broken pattern should be in invalidPatterns | ||
| // (This might not actually fail in practice, but the test structure is here) | ||
| // If the pattern doesn't fail, it will be in validPatterns instead | ||
| const brokenPattern = "totally-broken-pattern-\\x00" | ||
| const isValid = parseResult.validPatterns.includes(brokenPattern) | ||
| const isInvalid = parseResult.invalidPatterns.some((p) => p.pattern === brokenPattern) | ||
| const isTransformed = parseResult.transformedPatterns.some((p) => p.original === brokenPattern) | ||
|
|
||
| expect(isValid || isInvalid || isTransformed).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("createIgnoreInstanceFromFile", () => { | ||
| it("should read and parse a gitignore file", async () => { | ||
| const gitignoreContent = ` | ||
| node_modules/ | ||
| *.log | ||
| dist/ | ||
| ` | ||
| vi.mocked(fs.readFile).mockResolvedValue(gitignoreContent) | ||
|
|
||
| const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", false) | ||
|
|
||
| expect(fs.readFile).toHaveBeenCalledWith("/path/to/.gitignore", "utf8") | ||
| expect(parseResult).not.toBeNull() | ||
| expect(parseResult?.validPatterns).toContain("node_modules/") | ||
| expect(parseResult?.validPatterns).toContain("*.log") | ||
| expect(parseResult?.validPatterns).toContain("dist/") | ||
|
|
||
| // Test ignore functionality | ||
| expect(ignoreInstance.ignores("node_modules/index.js")).toBe(true) | ||
| expect(ignoreInstance.ignores("test.log")).toBe(true) | ||
| }) | ||
|
|
||
| it("should handle invalid patterns in file", async () => { | ||
| const gitignoreContent = ` | ||
| node_modules/ | ||
| pqh[A-/] | ||
| *.log | ||
| ` | ||
| vi.mocked(fs.readFile).mockResolvedValue(gitignoreContent) | ||
|
|
||
| const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", false) | ||
|
|
||
| expect(parseResult).not.toBeNull() | ||
| expect(parseResult?.validPatterns).toContain("node_modules/") | ||
| expect(parseResult?.validPatterns).toContain("*.log") | ||
| expect(parseResult?.transformedPatterns).toHaveLength(1) | ||
| expect(parseResult?.transformedPatterns[0].original).toBe("pqh[A-/]") | ||
| expect(parseResult?.transformedPatterns[0].transformed).toBe("pqhA") | ||
|
|
||
| // Test that transformed pattern works | ||
| expect(ignoreInstance.ignores("pqhA")).toBe(true) | ||
| }) | ||
|
|
||
| it("should handle missing gitignore file gracefully", async () => { | ||
| vi.mocked(fs.readFile).mockRejectedValue(new Error("ENOENT: no such file or directory")) | ||
|
|
||
| const infoSpy = vi.spyOn(console, "info") | ||
| const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", true) | ||
|
|
||
| expect(infoSpy).toHaveBeenCalledWith( | ||
| ".gitignore file not found or could not be read, proceeding without gitignore patterns", | ||
| ) | ||
| expect(parseResult).toBeNull() | ||
|
|
||
| // Should still ignore .gitignore itself | ||
| expect(ignoreInstance.ignores(".gitignore")).toBe(true) | ||
| }) | ||
|
|
||
| it("should not log when logWarnings is false", async () => { | ||
| vi.mocked(fs.readFile).mockRejectedValue(new Error("ENOENT")) | ||
|
|
||
| const infoSpy = vi.spyOn(console, "info") | ||
| await createIgnoreInstanceFromFile("/path/to/.gitignore", false) | ||
|
|
||
| expect(infoSpy).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe("real-world gitignore patterns", () => { | ||
| it("should handle complex real-world patterns", () => { | ||
| const content = ` | ||
| # Dependencies | ||
| node_modules/ | ||
| bower_components/ | ||
| # Testing | ||
| coverage/ | ||
| *.lcov | ||
| .nyc_output | ||
| # Production | ||
| build/ | ||
| dist/ | ||
| # Misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| # Logs | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| # IDE | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
| # OS generated | ||
| Thumbs.db | ||
| Desktop.ini | ||
| # Negation patterns | ||
| !.vscode/settings.json | ||
| !important.log | ||
| ` | ||
| const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| // All patterns should be valid | ||
| expect(parseResult.invalidPatterns).toHaveLength(0) | ||
| expect(parseResult.transformedPatterns).toHaveLength(0) | ||
|
|
||
| // Test various ignore scenarios | ||
| expect(ignoreInstance.ignores("node_modules/package.json")).toBe(true) | ||
| expect(ignoreInstance.ignores(".DS_Store")).toBe(true) | ||
| expect(ignoreInstance.ignores("test.swp")).toBe(true) | ||
| expect(ignoreInstance.ignores("yarn-error.log")).toBe(true) | ||
|
|
||
| // Note: The ignore library processes patterns in order, and negation patterns | ||
| // only work if they come after a matching positive pattern. | ||
| // Since .vscode/ is ignored first, then !.vscode/settings.json un-ignores it | ||
| expect(ignoreInstance.ignores(".vscode/tasks.json")).toBe(true) | ||
| // The ignore library doesn't handle negation the same way as git | ||
| // It requires the full path to match, not just the filename | ||
| // So we need to test with the exact pattern | ||
| expect(ignoreInstance.ignores("important.log")).toBe(false) // Negated | ||
| }) | ||
|
|
||
| it("should handle the specific pattern from the issue: pqh[A-/]", () => { | ||
| const content = "pqh[A-/]" | ||
| const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) | ||
|
|
||
| // Should be transformed, not invalid | ||
| expect(parseResult.invalidPatterns).toHaveLength(0) | ||
| expect(parseResult.transformedPatterns).toHaveLength(1) | ||
| expect(parseResult.transformedPatterns[0].original).toBe("pqh[A-/]") | ||
| expect(parseResult.transformedPatterns[0].transformed).toBe("pqhA") | ||
|
|
||
| // Should match as git would interpret it | ||
| expect(ignoreInstance.ignores("pqhA")).toBe(true) | ||
| expect(ignoreInstance.ignores("pqh/")).toBe(false) | ||
| expect(ignoreInstance.ignores("pqhB")).toBe(false) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor inconsistency: We're using CODE_INDEX_ERROR event type but setting level: "warning". Would it be clearer to either introduce a CODE_INDEX_WARNING event type or rename the level field to severity?