forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.spec.mjs
299 lines (243 loc) · 11.6 KB
/
utils.spec.mjs
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
import fs from 'fs';
import sh from 'shelljs';
import u from './utils.mjs';
describe('deploy-to-firebase/utils:', () => {
beforeEach(() => {
// Clear the `getRemoteRefs()` cache before each test to prevent previous executions from
// affecting subsequent tests.
u._GIT_REMOTE_REFS_CACHE.clear();
});
describe('computeMajorVersion()', () => {
it('should extract the major version from a branch name', () => {
expect(u.computeMajorVersion('1.2.3')).toBe(1);
expect(u.computeMajorVersion('4.5.6-rc.7')).toBe(4);
expect(u.computeMajorVersion('89.0')).toBe(89);
});
});
describe('getDirname()', () => {
it('should return the directory path given a file URL', () => {
expect(u.getDirname(import.meta.url)).toMatch(/aio[\\/]scripts[\\/]deploy-to-firebase$/);
expect(u.getDirname('file:///C:/foo/bar/baz.ext'))
.toBe((process.platform === 'win32') ? 'C:\\foo\\bar' : '/C:/foo/bar');
});
});
describe('getLatestCommit()', () => {
let getRemoteRefsSpy;
beforeEach(() => {
getRemoteRefsSpy = spyOn(u, 'getRemoteRefs').and.returnValue([
'1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40 refs/heads/3.0.x',
]);
});
it('should return the latest commit of a branch', () => {
expect(u.getLatestCommit('3.0.x')).toBe('1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40');
expect(getRemoteRefsSpy).toHaveBeenCalledWith('3.0.x', undefined);
});
it('should pass any options to `getRemoteRefs()`', () => {
const opts = {custom: true};
expect(u.getLatestCommit('3.0.x', opts)).toBe('1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40');
expect(getRemoteRefsSpy).toHaveBeenCalledWith('3.0.x', opts);
});
});
describe('getMostRecentMinorBranch()', () => {
let getRemoteRefsSpy;
beforeEach(() => {
const mockRefs3 = [
'1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40 refs/heads/3.1.x',
'1ccccccccccccccccccccccccccccccccccccc40 refs/heads/3.3.x',
'1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb40 refs/heads/3.2.x',
];
const mockRefs4 = [
'1ddddddddddddddddddddddddddddddddddddd40 refs/heads/4.5.x',
'1eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee40 refs/heads/4.6.x',
];
const mockRefsAll = [
...mockRefs3,
...mockRefs4,
'1fffffffffffffffffffffffffffffffffffff40 refs/heads/5.0.x',
];
getRemoteRefsSpy = spyOn(u, 'getRemoteRefs')
.withArgs('refs/heads/3.*.x', undefined).and.returnValue(mockRefs3)
.withArgs('refs/heads/3.*.x', jasmine.anything()).and.returnValue(mockRefs3)
.withArgs('refs/heads/4.*.x', undefined).and.returnValue(mockRefs4)
.withArgs('refs/heads/4.*.x', jasmine.anything()).and.returnValue(mockRefs4)
.withArgs('refs/heads/*.*.x', undefined).and.returnValue(mockRefsAll)
.withArgs('refs/heads/*.*.x', jasmine.anything()).and.returnValue(mockRefsAll);
});
it('should get all minor branches for the specified major version', () => {
u.getMostRecentMinorBranch('3');
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/3.*.x', undefined);
u.getMostRecentMinorBranch('4');
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/4.*.x', undefined);
});
it('should get all minor branches when no major version is specified', () => {
u.getMostRecentMinorBranch();
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/*.*.x', undefined);
u.getMostRecentMinorBranch(undefined);
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/*.*.x', undefined);
});
it('should pass any options to `getRemoteRefs()`', () => {
u.getMostRecentMinorBranch();
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/*.*.x', undefined);
u.getMostRecentMinorBranch('3');
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/3.*.x', undefined);
u.getMostRecentMinorBranch(undefined, {custom: 1});
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/*.*.x', {custom: 1});
u.getMostRecentMinorBranch('4', {custom: 2});
expect(getRemoteRefsSpy).toHaveBeenCalledWith('refs/heads/4.*.x', {custom: 2});
});
it('should return the most recent branch', () => {
expect(u.getMostRecentMinorBranch('3')).toBe('3.3.x');
expect(u.getMostRecentMinorBranch('4')).toBe('4.6.x');
expect(u.getMostRecentMinorBranch()).toBe('5.0.x');
});
it('should ignore branches that do not match the expected pattern', () => {
getRemoteRefsSpy.withArgs('refs/heads/*.*.x', undefined).and.returnValue([
'1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40 refs/heads/6.0.x',
'1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb40 refs/heads/6.1.x',
'1ccccccccccccccccccccccccccccccccccccc40 refs/heads/6.2.z',
'1ddddddddddddddddddddddddddddddddddddd40 refs/heads/7.3.x-rc.0',
]);
expect(u.getMostRecentMinorBranch()).toBe('6.1.x');
});
});
describe('getRemoteRefs()', () => {
let execSpy;
beforeEach(() => {
execSpy = spyOn(sh, 'exec').and.callFake(() => ([
' ',
'1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40 refs/heads/3.1.x',
'1ccccccccccccccccccccccccccccccccccccc40 refs/heads/3.3.x',
'1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb40 refs/heads/3.2.x',
'1ddddddddddddddddddddddddddddddddddddd40 refs/heads/4.5.x',
'1eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee40 refs/heads/4.6.x',
'1fffffffffffffffffffffffffffffffffffff40 refs/heads/5.0.x',
' ',
].join('\n')));
});
it('should retrieve the remote refs based on the speficied pattern/remote', () => {
u.getRemoteRefs('some-pattern', {remote: 'https://example.com/repo.git'});
expect(execSpy).toHaveBeenCalledWith(
'git ls-remote https://example.com/repo.git some-pattern', jasmine.anything());
});
it('should use the `angular/angular` repo if not remote is specified', () => {
u.getRemoteRefs('some-pattern');
expect(execSpy).toHaveBeenCalledWith(
`git ls-remote ${u.NG_REMOTE_URL} some-pattern`, jasmine.anything());
u.getRemoteRefs('other-pattern', {other: 'option'});
expect(execSpy).toHaveBeenCalledWith(
`git ls-remote ${u.NG_REMOTE_URL} other-pattern`, jasmine.anything());
});
it('should run the git command in silent mode', () => {
u.getRemoteRefs('some-pattern');
expect(execSpy).toHaveBeenCalledWith(jasmine.any(String), {silent: true});
});
it('should return a list of refs', () => {
expect(u.getRemoteRefs('some-pattern')).toEqual([
'1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa40 refs/heads/3.1.x',
'1ccccccccccccccccccccccccccccccccccccc40 refs/heads/3.3.x',
'1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb40 refs/heads/3.2.x',
'1ddddddddddddddddddddddddddddddddddddd40 refs/heads/4.5.x',
'1eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee40 refs/heads/4.6.x',
'1fffffffffffffffffffffffffffffffffffff40 refs/heads/5.0.x',
]);
});
it('should retrieve results from the cache (if available)', () => {
// Initially, retrieve results by executing the command.
const results1 = u.getRemoteRefs('some-pattern');
expect(execSpy).toHaveBeenCalledTimes(1);
// On subsequent calls with the same command, retrieve results from the cache.
expect(u.getRemoteRefs('some-pattern')).toBe(results1);
expect(u.getRemoteRefs('some-pattern', {remote: u.NG_REMOTE_URL})).toBe(results1);
expect(execSpy).toHaveBeenCalledTimes(1);
// Retrieve results for different command (different remote) by executing the command.
const results2 = u.getRemoteRefs('some-pattern', {remote: 'other-remote'});
expect(results2).not.toBe(results1);
expect(execSpy).toHaveBeenCalledTimes(2);
// Retrieve results for different command (different pattern) by executing the command.
const results3 = u.getRemoteRefs('other-pattern');
expect(results3).not.toBe(results1);
expect(results3).not.toBe(results2);
expect(execSpy).toHaveBeenCalledTimes(3);
// Retrieve results from the cache once available.
expect(u.getRemoteRefs('other-pattern', {remote: u.NG_REMOTE_URL})).toBe(results3);
expect(execSpy).toHaveBeenCalledTimes(3);
});
it('should not retrieve results from the cache with `retrieveFromCache: false`', () => {
// Initial call to retrieve and cache the results.
const results1 = u.getRemoteRefs('some-pattern');
expect(execSpy).toHaveBeenCalledTimes(1);
// Do not use cached results with `retrieveFromCache: false`.
const results2 = u.getRemoteRefs('some-pattern', {retrieveFromCache: false});
expect(results2).not.toBe(results1);
expect(execSpy).toHaveBeenCalledTimes(2);
const results3 = u.getRemoteRefs(
'some-pattern', {remote: u.NG_REMOTE_URL, retrieveFromCache: false});
expect(results3).not.toBe(results1);
expect(results3).not.toBe(results2);
expect(execSpy).toHaveBeenCalledTimes(3);
});
it('should cache the results for future use even with `retrieveFromCache: false`', () => {
// Initial call with `retrieveFromCache: false` (should still cache the results).
const results = u.getRemoteRefs('some-pattern', {retrieveFromCache: false});
expect(execSpy).toHaveBeenCalledTimes(1);
// Subsequent call uses the cached results.
expect(u.getRemoteRefs('some-pattern')).toBe(results);
expect(u.getRemoteRefs('some-pattern', {other: 'option'})).toBe(results);
expect(u.getRemoteRefs('some-pattern', {retrieveFromCache: true})).toBe(results);
expect(execSpy).toHaveBeenCalledTimes(1);
});
});
describe('loadJson()', () => {
let readFileSyncSpy;
beforeEach(() => readFileSyncSpy = spyOn(fs, 'readFileSync'));
it('should load and parse a JSON file', () => {
readFileSyncSpy.withArgs('/foo/bar.json', 'utf8').and.returnValue('{"foo": "bar"}');
expect(u.loadJson('/foo/bar.json')).toEqual({foo: 'bar'});
});
});
describe('logSectionHeader()', () => {
let logSpy;
beforeEach(() => logSpy = spyOn(console, 'log'));
it('should log a section header', () => {
u.logSectionHeader('Foo header');
expect(logSpy).toHaveBeenCalledWith('\n\n\n==== Foo header ====\n');
});
});
describe('nameFunction()', () => {
it('should overwrite a function\'s name', () => {
function foo() {}
const bar = () => {};
const baz = ({baz() {}}).baz;
expect(foo.name).toBe('foo');
expect(bar.name).toBe('bar');
expect(baz.name).toBe('baz');
u.nameFunction('foo2', foo);
u.nameFunction('bar2', bar);
u.nameFunction('baz2', baz);
expect(foo.name).toBe('foo2');
expect(bar.name).toBe('bar2');
expect(baz.name).toBe('baz2');
});
it('should return the function', () => {
function foo() {}
const bar = () => {};
const baz = ({baz() {}}).baz;
expect(u.nameFunction('foo2', foo)).toBe(foo);
expect(u.nameFunction('bar2', bar)).toBe(bar);
expect(u.nameFunction('baz2', baz)).toBe(baz);
});
});
describe('yarn()', () => {
let execSpy;
beforeEach(() => execSpy = spyOn(sh, 'exec'));
it('should execute yarn in silent mode', () => {
u.yarn('foo --bar');
const cmd = execSpy.calls.argsFor(0)[0];
expect(cmd.endsWith('--silent foo --bar')).toEqual(true);
});
it('should return the output from the command\'s execution', () => {
execSpy.and.returnValue('command output\n');
expect(u.yarn('foo --bar')).toBe('command output\n');
});
});
});