forked from prettier/pretty-quick
/
scm-git.test.js
263 lines (207 loc) · 6.66 KB
/
scm-git.test.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import mock from 'mock-fs';
import execa from 'execa';
import fs from 'fs';
import prettyQuick from '..';
jest.mock('execa');
afterEach(() => {
mock.restore();
jest.clearAllMocks();
});
const mockGitFs = (additionalUnstaged = '', additionalFiles = {}) => {
mock({
'/.git': {},
'/raz.js': 'raz()',
'/foo.js': 'foo()',
'/bar.md': '# foo',
...additionalFiles,
});
execa.sync.mockImplementation((command, args) => {
if (command !== 'git') {
throw new Error(`unexpected command: ${command}`);
}
switch (args[0]) {
case 'ls-files':
return { stdout: '' };
case 'diff':
return args[2] === '--cached'
? { stdout: './raz.js\n' }
: { stdout: './foo.js\n' + './bar.md\n' + additionalUnstaged };
case 'add':
return { stdout: '' };
default:
throw new Error(`unexpected arg0: ${args[0]}`);
}
});
};
describe('with git', () => {
test('calls `git merge-base`', () => {
mock({
'/.git': {},
});
prettyQuick('root');
expect(execa.sync).toHaveBeenCalledWith(
'git',
['merge-base', 'HEAD', 'master'],
{ cwd: '/' }
);
});
test('calls `git merge-base` with root git directory', () => {
mock({
'/.git': {},
'/other-dir': {},
});
prettyQuick('/other-dir');
expect(execa.sync).toHaveBeenCalledWith(
'git',
['merge-base', 'HEAD', 'master'],
{ cwd: '/' }
);
});
test('with --staged does NOT call `git merge-base`', () => {
mock({
'/.git': {},
});
prettyQuick('root');
expect(execa.sync).not.toHaveBeenCalledWith('git', [
'merge-base',
'HEAD',
'master',
]);
});
test('with --staged calls diff without revision', () => {
mock({
'/.git': {},
});
prettyQuick('root', { since: 'banana', staged: true });
expect(execa.sync).toHaveBeenCalledWith(
'git',
['diff', '--name-only', '--diff-filter=ACMRTUB'],
{ cwd: '/' }
);
});
test('calls `git diff --name-only` with revision', () => {
mock({
'/.git': {},
});
prettyQuick('root', { since: 'banana' });
expect(execa.sync).toHaveBeenCalledWith(
'git',
['diff', '--name-only', '--diff-filter=ACMRTUB', 'banana'],
{ cwd: '/' }
);
});
test('calls `git ls-files`', () => {
mock({
'/.git': {},
});
prettyQuick('root', { since: 'banana' });
expect(execa.sync).toHaveBeenCalledWith(
'git',
['ls-files', '--others', '--exclude-standard'],
{ cwd: '/' }
);
});
test('calls onFoundSinceRevision with return value from `git merge-base`', () => {
const onFoundSinceRevision = jest.fn();
mock({
'/.git': {},
});
execa.sync.mockReturnValue({ stdout: 'banana' });
prettyQuick('root', { onFoundSinceRevision });
expect(onFoundSinceRevision).toHaveBeenCalledWith('git', 'banana');
});
test('calls onFoundChangedFiles with changed files', () => {
const onFoundChangedFiles = jest.fn();
mockGitFs();
prettyQuick('root', { since: 'banana', onFoundChangedFiles });
expect(onFoundChangedFiles).toHaveBeenCalledWith(['./foo.js', './bar.md']);
});
test('calls onWriteFile with changed files', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', { since: 'banana', onWriteFile });
expect(onWriteFile).toHaveBeenCalledWith('./foo.js');
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
});
test('writes formatted files to disk', () => {
const onWriteFile = jest.fn();
mockGitFs();
prettyQuick('root', { since: 'banana', onWriteFile });
expect(fs.readFileSync('/foo.js', 'utf8')).toEqual('formatted:foo()');
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});
test('with --staged stages fully-staged files', () => {
mockGitFs();
prettyQuick('root', { since: 'banana', staged: true });
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './raz.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: '/',
});
});
test('with --staged AND --no-restage does not re-stage any files', () => {
mockGitFs();
prettyQuick('root', { since: 'banana', staged: true, restage: false });
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './raz.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: '/',
});
});
test('with --staged does not stage previously partially staged files AND aborts commit', () => {
const additionalUnstaged = './raz.js\n'; // raz.js is partly staged and partly not staged
mockGitFs(additionalUnstaged);
prettyQuick('root', { since: 'banana', staged: true });
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './raz.js'], {
cwd: '/',
});
});
test('without --staged does NOT stage changed files', () => {
mockGitFs();
prettyQuick('root', { since: 'banana' });
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: '/',
});
});
test('with --verbose calls onExamineFile', () => {
const onExamineFile = jest.fn();
mockGitFs();
prettyQuick('root', { since: 'banana', verbose: true, onExamineFile });
expect(onExamineFile).toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).toHaveBeenCalledWith('./bar.md');
});
test('without --verbose does NOT call onExamineFile', () => {
const onExamineFile = jest.fn();
mockGitFs();
prettyQuick('root', { since: 'banana', onExamineFile });
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
});
test('ignore files matching patterns from the repositories root .prettierignore', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
test('ignore files matching patterns from the working directories .prettierignore', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/sub-directory/.prettierignore': '*.md',
});
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
});