-
Notifications
You must be signed in to change notification settings - Fork 879
/
help_test.ts
237 lines (214 loc) · 6.3 KB
/
help_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
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import 'source-map-support/register.js';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {LitCli} from '../lib/lit-cli.js';
import {LitConsole} from '../lib/console.js';
import {BufferedWritable} from './cli-test-utils.js';
import {ReferenceToCommand} from '../lib/command.js';
import {ConsoleConstructorOptions} from 'console';
import * as stream from 'stream';
import {suite} from './uvu-wrapper.js';
import {FilesystemTestRig} from '@lit-internal/tests/utils/filesystem-test-rig.js';
interface TestContext {
console: TestConsole;
stdin: NodeJS.ReadableStream;
stdinLines: string[];
fs: FilesystemTestRig;
}
const test = suite<TestContext>();
class TestConsole extends LitConsole {
readonly outputStream;
readonly errorStream;
constructor(opts: Partial<ConsoleConstructorOptions> = {}) {
const outputStream = new BufferedWritable();
const errorStream = new BufferedWritable();
super({...opts, stdout: outputStream, stderr: errorStream});
this.outputStream = outputStream;
this.errorStream = errorStream;
}
}
test.before.each(async (ctx) => {
ctx.console = new TestConsole();
ctx.fs = new FilesystemTestRig();
// To use, push your input into stdinLines before it would be read.
ctx.stdinLines = [];
ctx.stdin = stream.Readable.from(ctx.stdinLines);
await ctx.fs.setup();
});
test.after.each(async (ctx) => {
await ctx.fs.cleanup();
});
test('help with no command', async ({console, stdin}) => {
const cli = new LitCli(['help'], {console, stdin: stdin});
await cli.run();
const output = console.outputStream.text;
assert.equal(console.errorStream.buffer.length, 0);
assert.match(output, 'Lit CLI');
assert.match(output, 'Available Commands');
assert.match(output, 'localize');
});
test('help with localize command', async ({console, stdin}) => {
const cli = new LitCli(['help', 'localize'], {console, stdin});
await cli.run();
const output = console.outputStream.text;
assert.equal(console.errorStream.buffer.length, 0);
assert.match(output, 'lit localize');
assert.match(output, 'Sub-Commands');
assert.match(output, 'extract');
assert.match(output, 'build');
});
test('help with localize extract command', async ({console, stdin}) => {
const cli = new LitCli(['help', 'localize', 'extract'], {
console,
stdin,
});
await cli.run();
const output = console.outputStream.text;
assert.equal(console.errorStream.buffer.length, 0);
assert.match(output, 'lit localize extract');
assert.match(output, '--config');
});
const fooCommandReference: ReferenceToCommand = {
kind: 'reference',
name: 'foo',
description: 'This is the description in the `foo` reference.',
importSpecifier: 'foo-package-name',
};
test('help includes unresolved external command descriptions', async ({
console,
fs,
stdin,
}) => {
const cli = new LitCli(['help'], {
console,
cwd: fs.rootDir,
stdin,
});
cli.addCommand(fooCommandReference);
await cli.run();
const output = console.outputStream.text;
assert.equal(console.errorStream.buffer.length, 0);
assert.match(
output,
/\s+foo\s+This is the description in the `foo` reference./
);
});
test(`help fails when getting details of unresolved external command`, async ({
console,
fs,
stdin,
stdinLines,
}) => {
stdinLines.push('N\n'); // don't give permission to install
const cli = new LitCli(['help', 'foo'], {
console,
cwd: fs.rootDir,
stdin: stdin,
});
cli.addCommand(fooCommandReference);
await cli.run();
assert.equal(console.errorStream.text, `'foo' wasn't installed.\n`);
assert.equal(
console.outputStream.text,
`
The command foo is not installed.
Run 'npm install --save-dev foo-package-name'? [Y/n]\n`.trimStart()
);
});
test(`help for a resolved external command`, async ({console, fs, stdin}) => {
await fs.write({
'node_modules/foo-package-name/index.js': `
export function getCommand() {
return {
kind: 'resolved',
name: 'foo',
description: 'this is the resolved foo command from the node_modules directory',
run() {
globalThis.resolvedFooCommandHasRun = true;
}
};
};
`,
'node_modules/foo-package-name/package.json': {
type: 'module',
main: 'index.js',
},
});
let cli = new LitCli(['help', 'foo'], {
console,
cwd: fs.rootDir,
stdin,
});
cli.addCommand(fooCommandReference);
await cli.run();
let output = console.outputStream.text;
assert.equal(console.errorStream.text, '');
assert.match(output, 'lit foo');
assert.match(
output,
`this is the resolved foo command from the node_modules directory`
);
console = new TestConsole();
cli = new LitCli(['help'], {
console,
cwd: fs.rootDir,
stdin,
});
cli.addCommand(fooCommandReference);
await cli.run();
output = console.outputStream.text;
assert.equal(console.errorStream.buffer.length, 0);
assert.match(
output,
/\s+foo\s+this is the resolved foo command from the node_modules directory/
);
});
test('we install a referenced command with permission', async ({
console,
fs,
stdin,
stdinLines,
}) => {
await fs.write({
'working-dir/package.json': {},
'foo-package/index.js': `
export function getCommand() {
return {
kind: 'resolved',
name: 'foo',
description: 'this is the resolved foo command from the node_modules directory',
run() {
globalThis.resolvedFooCommandHasRun = true;
}
};
};
`,
'foo-package/package.json': {
name: 'foo-package-name',
type: 'module',
main: 'index.js',
},
});
stdinLines.push('Y\n'); // give permission to install
const cli = new LitCli(['help', 'foo'], {
console,
stdin,
cwd: fs.resolve('working-dir'),
});
cli.addCommand({...fooCommandReference, installFrom: '../foo-package'});
await cli.run();
assert.equal(console.errorStream.text, '');
// The npm install happend.
assert.match(console.outputStream.text, 'added 1 package');
// After installation, we were able to resolve the command.
assert.match(
console.outputStream.text,
'this is the resolved foo command from the node_modules directory'
);
});
test.run();