-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathcli.test.ts
356 lines (302 loc) · 9.06 KB
/
cli.test.ts
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
import { spawn } from 'node:child_process';
import chalk from 'chalk';
import coffee from 'coffee';
import open from 'open';
import stripAnsi from 'strip-ansi';
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest';
import { unlink } from 'node:fs/promises';
import { onHome, onTest } from '../src/actions';
import { NPMRC, NRMRC, REGISTRIES } from '../src/constants';
import { isUnicodeSupported, readFile, writeFile } from '../src/helpers';
const isWin = process.platform === 'win32';
const shouldUseMain = isUnicodeSupported();
const pointer = shouldUseMain ? '❯' : '>';
const radioOff = shouldUseMain ? '◯' : '( )';
const radioOn = shouldUseMain ? '◉' : '(*)';
vi.setConfig({
testTimeout: 20000,
});
vi.mock('open', () => {
return {
default: vi.fn(() => console.log('browser opened')),
};
});
vi.mock('undici', () => {
return {
fetch: vi.fn((url) => {
return new Promise((resolve) => {
setTimeout(
() => resolve({ ok: !url.includes('error.com') }),
(Math.random() + 1) * 1000,
);
});
}),
};
});
vi.stubGlobal('__NRM_VERSION__', null);
vi.stubGlobal('__REGISTRY__', null);
beforeAll(async () => {
const { stdout } = await coffee.spawn('nrm', ['-V'], { shell: isWin }).end();
global.__NRM_VERSION__ = stdout ? stdout : null;
await coffee.spawn('npm', ['link'], { shell: isWin }).end();
});
afterAll(async () => {
await coffee.spawn('npm', ['unlink', 'nrm', '-g'], { shell: isWin }).end();
if (global.__NRM_VERSION__ !== null) {
await coffee
.spawn('npm', [`install -g nrm@${global.__NRM_VERSION__}`], {
shell: isWin,
})
.end();
}
});
it('nrm ls', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();
const { stdout, code } = await coffee
.spawn('nrm', ['ls'], { shell: isWin })
.end();
const match = `${chalk.green.bold('* ')}cnpm`;
expect(stdout.includes(match)).toBe(true);
expect(code).toBe(0);
});
it('nrm use <registry>', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();
});
it('nrm use <registry> local', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();
const npmrc = await readFile(NPMRC);
expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
await coffee
.spawn('nrm', ['current'], { shell: isWin })
.expect('stdout', /cnpm/g)
.expect('code', 0)
.end();
});
it('nrm use <registry> local with user config', async () => {
await writeFile(NPMRC, { abc: '123' });
await coffee
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();
const npmrc = await readFile(NPMRC);
expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
expect(npmrc.abc).toBe('123');
await coffee
.spawn('nrm', ['current'], { shell: isWin })
.expect('stdout', /cnpm/g)
.expect('code', 0)
.end();
});
it('nrm use without argument', async () => {
const { stdout } = spawn('nrm', ['use'], { shell: isWin });
const message = await new Promise((resolve) => {
stdout.on('data', (data) => {
resolve(stripAnsi(data.toString()).trim());
});
});
expect(
message,
).toBe(`? Please select the registry you want to use (Use arrow keys)
${pointer} npm
yarn
tencent
cnpm
taobao
npmMirror
huawei`);
});
it('nrm current', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();
await coffee
.spawn('nrm', ['current'], { shell: isWin })
.expect('stdout', /cnpm/g)
.expect('code', 0)
.end();
});
describe('nrm command which needs to add a custom registry', () => {
const customName = 'customName';
const url = 'https://registry.error.com/';
let __REGISTRY__ = '';
beforeEach(async () => {
__REGISTRY__ = customName;
await coffee
.spawn('nrm', ['add', `${__REGISTRY__}`, `${url}`], { shell: isWin })
.expect('stdout', /success/g)
.expect('code', 0)
.end();
});
afterEach(async () => {
await coffee
.spawn('nrm', ['del', `${__REGISTRY__}`], { shell: isWin })
.expect('stdout', /has been deleted successfully/g)
.expect('code', 0)
.end();
});
it('nrm rename', async () => {
const newName = 'newName';
__REGISTRY__ = newName;
const match = new RegExp(
`The registry '${customName}' has been renamed to '${newName}'`,
'g',
);
await coffee
.spawn('nrm', ['rename', `${customName}`, `${newName}`], { shell: isWin })
.expect('stdout', match)
.expect('code', 0)
.end();
});
it('nrm set <name>', async () => {
const attr = 'attr';
const value = 'value';
await coffee
.spawn(
'nrm',
['set', `${__REGISTRY__}`, '-a', `${attr}`, '-v', `${value}`],
{ shell: isWin },
)
.expect('stdout', /successfully/g)
.expect('code', 0)
.end();
});
it('nrm test [registry]', async () => {
const results = await onTest();
expect(results.every((ele) => /\d+\sms/.test(ele))).toBe(true);
expect(results.some((ele) => ele.includes('*'))).toBe(true);
expect(results.some((ele) => ele.includes('please ignore'))).toBe(true);
});
it('nrm set-scope <scopeName> <url>, del-scope <scopeName>', async () => {
const scopeName = 'nrm';
const url = 'https://scope.example.org';
await coffee
.spawn('nrm', ['set-scope', `${scopeName}`, `${url}`], { shell: isWin })
.expect('stdout', /success/g)
.expect('code', 0)
.end();
await coffee
.spawn('nrm', ['del-scope', `${scopeName}`], { shell: isWin })
.expect('stdout', /success/g)
.expect('code', 0)
.end();
});
it('nrm set-hosted-repo <name> <repo>', async () => {
const repo = 'repo';
const match = new RegExp(
`Set the repository of registry '${__REGISTRY__}' successfully`,
'g',
);
await coffee
.spawn('nrm', ['set-hosted-repo', `${__REGISTRY__}`, `${repo}`], {
shell: isWin,
})
.expect('stdout', match)
.expect('code', 0)
.end();
});
it('login <name> [base64]', async () => {
const username = 'username';
const password = 'password';
await coffee
.spawn(
'nrm',
['login', `${__REGISTRY__}`, '-u', `${username}`, '-p', `${password}`],
{ shell: isWin },
)
.expect('stdout', /success/g)
.expect('code', 0)
.end();
await coffee
.spawn('nrm', ['login', `${__REGISTRY__}`], { shell: isWin })
.expect(
'stderr',
/Authorization information in base64 format or username & password is required/g,
)
.end();
});
});
it('nrm home <registry> [browser]', async () => {
await onHome('cnpm');
expect(open).toHaveBeenCalled();
});
describe('nrm delete without argument (use keyword to select delete)', () => {
const registries = [
{ name: 'test', url: 'http://localhost:3000' },
{ name: 'test1', url: 'http://localhost:3001' },
{ name: 'test2', url: 'http://localhost:3002' },
];
beforeEach(async () => {
for (const registry of registries) {
await coffee
.spawn('nrm', ['add', `${registry.name}`, `${registry.url}`], {
shell: isWin,
})
.expect('stdout', /success/g)
.expect('code', 0)
.end();
}
});
afterEach(async () => {
await unlink(NRMRC);
});
it('nrm delete', async () => {
const { stdout } = spawn('nrm', ['del'], { shell: isWin });
const message = await new Promise((resolve) => {
stdout.on('data', (data) => {
resolve(stripAnsi(data.toString()).trim());
});
});
expect(message).toMatchInlineSnapshot(`
"? Please select the registries you want to delete (Press <space> to select, <a>
to toggle all, <i> to invert selection, and <enter> to proceed)
${pointer}${radioOff} test
${radioOff} test1
${radioOff} test2"
`);
});
it('nrm delete (with keyword input)', async () => {
const { stdout, stdin } = spawn('nrm', ['del'], { shell: isWin });
stdin.write('\u001b[B');
const message = await new Promise((resolve) => {
const m: string[] = [];
stdout.on('data', (data) => {
m.push(stripAnsi(data.toString()).trim());
// get the last output
if (m.length === 2) {
resolve(m[m.length - 1]);
}
});
});
expect(message).toMatchInlineSnapshot(`
"? Please select the registries you want to delete (Press <space> to select, <a>
to toggle all, <i> to invert selection, and <enter> to proceed)
${radioOff} test
${pointer}${radioOff} test1
${radioOff} test2"
`);
});
});