|
| 1 | +import { createTestContext, newSimpleGit, SimpleGitTestContext } from '../__fixtures__'; |
| 2 | +import { grepQueryBuilder } from '../..'; |
| 3 | + |
| 4 | +describe('grep', () => { |
| 5 | + |
| 6 | + let context: SimpleGitTestContext; |
| 7 | + |
| 8 | + beforeEach(async () => { |
| 9 | + context = await createTestContext(); |
| 10 | + await setUpFiles(context); |
| 11 | + }); |
| 12 | + |
| 13 | + it('finds tracked files matching a string', async () => { |
| 14 | + const result = await newSimpleGit(context.root).grep('foo'); |
| 15 | + |
| 16 | + expect(result).toEqual({ |
| 17 | + paths: new Set(['foo/bar.txt']), |
| 18 | + results: { |
| 19 | + 'foo/bar.txt': [ |
| 20 | + {line: 4, path: 'foo/bar.txt', preview: ' foo/bar'}, |
| 21 | + ], |
| 22 | + }, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('finds tracked files matching multiple strings', async () => { |
| 27 | + // finds all instances of `line` when there is also either `one` or `two` on the line |
| 28 | + // ie: doesn't find `another line` |
| 29 | + const result = await newSimpleGit(context.root).grep( |
| 30 | + grepQueryBuilder('line').and('one', 'two') |
| 31 | + ); |
| 32 | + |
| 33 | + expect(result).toEqual({ |
| 34 | + paths: new Set(['a/aaa.txt', 'foo/bar.txt']), |
| 35 | + results: { |
| 36 | + 'a/aaa.txt': [ |
| 37 | + {line: 1, path: 'a/aaa.txt', preview: 'something on line one'}, |
| 38 | + {line: 2, path: 'a/aaa.txt', preview: 'this is line two'}, |
| 39 | + ], |
| 40 | + 'foo/bar.txt': [ |
| 41 | + {line: 1, path: 'foo/bar.txt', preview: 'something on line one'}, |
| 42 | + {line: 2, path: 'foo/bar.txt', preview: 'this is line two'}, |
| 43 | + ], |
| 44 | + }, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('finds multiple tracked files matching a string', async () => { |
| 49 | + const result = await newSimpleGit(context.root).grep('something'); |
| 50 | + |
| 51 | + expect(result).toEqual({ |
| 52 | + paths: new Set(['a/aaa.txt', 'foo/bar.txt']), |
| 53 | + results: { |
| 54 | + 'foo/bar.txt': [ |
| 55 | + {line: 1, path: 'foo/bar.txt', preview: 'something on line one'}, |
| 56 | + ], |
| 57 | + 'a/aaa.txt': [ |
| 58 | + {line: 1, path: 'a/aaa.txt', preview: 'something on line one'}, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('finds multiple tracked files matching any string', async () => { |
| 65 | + const result = await newSimpleGit(context.root).grep( |
| 66 | + grepQueryBuilder('something', 'foo') |
| 67 | + ); |
| 68 | + |
| 69 | + expect(result).toEqual({ |
| 70 | + paths: new Set(['a/aaa.txt', 'foo/bar.txt']), |
| 71 | + results: { |
| 72 | + 'foo/bar.txt': [ |
| 73 | + {line: 1, path: 'foo/bar.txt', preview: 'something on line one'}, |
| 74 | + {line: 4, path: 'foo/bar.txt', preview: ' foo/bar'}, |
| 75 | + ], |
| 76 | + 'a/aaa.txt': [ |
| 77 | + {line: 1, path: 'a/aaa.txt', preview: 'something on line one'}, |
| 78 | + ], |
| 79 | + }, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('can be used to find the matching lines count per file without line detail', async () => { |
| 84 | + const result = await newSimpleGit(context.root).grep('line', {'-c': null}); |
| 85 | + |
| 86 | + expect(result).toEqual({ |
| 87 | + paths: new Set(['a/aaa.txt', 'foo/bar.txt']), |
| 88 | + results: { |
| 89 | + 'foo/bar.txt': [ |
| 90 | + {line: 3, path: 'foo/bar.txt'}, |
| 91 | + ], |
| 92 | + 'a/aaa.txt': [ |
| 93 | + {line: 3, path: 'a/aaa.txt'}, |
| 94 | + ], |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('also finds untracked files on request', async () => { |
| 100 | + const result = await newSimpleGit(context.root).grep('foo', {'--untracked': null}); |
| 101 | + |
| 102 | + expect(result).toEqual({ |
| 103 | + paths: new Set(['foo/bar.txt', 'foo/baz.txt']), |
| 104 | + results: { |
| 105 | + 'foo/bar.txt': [ |
| 106 | + {line: 4, path: 'foo/bar.txt', preview: ' foo/bar'}, |
| 107 | + ], |
| 108 | + 'foo/baz.txt': [ |
| 109 | + {line: 4, path: 'foo/baz.txt', preview: ' foo/baz'}, |
| 110 | + ], |
| 111 | + }, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | +}); |
| 116 | + |
| 117 | +async function setUpFiles(context: SimpleGitTestContext) { |
| 118 | + const content = `something on line one\nthis is line two\n another line `; |
| 119 | + |
| 120 | + await context.git.init(); |
| 121 | + |
| 122 | + // tracked files |
| 123 | + await context.file(['foo', 'bar.txt'], `${content}\n foo/bar `); |
| 124 | + await context.file(['a', 'aaa.txt'], `${content}\n a/aaa `); |
| 125 | + await context.git.add('*'); |
| 126 | + |
| 127 | + // untracked files |
| 128 | + await context.file(['foo', 'baz.txt'], `${content}\n foo/baz `); |
| 129 | + await context.file(['a', 'bbb.txt'], `${content}\n a/bbb `); |
| 130 | +} |
0 commit comments