forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-controller.test.js
More file actions
323 lines (255 loc) · 13.9 KB
/
Copy pathcommit-controller.test.js
File metadata and controls
323 lines (255 loc) · 13.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import path from 'path';
import fs from 'fs-extra';
import React from 'react';
import {shallow} from 'enzyme';
import Commit from '../../lib/models/commit';
import {nullBranch} from '../../lib/models/branch';
import CommitController, {COMMIT_GRAMMAR_SCOPE} from '../../lib/controllers/commit-controller';
import {cloneRepository, buildRepository, buildRepositoryWithPipeline} from '../helpers';
describe('CommitController', function() {
let atomEnvironment, workspace, commandRegistry, notificationManager, lastCommit, config, confirm, tooltips;
let app;
beforeEach(function() {
atomEnvironment = global.buildAtomEnvironment();
workspace = atomEnvironment.workspace;
commandRegistry = atomEnvironment.commands;
notificationManager = atomEnvironment.notifications;
config = atomEnvironment.config;
tooltips = atomEnvironment.tooltips;
confirm = sinon.stub(atomEnvironment, 'confirm');
lastCommit = new Commit({sha: 'a1e23fd45', message: 'last commit message'});
const noop = () => {};
app = (
<CommitController
workspace={workspace}
grammars={atomEnvironment.grammars}
commandRegistry={commandRegistry}
tooltips={tooltips}
config={config}
notificationManager={notificationManager}
repository={{}}
isMerging={false}
mergeConflictsExist={false}
stagedChangesExist={false}
mergeMessage={''}
lastCommit={lastCommit}
currentBranch={nullBranch}
prepareToCommit={noop}
commit={noop}
abortMerge={noop}
/>
);
});
afterEach(function() {
atomEnvironment.destroy();
});
it('correctly updates state when switching repos', async function() {
const workdirPath1 = await cloneRepository('three-files');
const repository1 = await buildRepository(workdirPath1);
const workdirPath2 = await cloneRepository('three-files');
const repository2 = await buildRepository(workdirPath2);
app = React.cloneElement(app, {repository: repository1});
const wrapper = shallow(app, {disableLifecycleMethods: true});
assert.strictEqual(wrapper.instance().getCommitMessage(), '');
wrapper.instance().setCommitMessage('message 1');
wrapper.setProps({repository: repository2});
assert.strictEqual(wrapper.instance().getCommitMessage(), '');
wrapper.setProps({repository: repository1});
assert.equal(wrapper.instance().getCommitMessage(), 'message 1');
});
describe('the passed commit message', function() {
let repository;
beforeEach(async function() {
const workdirPath = await cloneRepository('three-files');
repository = await buildRepository(workdirPath);
app = React.cloneElement(app, {repository});
});
it('is set to the getCommitMessage() in the default case', function() {
repository.setCommitMessage('some message');
const wrapper = shallow(app, {disableLifecycleMethods: true});
assert.strictEqual(wrapper.find('CommitView').prop('message'), 'some message');
});
it('does not cause the repository to update when commit message changes', function() {
repository.setCommitMessage('some message');
const wrapper = shallow(app, {disableLifecycleMethods: true}).instance();
sinon.spy(wrapper.props.repository.state, 'didUpdate');
assert.strictEqual(wrapper.getCommitMessage(), 'some message');
wrapper.handleMessageChange('new message');
assert.strictEqual(wrapper.getCommitMessage(), 'new message');
assert.isFalse(wrapper.props.repository.state.didUpdate.called);
});
describe('when a merge message is defined', function() {
it('is set to the merge message when merging', function() {
app = React.cloneElement(app, {isMerging: true, mergeMessage: 'merge conflict!'});
const wrapper = shallow(app, {disableLifecycleMethods: true});
assert.strictEqual(wrapper.find('CommitView').prop('message'), 'merge conflict!');
});
it('is set to getCommitMessage() if it is set', function() {
repository.setCommitMessage('some commit message');
app = React.cloneElement(app, {isMerging: true, mergeMessage: 'merge conflict!'});
const wrapper = shallow(app, {disableLifecycleMethods: true});
assert.strictEqual(wrapper.find('CommitView').prop('message'), 'some commit message');
});
});
});
describe('committing', function() {
let workdirPath, repository;
beforeEach(async function() {
workdirPath = await cloneRepository('three-files');
repository = await buildRepositoryWithPipeline(workdirPath, {confirm, notificationManager, workspace});
const commit = message => repository.commit(message);
app = React.cloneElement(app, {repository, commit});
});
it('clears the commit messages', async function() {
repository.setCommitMessage('a message');
await fs.writeFile(path.join(workdirPath, 'a.txt'), 'some changes', {encoding: 'utf8'});
await repository.git.exec(['add', '.']);
const wrapper = shallow(app, {disableLifecycleMethods: true});
await wrapper.instance().commit('another message');
assert.strictEqual(repository.getCommitMessage(), '');
});
it('issues a notification on failure', async function() {
repository.setCommitMessage('some message');
sinon.spy(notificationManager, 'addError');
const wrapper = shallow(app, {disableLifecycleMethods: true});
// Committing with no staged changes should cause commit error
try {
await wrapper.instance().commit('message');
} catch (e) {
assert(e, 'is error');
}
assert.isTrue(notificationManager.addError.called);
assert.strictEqual(repository.getCommitMessage(), 'some message');
});
describe('message formatting', function() {
let commitSpy;
beforeEach(function() {
commitSpy = sinon.stub().returns(Promise.resolve());
app = React.cloneElement(app, {commit: commitSpy});
});
it('wraps the commit message body at 72 characters if github.automaticCommitMessageWrapping is true', async function() {
config.set('github.automaticCommitMessageWrapping', false);
const wrapper = shallow(app, {disableLifecycleMethods: true});
await wrapper.instance().commit([
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
].join('\n'));
assert.deepEqual(commitSpy.args[0][0].split('\n'), [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
]);
commitSpy.reset();
config.set('github.automaticCommitMessageWrapping', true);
await wrapper.instance().commit([
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
].join('\n'));
assert.deepEqual(commitSpy.args[0][0].split('\n'), [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ',
'ut aliquip ex ea commodo consequat.',
]);
});
});
describe('toggling between commit box and commit editor', function() {
it('transfers the commit message contents of the last editor', async function() {
const wrapper = shallow(app, {disableLifecycleMethods: true});
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')('message in box');
await assert.async.equal(workspace.getActiveTextEditor().getPath(), wrapper.instance().getCommitMessagePath());
wrapper.update();
assert.isTrue(wrapper.find('CommitView').prop('deactivateCommitBox'));
const editor = workspace.getActiveTextEditor();
assert.strictEqual(editor.getText(), 'message in box');
editor.setText('message in editor');
await editor.save();
workspace.paneForItem(editor).splitRight({copyActiveItem: true});
await assert.async.notEqual(workspace.getActiveTextEditor(), editor);
editor.destroy();
assert.isTrue(wrapper.find('CommitView').prop('deactivateCommitBox'));
workspace.getActiveTextEditor().destroy();
assert.isTrue(wrapper.find('CommitView').prop('deactivateCommitBox'));
await assert.async.strictEqual(wrapper.update().find('CommitView').prop('message'), 'message in editor');
});
it('activates editor if already opened but in background', async function() {
const wrapper = shallow(app, {disableLifecycleMethods: true});
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')('sup');
await assert.async.strictEqual(workspace.getActiveTextEditor().getPath(), wrapper.instance().getCommitMessagePath());
const editor = workspace.getActiveTextEditor();
await workspace.open(path.join(workdirPath, 'a.txt'));
workspace.getActivePane().splitRight();
await workspace.open(path.join(workdirPath, 'b.txt'));
assert.notStrictEqual(workspace.getActiveTextEditor(), editor);
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')();
await assert.async.strictEqual(workspace.getActiveTextEditor(), editor);
});
it('closes all open commit message editors if one is in the foreground of a pane, prompting for unsaved changes', async function() {
const wrapper = shallow(app, {disableLifecycleMethods: true});
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')('sup');
await assert.async.strictEqual(workspace.getActiveTextEditor().getPath(), wrapper.instance().getCommitMessagePath());
const editor = workspace.getActiveTextEditor();
workspace.paneForItem(editor).splitRight({copyActiveItem: true});
assert.lengthOf(wrapper.instance().getCommitMessageEditors(), 2);
// Activate another editor but keep commit message editor in foreground of inactive pane
await workspace.open(path.join(workdirPath, 'a.txt'));
assert.notStrictEqual(workspace.getActiveTextEditor(), editor);
editor.setText('make some new changes');
// atom internals calls `confirm` on the ApplicationDelegate instead of the atom environment
sinon.stub(atomEnvironment.applicationDelegate, 'confirm').callsFake((options, callback) => {
if (typeof callback === 'function') {
callback(0); // Save
}
return 0; // TODO: Remove this return and typeof check once https://github.com/atom/atom/pull/16229 is on stable
});
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')();
await assert.async.lengthOf(wrapper.instance().getCommitMessageEditors(), 0);
assert.isTrue(atomEnvironment.applicationDelegate.confirm.called);
await assert.async.strictEqual(wrapper.update().find('CommitView').prop('message'), 'make some new changes');
});
});
describe('committing from commit editor', function() {
it('uses git commit grammar in the editor', async function() {
const wrapper = shallow(app, {disableLifecycleMethods: true});
await atomEnvironment.packages.activatePackage('language-git');
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')('sup');
await assert.async.strictEqual(workspace.getActiveTextEditor().getGrammar().scopeName, COMMIT_GRAMMAR_SCOPE);
});
it('takes the commit message from the editor and deletes the `ATOM_COMMIT_EDITMSG` file', async function() {
fs.writeFileSync(path.join(workdirPath, 'a.txt'), 'some changes');
await repository.stageFiles(['a.txt']);
app = React.cloneElement(app, {prepareToCommit: () => true, stagedChangesExist: true});
const wrapper = shallow(app, {disableLifecycleMethods: true});
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')();
await assert.async.strictEqual(workspace.getActiveTextEditor().getPath(), wrapper.instance().getCommitMessagePath());
const editor = workspace.getActiveTextEditor();
editor.setText('message in editor');
await editor.save();
wrapper.find('CommitView').prop('commit')('message in box');
await assert.async.strictEqual((await repository.getLastCommit()).getMessageSubject(), 'message in editor');
await assert.async.isFalse(fs.existsSync(wrapper.instance().getCommitMessagePath()));
});
it('asks user to confirm if commit editor has unsaved changes', async function() {
app = React.cloneElement(app, {confirm, prepareToCommit: () => true, stagedChangesExist: true});
const wrapper = shallow(app, {disableLifecycleMethods: true});
sinon.stub(repository.git, 'commit');
wrapper.find('CommitView').prop('toggleExpandedCommitMessageEditor')();
await assert.async.strictEqual(workspace.getActiveTextEditor().getPath(), wrapper.instance().getCommitMessagePath());
const editor = workspace.getActiveTextEditor();
editor.setText('unsaved changes');
workspace.paneForItem(editor).splitRight({copyActiveItem: true});
await assert.async.notStrictEqual(workspace.getActiveTextEditor(), editor);
assert.lengthOf(workspace.getTextEditors(), 2);
confirm.returns(1); // Cancel
wrapper.find('CommitView').prop('commit')('message in box');
assert.strictEqual(repository.git.commit.callCount, 0);
confirm.returns(0); // Commit
wrapper.find('CommitView').prop('commit')('message in box');
await assert.async.equal(repository.git.commit.callCount, 1);
await assert.async.lengthOf(workspace.getTextEditors(), 0);
});
});
});
});