This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathhelpers.js
481 lines (405 loc) · 14.7 KB
/
helpers.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import fs from 'fs-extra';
import path from 'path';
import temp from 'temp';
import until from 'test-until';
import transpiler from '@atom/babel7-transpiler';
import React from 'react';
import ReactDom from 'react-dom';
import sinon from 'sinon';
import {Directory} from 'atom';
import {Emitter, CompositeDisposable, Disposable} from 'event-kit';
import Repository from '../lib/models/repository';
import GitShellOutStrategy from '../lib/git-shell-out-strategy';
import WorkerManager from '../lib/worker-manager';
import ContextMenuInterceptor from '../lib/context-menu-interceptor';
import getRepoPipelineManager from '../lib/get-repo-pipeline-manager';
import {clearRelayExpectations} from '../lib/relay-network-layer-manager';
import FileSystemChangeObserver from '../lib/models/file-system-change-observer';
assert.autocrlfEqual = (actual, expected, ...args) => {
const newActual = actual.replace(/\r\n/g, '\n');
const newExpected = expected.replace(/\r\n/g, '\n');
return assert.equal(newActual, newExpected, ...args);
};
// cloning a repo into a folder and then copying it
// for each subsequent request to clone makes cloning
// 2-3x faster on macOS and 5-10x faster on Windows
const cachedClonedRepos = {};
async function copyCachedRepo(repoName) {
const workingDirPath = temp.mkdirSync('git-fixture-');
await fs.copy(cachedClonedRepos[repoName], workingDirPath);
return fs.realpath(workingDirPath);
}
export const FAKE_USER = {
email: '[email protected]',
name: 'Someone',
};
export async function cloneRepository(repoName = 'three-files') {
if (!cachedClonedRepos[repoName]) {
const cachedPath = temp.mkdirSync('git-fixture-cache-');
const git = new GitShellOutStrategy(cachedPath);
await git.clone(path.join(__dirname, 'fixtures', `repo-${repoName}`, 'dot-git'), {noLocal: true});
await git.exec(['config', '--local', 'core.autocrlf', 'false']);
await git.exec(['config', '--local', 'commit.gpgsign', 'false']);
await git.exec(['config', '--local', 'user.email', FAKE_USER.email]);
await git.exec(['config', '--local', 'user.name', FAKE_USER.name]);
await git.exec(['config', '--local', 'push.default', 'simple']);
await git.exec(['checkout', '--', '.']); // discard \r in working directory
cachedClonedRepos[repoName] = cachedPath;
}
return copyCachedRepo(repoName);
}
export async function sha(directory) {
const git = new GitShellOutStrategy(directory);
const head = await git.getHeadCommit();
return head.sha;
}
/*
* Initialize an empty repository at a temporary path.
*/
export async function initRepository() {
const workingDirPath = temp.mkdirSync('git-fixture-');
const git = new GitShellOutStrategy(workingDirPath);
await git.exec(['init']);
await git.exec(['config', '--local', 'user.email', FAKE_USER.email]);
await git.exec(['config', '--local', 'user.name', FAKE_USER.name]);
await git.exec(['config', '--local', 'core.autocrlf', 'false']);
await git.exec(['config', '--local', 'commit.gpgsign', 'false']);
return fs.realpath(workingDirPath);
}
export async function setUpLocalAndRemoteRepositories(repoName = 'multiple-commits', options = {}) {
if (typeof repoName === 'object') {
options = repoName;
repoName = 'multiple-commits';
}
const baseRepoPath = await cloneRepository(repoName);
const baseGit = new GitShellOutStrategy(baseRepoPath);
// create remote bare repo with all commits
const remoteRepoPath = temp.mkdirSync('git-remote-fixture-');
const remoteGit = new GitShellOutStrategy(remoteRepoPath);
await remoteGit.clone(baseRepoPath, {noLocal: true, bare: true});
// create local repo with one fewer commit
if (options.remoteAhead) { await baseGit.exec(['reset', 'HEAD~']); }
const localRepoPath = temp.mkdirSync('git-local-fixture-');
const localGit = new GitShellOutStrategy(localRepoPath);
await localGit.clone(baseRepoPath, {noLocal: true});
await localGit.exec(['remote', 'set-url', 'origin', remoteRepoPath]);
await localGit.exec(['config', '--local', 'commit.gpgsign', 'false']);
await localGit.exec(['config', '--local', 'user.email', FAKE_USER.email]);
await localGit.exec(['config', '--local', 'user.name', FAKE_USER.name]);
await localGit.exec(['config', '--local', 'pull.rebase', false]);
return {baseRepoPath, remoteRepoPath, localRepoPath};
}
export async function getHeadCommitOnRemote(remotePath) {
const workingDirPath = temp.mkdirSync('git-fixture-');
const git = new GitShellOutStrategy(workingDirPath);
await git.clone(remotePath, {noLocal: true});
return git.getHeadCommit();
}
export async function buildRepository(workingDirPath, options) {
const repository = new Repository(workingDirPath, null, options);
await repository.getLoadPromise();
// eslint-disable-next-line jasmine/no-global-setup
afterEach(async () => {
const repo = await repository;
repo && repo.destroy();
});
return repository;
}
export function buildRepositoryWithPipeline(workingDirPath, options) {
const pipelineManager = getRepoPipelineManager(options);
return buildRepository(workingDirPath, {pipelineManager});
}
// Custom assertions
export function assertDeepPropertyVals(actual, expected) {
function extractObjectSubset(actualValue, expectedValue) {
if (actualValue !== Object(actualValue)) { return actualValue; }
const actualSubset = Array.isArray(actualValue) ? [] : {};
for (const key of Object.keys(expectedValue)) {
if (actualValue.hasOwnProperty(key)) {
actualSubset[key] = extractObjectSubset(actualValue[key], expectedValue[key]);
}
}
return actualSubset;
}
assert.deepEqual(extractObjectSubset(actual, expected), expected);
}
export function assertEqualSets(a, b) {
assert.equal(a.size, b.size, 'Sets are a different size');
a.forEach(item => assert(b.has(item), 'Sets have different elements'));
}
export function assertEqualSortedArraysByKey(arr1, arr2, key) {
const sortFn = (a, b) => a[key] < b[key];
assert.deepEqual(arr1.sort(sortFn), arr2.sort(sortFn));
}
// Helpers for test/models/patch classes
class PatchBufferAssertions {
constructor(patch, buffer) {
this.patch = patch;
this.buffer = buffer;
}
hunk(hunkIndex, {startRow, endRow, header, regions}) {
const hunk = this.patch.getHunks()[hunkIndex];
assert.isDefined(hunk);
assert.strictEqual(hunk.getRange().start.row, startRow);
assert.strictEqual(hunk.getRange().end.row, endRow);
assert.strictEqual(hunk.getHeader(), header);
assert.lengthOf(hunk.getRegions(), regions.length);
for (let i = 0; i < regions.length; i++) {
const region = hunk.getRegions()[i];
const spec = regions[i];
assert.strictEqual(region.constructor.name.toLowerCase(), spec.kind);
assert.strictEqual(region.toStringIn(this.buffer), spec.string);
assert.deepEqual(region.getRange().serialize(), spec.range);
}
}
hunks(...specs) {
assert.lengthOf(this.patch.getHunks(), specs.length);
for (let i = 0; i < specs.length; i++) {
this.hunk(i, specs[i]);
}
}
}
export function assertInPatch(patch, buffer) {
return new PatchBufferAssertions(patch, buffer);
}
export function assertInFilePatch(filePatch, buffer) {
return assertInPatch(filePatch.getPatch(), buffer);
}
export function assertMarkerRanges(markerLayer, ...expectedRanges) {
const bufferLayer = markerLayer.bufferMarkerLayer || markerLayer;
const actualRanges = bufferLayer.getMarkers().map(m => m.getRange().serialize());
assert.deepEqual(actualRanges, expectedRanges);
}
let activeRenderers = [];
export function createRenderer() {
let instance;
let lastInstance;
let node = document.createElement('div');
// ref function should be reference equal over renders to avoid React
// calling the "old" one with `null` and the "new" one with the instance
const setTopLevelRef = c => { instance = c; };
const renderer = {
render(appWithoutRef) {
lastInstance = instance;
const app = React.cloneElement(appWithoutRef, {ref: setTopLevelRef});
ReactDom.render(app, node);
},
get instance() {
return instance;
},
get lastInstance() {
return lastInstance;
},
get node() {
return node;
},
unmount() {
if (node) {
lastInstance = instance;
ReactDom.unmountComponentAtNode(node);
node = null;
}
},
};
activeRenderers.push(renderer);
return renderer;
}
export function isProcessAlive(pid) {
if (typeof pid !== 'number') {
throw new Error(`PID must be a number. Got ${pid}`);
}
let alive = true;
try {
return process.kill(pid, 0);
} catch (e) {
alive = false;
}
return alive;
}
class UnwatchedDirectory extends Directory {
onDidChangeFiles(callback) {
return {dispose: () => {}};
}
}
export async function disableFilesystemWatchers(atomEnv) {
atomEnv.packages.serviceHub.provide('atom.directory-provider', '0.1.0', {
directoryForURISync(uri) {
return new UnwatchedDirectory(uri);
},
});
await until('directoryProvider is available', () => atomEnv.project.directoryProviders.length > 0);
}
const packageRoot = path.resolve(__dirname, '..');
const transpiledRoot = path.resolve(__dirname, 'output/transpiled/');
export function transpile(...relPaths) {
return Promise.all(
relPaths.map(async relPath => {
const untranspiledPath = path.resolve(__dirname, '..', relPath);
const transpiledPath = path.join(transpiledRoot, path.relative(packageRoot, untranspiledPath));
const untranspiledSource = await fs.readFile(untranspiledPath, {encoding: 'utf8'});
const transpiledSource = transpiler.transpile(untranspiledSource, untranspiledPath, {}, {}).code;
await fs.mkdirs(path.dirname(transpiledPath));
await fs.writeFile(transpiledPath, transpiledSource, {encoding: 'utf8'});
return transpiledPath;
}),
);
}
// Manually defer the next setState() call performed on a React component instance until a returned resolution method
// is called. This is useful for testing code paths that will only be triggered if a setState call is asynchronous,
// which React can choose to do at any time, but Enzyme will never do on its own.
//
// This function will also return a pair of Promises which will be resolved when the stubbed setState call has begun
// and when it has completed. Be sure to await the `started` Promise before calling `deferSetState` again.
//
// Examples:
//
// ```
// const {resolve} = deferSetState(wrapper.instance());
// wrapper.instance().doStateChange();
// assert.isFalse(wrapper.update().find('Child').prop('changed'));
// resolve();
// assert.isTrue(wrapper.update().find('Child').prop('changed'));
// ```
//
// ```
// const {resolve: resolve0, started: started0} = deferSetState(wrapper.instance());
// /* ... */
// await started0;
// const {resolve: resolve1} = deferSetState(wrapper.instance());
// /* ... */
// resolve1();
// resolve0();
// ```
export function deferSetState(instance) {
if (!instance.__deferOriginalSetState) {
instance.__deferOriginalSetState = instance.setState;
}
let resolve, resolveStarted, resolveCompleted;
const started = new Promise(r => { resolveStarted = r; });
const completed = new Promise(r => { resolveCompleted = r; });
const resolved = new Promise(r => { resolve = r; });
const stub = function(updater, callback) {
resolveStarted();
resolved.then(() => {
instance.__deferOriginalSetState(updater, () => {
if (callback) {
callback();
}
resolveCompleted();
});
});
};
instance.setState = stub;
return {resolve, started, completed};
}
// eslint-disable-next-line jasmine/no-global-setup
beforeEach(function() {
global.sinon = sinon.createSandbox();
});
// eslint-disable-next-line jasmine/no-global-setup
afterEach(function() {
activeRenderers.forEach(r => r.unmount());
activeRenderers = [];
ContextMenuInterceptor.dispose();
global.sinon.restore();
clearRelayExpectations();
});
// eslint-disable-next-line jasmine/no-global-setup
after(() => {
if (!process.env.ATOM_GITHUB_SHOW_RENDERER_WINDOW) {
WorkerManager.reset(true);
}
if (global._nyc) {
global._nyc.writeCoverageFile();
if (global._nycInProcess) {
global._nyc.report();
}
}
});
export class ManualStateObserver {
constructor() {
this.emitter = new Emitter();
}
onDidComplete(callback) {
return this.emitter.on('did-complete', callback);
}
trigger() {
this.emitter.emit('did-complete');
}
dispose() {
this.emitter.dispose();
}
}
// File system event helpers
let observedEvents, eventCallback;
export async function wireUpObserver(fixtureName = 'multi-commits-files', existingWorkdir = null) {
observedEvents = [];
eventCallback = () => {};
const workdir = existingWorkdir || await cloneRepository(fixtureName);
const repository = new Repository(workdir);
await repository.getLoadPromise();
const observer = new FileSystemChangeObserver(repository);
const subscriptions = new CompositeDisposable(
new Disposable(async () => {
await observer.destroy();
repository.destroy();
}),
);
subscriptions.add(observer.onDidChange(events => {
observedEvents.push(...events);
eventCallback();
}));
return {repository, observer, subscriptions};
}
export function expectEvents(repository, ...suffixes) {
const pending = new Set(suffixes);
return new Promise((resolve, reject) => {
eventCallback = () => {
const matchingPaths = observedEvents
.filter(event => {
for (const suffix of pending) {
if (event.path.endsWith(suffix)) {
pending.delete(suffix);
return true;
}
}
return false;
});
if (matchingPaths.length > 0) {
repository.observeFilesystemChange(matchingPaths);
}
if (pending.size === 0) {
resolve();
}
};
if (observedEvents.length > 0) {
eventCallback();
}
});
}
// Atom environment utilities
// Ensure the Workspace doesn't mangle atom-github://... URIs.
// If you don't have an opener registered for a non-standard URI protocol, the Workspace coerces it into a file URI
// and tries to open it with a TextEditor. In the process, the URI gets mangled:
//
// atom.workspace.open('atom-github://unknown/whatever').then(item => console.log(item.getURI()))
// > 'atom-github:/unknown/whatever'
//
// Adding an opener that creates fake items prevents it from doing this and keeps the URIs unchanged.
export function registerGitHubOpener(atomEnv) {
atomEnv.workspace.addOpener(uri => {
if (uri.startsWith('atom-github://')) {
return {
getURI() { return uri; },
getElement() {
if (!this.element) {
this.element = document.createElement('div');
}
return this.element;
},
};
} else {
return undefined;
}
});
}