forked from pulsar-edit/ppm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish-spec.js
More file actions
381 lines (342 loc) · 11.7 KB
/
Copy pathpublish-spec.js
File metadata and controls
381 lines (342 loc) · 11.7 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
const path = require('path');
const fs = require('fs-plus');
const temp = require('temp');
const express = require('express');
const http = require('http');
const childProcess = require('child_process');
const Publish = require('../src/publish');
const Command = require('../src/command');
describe('apm publish', () => {
let server;
let requests;
let originalInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL;
afterAll(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalInterval;
});
beforeEach(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
delete process.env.ATOM_PACKAGES_URL;
spyOnToken();
silenceOutput();
spyOn(Command.prototype, 'spawn').and.callFake(
(_command, _args, optionsOrCallback, callbackOrMissing) => {
const [callback, _options] = callbackOrMissing == null
? [optionsOrCallback]
: [callbackOrMissing, optionsOrCallback];
callback(0, '', '');
}
);
spyOn(Publish.prototype, 'waitForTagToBeAvailable').and.callFake(
() => {
return Promise.resolve();
}
);
spyOn(Publish.prototype, 'versionPackage').and.callFake(
(_version) => {
return Promise.resolve('0.0.1');
}
);
requests = [];
const app = express();
app.post('/api/packages', (req, res) => {
requests.push(req);
res.sendStatus(201);
});
app.post('/api/packages/:packageName/versions', (req, res) => {
requests.push(req);
res.sendStatus(201);
});
server = http.createServer(app);
await new Promise((resolve) => {
server.listen(3000, '127.0.0.1', async () => {
process.env.ATOM_HOME = temp.mkdirSync('apm-home-dir-');
process.env.ATOM_API_URL = 'http://localhost:3000/api';
process.env.ATOM_RESOURCE_PATH = temp.mkdirSync('atom-resource-path-');
resolve();
});
});
});
afterEach(async () => {
await new Promise(resolve => server.close(resolve));
});
it("validates the package's package.json file", async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
fs.writeFileSync(path.join(packageToPublish, 'package.json'), '}{');
process.chdir(packageToPublish);
const callback = jasmine.createSpy('callback');
await apmRun(['publish'], callback);
expect(callback.calls.mostRecent().args[0].message).toBe(
`Error parsing package.json file: Unexpected token '}', "}{" is not valid JSON`
);
});
it('validates the package is in a Git repository', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0'
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
const callback = jasmine.createSpy('callback');
await apmRun(['publish'], callback);
expect(callback.calls.mostRecent().args[0].message).toBe(
'Package must be in a Git repository before publishing: https://help.github.com/articles/create-a-repo'
);
});
it('validates the engines.atom range in the package.json file', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
engines: {
atom: '><>'
}
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
const callback = jasmine.createSpy('callback');
await apmRun(['publish'], callback);
expect(callback.calls.mostRecent().args[0].message).toBe(
'The Pulsar or Atom engine range in the package.json file is invalid: ><>'
);
});
it('validates the dependency semver ranges in the package.json file', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
engines: {
atom: '1'
},
dependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
foo: '^^'
}
};
fs.writeFileSync(path.join(packageToPublish, 'package.json'), JSON.stringify(metadata));
process.chdir(packageToPublish);
const callback = jasmine.createSpy('callback');
await apmRun(['publish'], callback);
expect(callback.calls.mostRecent().args[0].message).toBe(
'The foo dependency range in the package.json file is invalid: ^^'
);
});
it('validates the dev dependency semver ranges in the package.json file', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
engines: {
atom: '1'
},
dependencies: {
foo: '^5'
},
devDependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
bar: '1,3'
}
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
const callback = jasmine.createSpy('callback');
await apmRun(['publish'], callback);
expect(callback.calls.mostRecent().args[0].message).toBe(
'The bar dev dependency range in the package.json file is invalid: 1,3'
);
});
it('publishes successfully when new', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
"repository": {
"type": "git",
"url": "https://github.com/pulsar-edit/foo"
},
engines: {
atom: '1'
},
dependencies: {
foo: '^5'
},
devDependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
}
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
childProcess.execSync('git init', { cwd: packageToPublish });
childProcess.execSync('git remote add origin https://github.com/pulsar-edit/foo', { cwd: packageToPublish });
const callback = jasmine.createSpy('callback');
await apmRun(['publish', 'patch'], callback);
expect(requests.length).toBe(1);
expect(callback.calls.mostRecent().args[0]).toBeUndefined();
});
it('publishes successfully when package exists', async () => {
spyOn(Publish.prototype, 'packageExists').and.callFake(() => {
return Promise.resolve(true);
});
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
"repository": {
"type": "git",
"url": "https://github.com/pulsar-edit/foo"
},
engines: {
atom: '1'
},
dependencies: {
foo: '^5'
},
devDependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
}
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
childProcess.execSync('git init', { cwd: packageToPublish });
childProcess.execSync('git remote add origin https://github.com/pulsar-edit/foo', { cwd: packageToPublish });
const callback = jasmine.createSpy('callback');
await apmRun(['publish', 'patch'], callback);
expect(requests.length).toBe(1);
expect(callback.calls.mostRecent().args[0]).toBeUndefined();
});
it('publishes successfully with --branch flag', async () => {
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
"repository": {
"type": "git",
"url": "https://github.com/pulsar-edit/foo"
},
engines: {
atom: '1'
},
dependencies: {
foo: '^5'
},
devDependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
}
};
fs.writeFileSync(
path.join(packageToPublish, 'package.json'),
JSON.stringify(metadata)
);
process.chdir(packageToPublish);
childProcess.execSync('git init', { cwd: packageToPublish });
childProcess.execSync('git remote add origin https://github.com/pulsar-edit/foo', { cwd: packageToPublish });
const callback = jasmine.createSpy('callback');
await apmRun(['publish', 'patch', '--branch', 'main'], callback);
expect(requests.length).toBe(1);
expect(callback.calls.mostRecent().args[0]).toBeUndefined();
});
it('publishes successfully when the package exists and is being renamed', async () => {
spyOn(Publish.prototype, 'packageExists').and.callFake((name) => {
// If we're renaming the package, we need to ask the API if the package's
// _old_ name exists. This mock will simulate what the API would say if
// we mistakenly ask it if the _new_ name exists.
return name === 'test';
});
let publishPackageSpy = spyOn(Publish.prototype, 'publishPackage').and.callThrough();
let registerPackageSpy = spyOn(Publish.prototype, 'registerPackage').and.callThrough();
const packageToPublish = temp.mkdirSync('apm-test-package-');
const metadata = {
name: 'test',
version: '1.0.0',
"repository": {
"type": "git",
"url": "https://github.com/pulsar-edit/foo"
},
engines: {
atom: '1'
},
dependencies: {
foo: '^5'
},
devDependencies: {
abc: 'git://github.com/user/project.git',
abcd: 'latest',
}
};
fs.writeFileSync(path.join(packageToPublish, 'package.json'), JSON.stringify(metadata));
process.chdir(packageToPublish);
childProcess.execSync('git init', { cwd: packageToPublish });
childProcess.execSync('git remote add origin https://github.com/pulsar-edit/foo', { cwd: packageToPublish });
const callback = jasmine.createSpy('callback');
await apmRun(['publish', 'patch', '--rename', 'test-renamed'], callback);
expect(registerPackageSpy.calls.count()).toBe(0);
expect(publishPackageSpy.calls.count()).toBe(1);
expect(
publishPackageSpy.calls.mostRecent()?.args?.[2]?.rename
).toBe('test-renamed');
expect(requests.length).toBe(1);
expect(callback.calls.mostRecent().args[0]).toBeUndefined();
});
});
describe('Publish.getDefaultBranch', () => {
let publish;
beforeEach(() => {
publish = new Publish();
});
it('falls back to main when symbolic-ref is unavailable', () => {
// execSync is bound at import time, so we call getDefaultBranch
// outside a real git repo where symbolic-ref will naturally fail
const repo = {
getConfigValue: jasmine.createSpy('getConfigValue').and.callFake((key) => {
if (key === 'branch.main.remote') return 'origin';
return null;
})
};
expect(publish.getDefaultBranch(repo)).toBe('main');
});
it('falls back to master when main is not configured', () => {
const repo = {
getConfigValue: jasmine.createSpy('getConfigValue').and.callFake((key) => {
if (key === 'branch.master.remote') return 'origin';
return null;
})
};
expect(publish.getDefaultBranch(repo)).toBe('master');
});
it('returns null when no default branch can be determined', () => {
const repo = {
getConfigValue: jasmine.createSpy('getConfigValue').and.returnValue(null)
};
expect(publish.getDefaultBranch(repo)).toBeNull();
});
it('prefers main over master', () => {
const repo = {
getConfigValue: jasmine.createSpy('getConfigValue').and.callFake((key) => {
if (key === 'branch.main.remote') return 'origin';
if (key === 'branch.master.remote') return 'origin';
return null;
})
};
expect(publish.getDefaultBranch(repo)).toBe('main');
});
});