-
Notifications
You must be signed in to change notification settings - Fork 3.5k
improvement(vfs): update custom glob impl to use micromatch, fix vfs filename regex #3680
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5abe61e
improvement(vfs): update custom glob impl to use micromatch, fix vfs …
icecrasher321 ad8fb83
add tests
icecrasher321 de73cc5
file caps
icecrasher321 ffe56fc
address comments
icecrasher321 f254678
fix open resource
icecrasher321 0fb04fb
consolidate files
icecrasher321 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
add tests
- Loading branch information
commit ad8fb838b9389ef142cf6c43809412aaacdc1a5a
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { glob, grep } from '@/lib/copilot/vfs/operations' | ||
|
|
||
| function vfsFromEntries(entries: [string, string][]): Map<string, string> { | ||
| return new Map(entries) | ||
| } | ||
|
|
||
| describe('glob', () => { | ||
| it('matches one path segment for single star (files listing pattern)', () => { | ||
| const files = vfsFromEntries([ | ||
| ['files/a/meta.json', '{}'], | ||
| ['files/a/b/meta.json', '{}'], | ||
| ['uploads/x.png', ''], | ||
| ]) | ||
| const hits = glob(files, 'files/*/meta.json') | ||
| expect(hits).toContain('files/a/meta.json') | ||
| expect(hits).not.toContain('files/a/b/meta.json') | ||
| }) | ||
|
|
||
| it('matches nested paths with double star', () => { | ||
| const files = vfsFromEntries([ | ||
| ['workflows/W/state.json', ''], | ||
| ['workflows/W/sub/state.json', ''], | ||
| ]) | ||
| const hits = glob(files, 'workflows/**/state.json') | ||
| expect(hits.sort()).toEqual(['workflows/W/state.json', 'workflows/W/sub/state.json'].sort()) | ||
| }) | ||
|
|
||
| it('includes virtual directory prefixes when pattern matches descendants', () => { | ||
| const files = vfsFromEntries([['files/a/meta.json', '{}']]) | ||
| const hits = glob(files, 'files/**') | ||
| expect(hits).toContain('files') | ||
| expect(hits).toContain('files/a') | ||
| expect(hits).toContain('files/a/meta.json') | ||
| }) | ||
|
|
||
| it('treats braces literally when nobrace is set (matches old builder)', () => { | ||
| const files = vfsFromEntries([ | ||
| ['weird{brace}/x', ''], | ||
| ['weirdA/x', ''], | ||
| ]) | ||
| const hits = glob(files, 'weird{brace}/*') | ||
| expect(hits).toContain('weird{brace}/x') | ||
| expect(hits).not.toContain('weirdA/x') | ||
| }) | ||
| }) | ||
|
|
||
| describe('grep', () => { | ||
| it('returns content matches per line in default mode', () => { | ||
| const files = vfsFromEntries([['a.txt', 'hello\nworld\nhello']]) | ||
| const matches = grep(files, 'hello', undefined, { outputMode: 'content' }) | ||
| expect(matches).toHaveLength(2) | ||
| expect(matches[0]).toMatchObject({ path: 'a.txt', line: 1, content: 'hello' }) | ||
| expect(matches[1]).toMatchObject({ path: 'a.txt', line: 3, content: 'hello' }) | ||
| }) | ||
|
|
||
| it('strips CR before end-of-line matching on CRLF content', () => { | ||
| const files = vfsFromEntries([['x.txt', 'foo\r\n']]) | ||
| const matches = grep(files, 'foo$', undefined, { outputMode: 'content' }) | ||
| expect(matches).toHaveLength(1) | ||
| expect(matches[0]?.content).toBe('foo') | ||
| }) | ||
|
|
||
| it('counts matching lines', () => { | ||
| const files = vfsFromEntries([['a.txt', 'a\nb\na']]) | ||
| const counts = grep(files, 'a', undefined, { outputMode: 'count' }) | ||
| expect(counts).toEqual([{ path: 'a.txt', count: 2 }]) | ||
| }) | ||
|
|
||
| it('files_with_matches scans whole file (can match across newlines with dot-all style pattern)', () => { | ||
| const files = vfsFromEntries([['a.txt', 'foo\nbar']]) | ||
| const multiline = grep(files, 'foo[\\s\\S]*bar', undefined, { | ||
| outputMode: 'files_with_matches', | ||
| }) | ||
| expect(multiline).toContain('a.txt') | ||
|
|
||
| const lineOnly = grep(files, 'foo[\\s\\S]*bar', undefined, { outputMode: 'content' }) | ||
| expect(lineOnly).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('scopes to directory prefix without matching unrelated prefixes', () => { | ||
| const files = vfsFromEntries([ | ||
| ['workflows/a/x', 'needle'], | ||
| ['workflowsManual/x', 'needle'], | ||
| ]) | ||
| const hits = grep(files, 'needle', 'workflows', { outputMode: 'files_with_matches' }) | ||
| expect(hits).toContain('workflows/a/x') | ||
| expect(hits).not.toContain('workflowsManual/x') | ||
| }) | ||
|
|
||
| it('scopes with glob pattern when path contains metacharacters', () => { | ||
| const files = vfsFromEntries([ | ||
| ['workflows/A/state.json', '{"x":1}'], | ||
| ['workflows/B/sub/state.json', '{"x":1}'], | ||
| ['workflows/C/other.json', '{"x":1}'], | ||
| ]) | ||
| const hits = grep(files, '1', 'workflows/*/state.json', { outputMode: 'files_with_matches' }) | ||
| expect(hits).toEqual(['workflows/A/state.json']) | ||
| }) | ||
|
|
||
| it('returns empty array for invalid regex pattern', () => { | ||
| const files = vfsFromEntries([['a.txt', 'x']]) | ||
| expect(grep(files, '(unclosed', undefined, { outputMode: 'content' })).toEqual([]) | ||
| }) | ||
|
|
||
| it('respects ignoreCase', () => { | ||
| const files = vfsFromEntries([['a.txt', 'Hello']]) | ||
| const hits = grep(files, 'hello', undefined, { outputMode: 'content', ignoreCase: true }) | ||
| expect(hits).toHaveLength(1) | ||
| }) | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.