-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
grep.spec.ts
181 lines (151 loc) · 6.34 KB
/
grep.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { promiseError } from '@kwsites/promise-result';
import {
assertExecutedCommands,
assertGitError,
closeWithSuccess,
newSimpleGit,
} from './__fixtures__';
import { grepQueryBuilder, TaskConfigurationError } from '../..';
import { NULL } from '../../src/lib/utils';
import { pathspec } from '../../src/lib/args/pathspec';
describe('grep', () => {
describe('grepQueryBuilder', () => {
it('-e NODE -e Unexpected', () => {
expect(Array.from(grepQueryBuilder('NODE', 'Unexpected'))).toEqual([
'-e',
'NODE',
'-e',
'Unexpected',
]);
});
it('-e #define --and ( -e MAX_PATH -e PATH_MAX )', () => {
let query = grepQueryBuilder('#define').and('MAX_PATH', 'PATH_MAX');
expect(Array.from(query)).toEqual([
'-e',
'#define',
'--and',
'(',
'-e',
'MAX_PATH',
'-e',
'PATH_MAX',
')',
]);
});
});
describe('usage', () => {
const callback = jest.fn();
afterEach(() => callback.mockReset());
it('prevents using -h as an option', async () => {
const result = await promiseError(newSimpleGit().grep('hello', ['-h']));
assertGitError(result, 'git.grep: use of "-h" is not supported', TaskConfigurationError);
});
it('single term with callback', async () => {
const queue = newSimpleGit().grep('foo', callback);
await closeWithSuccess(`file.txt${NULL}2`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '-e', 'foo');
expect(callback).toHaveBeenCalledWith(null, await queue);
});
it('single term with options object and callback', async () => {
const queue = newSimpleGit().grep('foo', { '--foo': 'bar' }, callback);
await closeWithSuccess(`file.txt${NULL}2`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '--foo=bar', '-e', 'foo');
expect(callback).toHaveBeenCalledWith(null, await queue);
});
it('single term with options array and callback', async () => {
const queue = newSimpleGit().grep('foo', ['boo'], callback);
await closeWithSuccess(`file.txt${NULL}2`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', 'boo', '-e', 'foo');
expect(callback).toHaveBeenCalledWith(null, await queue);
});
it('awaits single term with options array', async () => {
const queue = newSimpleGit().grep('foo', ['--bar']);
await closeWithSuccess(`file.txt${NULL}2`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '--bar', '-e', 'foo');
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('awaits single term with options object', async () => {
const queue = newSimpleGit().grep('foo', { '-c': null });
await closeWithSuccess(`file.txt${NULL}2`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '-c', '-e', 'foo');
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('awaits single search term', async () => {
const queue = newSimpleGit().grep('foo');
await closeWithSuccess(`
path/to/file.txt${NULL}2${NULL}some foo content
another/file.txt${NULL}4${NULL}food content
`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '-e', 'foo');
expect(await queue).toEqual({
paths: new Set(['path/to/file.txt', 'another/file.txt']),
results: {
'path/to/file.txt': [
{ line: 2, path: 'path/to/file.txt', preview: 'some foo content' },
],
'another/file.txt': [{ line: 4, path: 'another/file.txt', preview: 'food content' }],
},
});
});
it('awaits multiple search terms', async () => {
const queue = newSimpleGit().grep(grepQueryBuilder('a', 'b'));
await closeWithSuccess(`file.txt${NULL}2${NULL}some foo content`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '-e', 'a', '-e', 'b');
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('awaits multiple search terms with options object', async () => {
const queue = newSimpleGit().grep(grepQueryBuilder('a', 'b'), { '--c': null });
await closeWithSuccess(`file.txt${NULL}2${NULL}some foo content`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '--c', '-e', 'a', '-e', 'b');
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('awaits multiple search terms with options array', async () => {
const queue = newSimpleGit().grep(grepQueryBuilder('a', 'b'), ['--c']);
await closeWithSuccess(`file.txt${NULL}2${NULL}some foo content`);
assertExecutedCommands('grep', '--null', '-n', '--full-name', '--c', '-e', 'a', '-e', 'b');
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('appends paths provided as a pathspec in array TaskOptions', async () => {
const queue = newSimpleGit().grep(grepQueryBuilder('a', 'b'), [
pathspec('path/to'),
'--c',
]);
await closeWithSuccess(`file.txt${NULL}2${NULL}some foo content`);
assertExecutedCommands(
'grep',
'--null',
'-n',
'--full-name',
'--c',
'-e',
'a',
'-e',
'b',
'--',
'path/to'
);
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
it('appends paths provided as a pathspec in object TaskOptions', async () => {
const queue = newSimpleGit().grep(grepQueryBuilder('a', 'b'), {
'--c': null,
'paths': pathspec('path/to'),
});
await closeWithSuccess(`file.txt${NULL}2${NULL}some foo content`);
assertExecutedCommands(
'grep',
'--null',
'-n',
'--full-name',
'--c',
'-e',
'a',
'-e',
'b',
'--',
'path/to'
);
expect(await queue).toHaveProperty('paths', new Set(['file.txt']));
});
});
});