Repository navigation
Expand file tree
/
Copy pathcommitOps.test.js
More file actions
245 lines (210 loc) · 8.88 KB
/
Copy pathcommitOps.test.js
File metadata and controls
245 lines (210 loc) · 8.88 KB
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
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const {
commitArgs,
commitConfirmation,
commitMenu,
stepTo,
} = require('../out/git/commitOps.js');
const commit = (extra) =>
Object.assign(
{ hash: 'a'.repeat(40), shortHash: 'aaaaaaa', subject: 'Fix the badge', parents: ['b'.repeat(40)] },
extra
);
const merge = () => commit({ parents: ['b'.repeat(40), 'c'.repeat(40)] });
const root = () => commit({ parents: [] });
const ctx = (target, extra) => Object.assign({ target, current: 'main' }, extra);
const opsOf = (menu) => menu.map((e) => e.op);
const find = (menu, op) => menu.find((e) => e.op === op);
// ------------------------------------------------------------------ commands
test('a commit is checked out detached, by hash', () => {
assert.deepEqual(commitArgs('checkoutDetach', ctx(commit())), [
'checkout',
'--detach',
'a'.repeat(40),
]);
});
test('a branch or tag made here needs a name first', () => {
assert.deepEqual(commitArgs('newBranch', ctx(commit(), { name: 'fix/1' })), [
'branch',
'fix/1',
'a'.repeat(40),
]);
assert.equal(commitArgs('newBranch', ctx(commit())), null);
assert.equal(commitArgs('newTag', ctx(commit())), null);
});
test('a tag with something to say is annotated, one without is not', () => {
// -m on its own would record a tag object with an empty message.
assert.deepEqual(commitArgs('newTag', ctx(commit(), { name: 'v1.0' })), [
'tag',
'v1.0',
'a'.repeat(40),
]);
assert.deepEqual(
commitArgs('newTag', ctx(commit(), { name: 'v1.0', message: 'First release' })),
['tag', '-a', 'v1.0', '-m', 'First release', 'a'.repeat(40)]
);
});
test('reverting and cherry-picking a merge say which side to take', () => {
// A merge has no single "before"; -m 1 means as it looked from the branch it
// was merged into.
assert.deepEqual(commitArgs('revert', ctx(merge())), [
'revert',
'--no-edit',
'-m',
'1',
'a'.repeat(40),
]);
assert.deepEqual(commitArgs('cherryPick', ctx(merge())), [
'cherry-pick',
'-m',
'1',
'a'.repeat(40),
]);
});
test('an ordinary commit needs no such thing', () => {
assert.deepEqual(commitArgs('revert', ctx(commit())), ['revert', '--no-edit', 'a'.repeat(40)]);
assert.deepEqual(commitArgs('cherryPick', ctx(commit())), ['cherry-pick', 'a'.repeat(40)]);
});
test('a worktree made from a commit is detached, having no branch to be on', () => {
assert.deepEqual(commitArgs('newWorktree', ctx(commit(), { path: 'D:/wt/x' })), [
'worktree',
'add',
'--detach',
'D:/wt/x',
'a'.repeat(40),
]);
});
test("the window's own entries have no git command", () => {
for (const op of ['viewDetails', 'compare', 'goToChild', 'goToParent', 'refresh', 'outgoingOnly', 'copyId', 'viewPatch']) {
assert.equal(commitArgs(op, ctx(commit())), null, op);
}
});
// ------------------------------------------------------------- confirmations
test('what changes nothing is not worth asking about', () => {
for (const op of ['viewDetails', 'newBranch', 'newTag', 'copyId', 'refresh']) {
assert.equal(commitConfirmation(op, ctx(commit())), null, op);
}
});
test('what rewrites or discards asks first, and names the commit', () => {
for (const op of ['revert', 'cherryPick', 'resetSoft', 'resetMixed', 'resetHard', 'checkoutDetach']) {
const ask = commitConfirmation(op, ctx(commit()));
assert.ok(ask, op + ' should confirm');
assert.match(ask.message, /aaaaaaa/, op + ' should name the commit');
}
});
test('only the hard reset is marked destructive', () => {
assert.equal(commitConfirmation('resetHard', ctx(commit())).destructive, true);
assert.match(commitConfirmation('resetHard', ctx(commit())).detail, /cannot bring it back/);
for (const op of ['revert', 'cherryPick', 'resetSoft', 'resetMixed', 'checkoutDetach']) {
assert.ok(!commitConfirmation(op, ctx(commit())).destructive, op + ' is recoverable');
}
});
test('a revert says the original commit stays', () => {
assert.match(commitConfirmation('revert', ctx(commit())).detail, /left in place/);
});
test('a detached checkout explains what detached means', () => {
assert.match(commitConfirmation('checkoutDetach', ctx(commit())).detail, /not be on a branch/);
});
// --------------------------------------------------------------------- menu
test('the menu offers what Visual Studio does, less what we cannot do', () => {
const ops = opsOf(commitMenu(commit(), { current: 'main' }));
for (const op of [
'checkoutDetach', 'viewDetails', 'compare', 'newBranch', 'newTag', 'revert',
'resetSoft', 'resetMixed', 'resetHard', 'cherryPick', 'goToChild',
'goToParent', 'refresh', 'outgoingOnly', 'newWorktree',
]) {
assert.ok(ops.includes(op), 'missing ' + op);
}
});
test('nothing is offered that this extension cannot do', () => {
// "Add to Chat", "Review Commit" and "Squash Commits…" are Visual Studio's
// and not ours; a menu that offers what it cannot deliver is worse than a
// shorter one.
const labels = commitMenu(commit()).map((e) => e.label.toLowerCase());
for (const absent of ['chat', 'review', 'squash']) {
assert.ok(!labels.some((l) => l.includes(absent)), absent + ' should not be offered');
}
});
test('the root commit disables what needs a commit before it', () => {
const menu = commitMenu(root(), { current: 'main' });
for (const op of ['revert', 'cherryPick', 'goToParent']) {
assert.equal(find(menu, op).disabled, true, op);
assert.ok(find(menu, op).why, op + ' says why');
}
assert.ok(!find(menu, 'newBranch').disabled, 'but a branch can still start here');
});
test('the commit HEAD is on cannot be checked out or picked again', () => {
const menu = commitMenu(commit({ isHead: true }), { current: 'main' });
assert.equal(find(menu, 'checkoutDetach').disabled, true);
assert.match(find(menu, 'checkoutDetach').why, /already here/);
assert.equal(find(menu, 'cherryPick').disabled, true);
});
test('a merge says which parent it will act against', () => {
const menu = commitMenu(merge(), { current: 'main' });
assert.match(find(menu, 'revert').label, /first parent/);
assert.match(find(menu, 'cherryPick').label, /first parent/);
assert.ok(!find(menu, 'revert').disabled, 'and it is still allowed');
});
test('the outgoing filter shows whether it is on', () => {
assert.equal(find(commitMenu(commit(), { current: 'main' }), 'outgoingOnly').checked, false);
assert.equal(
find(commitMenu(commit(), { current: 'main', filtered: true }), 'outgoingOnly').checked,
true
);
});
test('a detached HEAD has no upstream to filter against', () => {
const menu = commitMenu(commit(), {});
assert.equal(find(menu, 'outgoingOnly').disabled, true);
assert.match(find(menu, 'outgoingOnly').why, /detached/);
});
test('the two that have keyboard shortcuts say so', () => {
const menu = commitMenu(commit(), { current: 'main' });
assert.equal(find(menu, 'goToChild').shortcut, 'Alt+PgUp');
assert.equal(find(menu, 'goToParent').shortcut, 'Alt+PgDn');
});
test('every entry can be acted on', () => {
const handled = ['viewDetails', 'compare', 'goToChild', 'goToParent', 'refresh', 'outgoingOnly', 'copyId', 'viewPatch'];
for (const entry of commitMenu(commit(), { current: 'main' })) {
const args = commitArgs(entry.op, ctx(commit(), { name: 'n', path: 'p' }));
assert.ok(args !== null || handled.includes(entry.op), entry.op + ' does nothing');
}
});
test('every disabled entry gives a reason', () => {
for (const target of [root(), commit({ isHead: true }), merge()]) {
for (const entry of commitMenu(target, {})) {
if (entry.disabled) {
assert.ok(entry.why, entry.op + ' is disabled without saying why');
}
}
}
});
// ------------------------------------------------------------- walking the graph
const rows = [
{ hash: 'c3', parents: ['c2'] },
{ hash: 'c2', parents: ['c1'] },
{ hash: 'c1', parents: [] },
];
test('the parent is the first one, so a merge follows the line being read', () => {
assert.equal(stepTo('parent', 'c3', rows), 'c2');
const merged = [{ hash: 'm', parents: ['main-side', 'feature-side'] }];
assert.equal(stepTo('parent', 'm', merged), 'main-side');
});
test('a child has to be searched for, because a commit does not record one', () => {
assert.equal(stepTo('child', 'c2', rows), 'c3');
assert.equal(stepTo('child', 'c1', rows), 'c2');
});
test('the newest child is taken, so repeated steps walk back the way in', () => {
const forked = [
{ hash: 'newer', parents: ['base'] },
{ hash: 'older', parents: ['base'] },
{ hash: 'base', parents: [] },
];
assert.equal(stepTo('child', 'base', forked), 'newer');
});
test('nowhere to go reports nothing rather than staying put silently', () => {
assert.equal(stepTo('parent', 'c1', rows), null, 'the root has no parent');
assert.equal(stepTo('child', 'c3', rows), null, 'the newest has no child');
assert.equal(stepTo('parent', 'missing', rows), null);
});