forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-package.test.js
More file actions
387 lines (312 loc) · 18.4 KB
/
Copy pathgithub-package.test.js
File metadata and controls
387 lines (312 loc) · 18.4 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import {Directory} from 'atom';
import fs from 'fs';
import path from 'path';
import temp from 'temp';
import until from 'test-until';
import {cloneRepository} from './helpers';
import GithubPackage from '../lib/github-package';
describe('GithubPackage', function() {
let atomEnv, workspace, project, commandRegistry, notificationManager, config, confirm, tooltips, styles;
let githubPackage;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
project = atomEnv.project;
commandRegistry = atomEnv.commands;
notificationManager = atomEnv.notifications;
tooltips = atomEnv.tooltips;
config = atomEnv.config;
confirm = atomEnv.confirm.bind(atomEnv);
styles = atomEnv.styles;
githubPackage = new GithubPackage(
workspace, project, commandRegistry, notificationManager, tooltips, styles, config, confirm,
);
});
afterEach(async function() {
await githubPackage.deactivate();
atomEnv.destroy();
});
describe('activate()', function() {
it('updates the active repository', async function() {
await githubPackage.activate();
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
project.setPaths([workdirPath1, workdirPath2]);
fs.writeFileSync(path.join(workdirPath1, 'a.txt'), 'change 1', 'utf8');
fs.writeFileSync(path.join(workdirPath1, 'b.txt'), 'change 2', 'utf8');
await workspace.open(path.join(workdirPath1, 'a.txt'));
const repository = await githubPackage.getRepositoryForWorkdirPath(workdirPath1);
await until(() => githubPackage.getActiveRepository() === repository);
});
});
describe('changing the project paths', function() {
it('updates the active repository and project path', async function() {
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
const nonRepositoryPath = fs.realpathSync(temp.mkdirSync());
fs.writeFileSync(path.join(nonRepositoryPath, 'c.txt'));
project.setPaths([workdirPath1, workdirPath2, nonRepositoryPath]);
fs.writeFileSync(path.join(workdirPath1, 'a.txt'), 'change 1', 'utf8');
await githubPackage.activate();
sinon.spy(githubPackage, 'rerender');
await workspace.open(path.join(workdirPath1, 'a.txt'));
const repository1 = await githubPackage.getRepositoryForWorkdirPath(workdirPath1);
await assert.async.strictEqual(githubPackage.getActiveRepository(), repository1);
await assert.async.equal(githubPackage.getActiveProjectPath(), workdirPath1);
await assert.async.equal(githubPackage.rerender.callCount, 1);
// Remove repository for open file
project.setPaths([workdirPath2, nonRepositoryPath]);
await assert.async.equal(githubPackage.getActiveProjectPath(), workdirPath1);
await assert.async.isNull(githubPackage.getActiveRepository());
await assert.async.equal(githubPackage.rerender.callCount, 2);
await workspace.open(path.join(workdirPath2, 'b.txt'));
const repository2 = await githubPackage.getRepositoryForWorkdirPath(workdirPath2);
await assert.async.strictEqual(githubPackage.getActiveRepository(), repository2);
await assert.async.equal(githubPackage.getActiveProjectPath(), workdirPath2);
await assert.async.equal(githubPackage.rerender.callCount, 3);
await workspace.open(path.join(nonRepositoryPath, 'c.txt'));
await assert.async.isNull(githubPackage.getActiveRepository());
await assert.async.equal(githubPackage.getActiveProjectPath(), nonRepositoryPath);
await assert.async.equal(githubPackage.rerender.callCount, 4);
});
it('destroys all the repositories associated with the removed project folders', async function() {
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
const workdirPath3 = await cloneRepository('three-files');
project.setPaths([workdirPath1, workdirPath2, workdirPath3]);
await githubPackage.activate();
await githubPackage.getInitialModelsPromise();
const repository1 = await githubPackage.getRepositoryForWorkdirPath(workdirPath1);
const repository2 = await githubPackage.getRepositoryForWorkdirPath(workdirPath2);
const repository3 = await githubPackage.getRepositoryForWorkdirPath(workdirPath3);
assert.isOk(repository1);
assert.isOk(repository2);
assert.isOk(repository3);
sinon.stub(repository1, 'destroy');
sinon.stub(repository2, 'destroy');
sinon.stub(repository3, 'destroy');
project.removePath(workdirPath1);
project.removePath(workdirPath3);
await until(() => repository1.destroy.callCount === 1, 'repository1 is destroyed');
await until(() => repository3.destroy.callCount === 1, 'repository3 is destroyed');
assert.equal(repository2.destroy.callCount, 0);
assert.notEqual(await githubPackage.getRepositoryForWorkdirPath(repository1.getWorkingDirectoryPath()), repository1);
assert.notEqual(await githubPackage.getRepositoryForWorkdirPath(repository3.getWorkingDirectoryPath()), repository3);
assert.equal(await githubPackage.getRepositoryForWorkdirPath(repository2.getWorkingDirectoryPath()), repository2);
});
});
describe('didChangeActivePaneItem()', function() {
it('updates the active repository', async function() {
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
project.setPaths([workdirPath1, workdirPath2]);
fs.writeFileSync(path.join(workdirPath1, 'a.txt'), 'change 1', 'utf8');
fs.writeFileSync(path.join(workdirPath2, 'b.txt'), 'change 2', 'utf8');
await workspace.open(path.join(workdirPath1, 'a.txt'));
githubPackage.didChangeActivePaneItem();
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath1));
await workspace.open(path.join(workdirPath2, 'b.txt'));
githubPackage.didChangeActivePaneItem();
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath2));
});
});
describe('updateActiveModels()', function() {
it('updates the active repository based on the active item, setting it to null when the active item is not in a project repository', async function() {
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
const nonRepositoryPath = fs.realpathSync(temp.mkdirSync());
fs.writeFileSync(path.join(nonRepositoryPath, 'c.txt'));
project.setPaths([workdirPath1, workdirPath2, nonRepositoryPath]);
await githubPackage.activate();
await workspace.open(path.join(workdirPath1, 'a.txt'));
await workspace.open(path.join(workdirPath2, 'b.txt'));
await githubPackage.updateActiveModels();
assert.isNotNull(githubPackage.getActiveRepository());
assert.equal(githubPackage.getActiveProjectPath(), workdirPath2);
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath2));
await workspace.open(path.join(nonRepositoryPath, 'c.txt'));
await githubPackage.updateActiveModels();
assert.equal(githubPackage.getActiveProjectPath(), nonRepositoryPath);
assert.isNull(githubPackage.getActiveRepository());
await workspace.open(path.join(workdirPath1, 'a.txt'));
await githubPackage.updateActiveModels();
assert.equal(githubPackage.getActiveProjectPath(), workdirPath1);
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath1));
workspace.getActivePane().activateItem({}); // such as when find & replace results pane is focused
await githubPackage.updateActiveModels();
assert.equal(githubPackage.getActiveProjectPath(), workdirPath1);
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath1));
await workspace.open(path.join(workdirPath2, 'b.txt'));
await githubPackage.updateActiveModels();
assert.equal(githubPackage.getActiveProjectPath(), workdirPath2);
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(workdirPath2));
project.removePath(workdirPath2);
await githubPackage.updateActiveModels();
assert.isNull(githubPackage.getActiveProjectPath());
assert.isNull(githubPackage.getActiveRepository());
project.removePath(workdirPath1);
await githubPackage.updateActiveModels();
assert.isNull(githubPackage.getActiveProjectPath());
assert.isNull(githubPackage.getActiveRepository());
await workspace.open(path.join(workdirPath1, 'a.txt'));
await githubPackage.updateActiveModels();
assert.isNull(githubPackage.getActiveProjectPath());
assert.isNull(githubPackage.getActiveRepository());
});
it('defaults to a single repository even without an active pane item', async function() {
const workdirPath = await cloneRepository('three-files');
project.setPaths([workdirPath]);
await githubPackage.activate();
await githubPackage.updateActiveModels();
const repository = await githubPackage.getRepositoryForWorkdirPath(workdirPath);
await assert.async.strictEqual(githubPackage.getActiveRepository(), repository);
});
it('updates the active resolution progress', async function() {
// Repository with a merge conflict, repository without a merge conflict, path without a repository
const workdirMergeConflict = await cloneRepository('merge-conflict');
const workdirNoConflict = await cloneRepository('three-files');
const nonRepositoryPath = fs.realpathSync(temp.mkdirSync());
fs.writeFileSync(path.join(nonRepositoryPath, 'c.txt'));
project.setPaths([workdirMergeConflict, workdirNoConflict, nonRepositoryPath]);
await githubPackage.activate();
await githubPackage.getInitialModelsPromise();
// Open a file in the merge conflict repository.
await workspace.open(path.join(workdirMergeConflict, 'modified-on-both-ours.txt'));
await githubPackage.updateActiveModels();
const resolutionMergeConflict = await githubPackage.getResolutionProgressForWorkdirPath(workdirMergeConflict);
await assert.strictEqual(githubPackage.getActiveResolutionProgress(), resolutionMergeConflict);
// Record some resolution progress to recall later
resolutionMergeConflict.reportMarkerCount('modified-on-both-ours.txt', 3);
// Open a file in the non-merge conflict repository.
await workspace.open(path.join(workdirNoConflict, 'b.txt'));
await githubPackage.updateActiveModels();
const resolutionNoConflict = await githubPackage.getResolutionProgressForWorkdirPath(workdirNoConflict);
await assert.strictEqual(githubPackage.getActiveResolutionProgress(), resolutionNoConflict);
assert.isTrue(githubPackage.getActiveResolutionProgress().isEmpty());
// Open a file in the workdir with no repository.
await workspace.open(path.join(nonRepositoryPath, 'c.txt'));
await githubPackage.updateActiveModels();
await assert.isNull(githubPackage.getActiveResolutionProgress());
// Re-open a file in the merge conflict repository.
await workspace.open(path.join(workdirMergeConflict, 'modified-on-both-theirs.txt'));
await githubPackage.updateActiveModels();
await assert.strictEqual(githubPackage.getActiveResolutionProgress(), resolutionMergeConflict);
assert.isFalse(githubPackage.getActiveResolutionProgress().isEmpty());
assert.equal(githubPackage.getActiveResolutionProgress().getRemaining('modified-on-both-ours.txt'), 3);
});
// Don't worry about this on Windows as it's not a common op
if (process.platform !== 'win32') {
it('handles symlinked project paths', async () => {
const workdirPath = await cloneRepository('three-files');
const symlinkPath = fs.realpathSync(temp.mkdirSync()) + '-symlink';
fs.symlinkSync(workdirPath, symlinkPath);
project.setPaths([symlinkPath]);
await githubPackage.activate();
await workspace.open(path.join(symlinkPath, 'a.txt'));
await githubPackage.updateActiveModels();
assert.isOk(githubPackage.getActiveRepository());
});
}
});
describe('when there is a change in the repository', function() {
let workdirPath1, atomGitRepository1, repository1;
let workdirPath2, atomGitRepository2, repository2;
beforeEach(async function() {
workdirPath1 = await cloneRepository('three-files');
workdirPath2 = await cloneRepository('three-files');
fs.writeFileSync(path.join(workdirPath1, 'c.txt'), 'ch-ch-ch-changes', 'utf8');
fs.writeFileSync(path.join(workdirPath2, 'c.txt'), 'ch-ch-ch-changes', 'utf8');
project.setPaths([workdirPath1, workdirPath2]);
await githubPackage.activate();
await githubPackage.getInitialWatchersStartedPromise();
[atomGitRepository1, atomGitRepository2] = githubPackage.project.getRepositories();
sinon.stub(atomGitRepository1, 'refreshStatus');
sinon.stub(atomGitRepository2, 'refreshStatus');
repository1 = await githubPackage.getRepositoryForWorkdirPath(workdirPath1);
repository2 = await githubPackage.getRepositoryForWorkdirPath(workdirPath2);
sinon.stub(repository1, 'refresh');
sinon.stub(repository2, 'refresh');
});
it('refreshes the appropriate Repository and Atom GitRepository when a file is changed in workspace 1', async function() {
if (process.platform === 'linux') {
this.skip();
}
fs.writeFileSync(path.join(workdirPath1, 'a.txt'), 'some changes', 'utf8');
await assert.async.isTrue(repository1.refresh.called);
await assert.async.isTrue(atomGitRepository1.refreshStatus.called);
});
it('refreshes the appropriate Repository and Atom GitRepository when a file is changed in workspace 2', async function() {
if (process.platform === 'linux') {
this.skip();
}
fs.writeFileSync(path.join(workdirPath2, 'b.txt'), 'other changes', 'utf8');
await assert.async.isTrue(repository2.refresh.called);
await assert.async.isTrue(atomGitRepository2.refreshStatus.called);
});
it('refreshes the appropriate Repository and Atom GitRepository when a commit is made in workspace 1', async function() {
await repository1.git.exec(['commit', '-am', 'commit in repository1']);
await assert.async.isTrue(repository1.refresh.called);
await assert.async.isTrue(atomGitRepository1.refreshStatus.called);
});
it('refreshes the appropriate Repository and Atom GitRepository when a commit is made in workspace 2', async function() {
await repository2.git.exec(['commit', '-am', 'commit in repository2']);
await assert.async.isTrue(repository2.refresh.called);
await assert.async.isTrue(atomGitRepository2.refreshStatus.called);
});
});
describe('#createRepositoryForProjectPath', function() {
it('creates and sets a repository for the given project path', async function() {
const workdirPath1 = await cloneRepository('three-files');
const workdirPath2 = await cloneRepository('three-files');
const nonRepositoryPath = fs.realpathSync(temp.mkdirSync());
fs.writeFileSync(path.join(nonRepositoryPath, 'c.txt'));
project.setPaths([workdirPath1, workdirPath2, nonRepositoryPath]);
await githubPackage.activate();
await workspace.open(path.join(nonRepositoryPath, 'c.txt'));
await assert.async.isNull(githubPackage.getActiveRepository());
await githubPackage.createRepositoryForProjectPath(nonRepositoryPath);
assert.isOk(githubPackage.getActiveRepository());
assert.equal(githubPackage.getActiveRepository(), await githubPackage.getRepositoryForWorkdirPath(nonRepositoryPath));
});
});
describe('#projectPathForItemPath', function() {
it('does not error when the path is falsy (e.g. new unsaved file)', function() {
sinon.stub(project, 'getDirectories').returns([new Directory('path')]);
assert.doesNotThrow(() => {
githubPackage.projectPathForItemPath(null);
});
});
it('returns the correct path when the item path starts with the project path but the item path is not in the project', function() {
const dirs = [path.join('path', 'to', 'my'), path.join('path', 'to', 'my-project')].map(p => new Directory(p));
sinon.stub(project, 'getDirectories').returns(dirs);
assert.equal(githubPackage.projectPathForItemPath(path.join('path', 'to', 'my-project', 'file.txt')), path.join('path', 'to', 'my-project'));
});
});
describe('serialized state', function() {
it('restores nonempty resolution progress', async function() {
const workdirMergeConflict = await cloneRepository('merge-conflict');
const workdirNoConflict = await cloneRepository('three-files');
project.setPaths([workdirMergeConflict, workdirNoConflict]);
await githubPackage.activate();
await githubPackage.getInitialModelsPromise();
// Record some state to recover later.
const resolutionMergeConflict0 = await githubPackage.getResolutionProgressForWorkdirPath(workdirMergeConflict);
resolutionMergeConflict0.reportMarkerCount('modified-on-both-ours.txt', 3);
const payload = githubPackage.serialize();
// Use a little guilty knowledge of the payload structure to ensure that the workdir without resolution
// progress isn't serialized with the rest of the package state.
assert.isDefined(payload.resolutionProgressByPath[workdirMergeConflict]);
assert.isUndefined(payload.resolutionProgressByPath[workdirNoConflict]);
const githubPackage1 = new GithubPackage(
workspace, project, commandRegistry, notificationManager, tooltips, styles, config, confirm,
);
await githubPackage1.activate(payload);
await githubPackage1.getInitialModelsPromise();
const resolutionMergeConflict1 = await githubPackage1.getResolutionProgressForWorkdirPath(workdirMergeConflict);
const resolutionNoConflict1 = await githubPackage1.getResolutionProgressForWorkdirPath(workdirNoConflict);
assert.isFalse(resolutionMergeConflict1.isEmpty());
assert.equal(resolutionMergeConflict1.getRemaining('modified-on-both-ours.txt'), 3);
assert.isTrue(resolutionNoConflict1.isEmpty());
});
});
});