Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
261 lines (220 loc) · 8.39 KB
/
Copy pathhelpers.js
File metadata and controls
261 lines (220 loc) · 8.39 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
import fs from 'fs-extra';
import path from 'path';
import temp from 'temp';
import until from 'test-until';
import transpiler from 'atom-babel6-transpiler';
import React from 'react';
import ReactDom from 'react-dom';
import sinon from 'sinon';
import {Directory} from 'atom';
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';
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 = {};
function copyCachedRepo(repoName) {
const workingDirPath = temp.mkdirSync('git-fixture-');
fs.copySync(cachedClonedRepos[repoName], workingDirPath);
return fs.realpath(workingDirPath);
}
export const FAKE_USER = {
email: 'nope@nah.com',
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(['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]);
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});
}
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));
}
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, 'transpiled');
export function transpile(...relPaths) {
return Promise.all(
relPaths.map(async relPath => {
const untranspiledPath = require.resolve(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;
}),
);
}
// 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);
}
});