-
Notifications
You must be signed in to change notification settings - Fork 482
/
bin.js
404 lines (359 loc) · 10.2 KB
/
bin.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* global jasmine */
import path from 'path';
import os from 'os';
import { exec } from 'child_process';
import tmp from 'tmp';
import fs from 'fs-extra';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function documentation(args, options, parseJSON) {
if (!options) {
options = {};
}
if (!options.cwd) {
options.cwd = __dirname;
}
options.maxBuffer = 1024 * 1024;
args.unshift('node ' + path.join(__dirname, '..', 'bin', 'documentation.js'));
return new Promise((resolve, reject) => {
exec(args.join(' '), options, function (err, stdout, stderr) {
if (err) {
err.stderr = stderr;
return reject(err);
}
if (parseJSON === false) {
resolve(stdout);
} else {
try {
resolve(JSON.parse(stdout));
} catch (e) {
reject(e);
}
}
});
});
}
function normalize(result) {
result.forEach(function (item) {
item.context.file = '[path]';
});
return result;
}
test('documentation binary', async function () {
const data = await documentation(['build fixture/simple.input.js'], {});
expect(data.length).toBe(1);
});
test.skip('defaults to parsing package.json main', async function () {
const data = await documentation(['build'], {
cwd: path.join(__dirname, '..')
});
expect(data.length).toBeTruthy();
});
test('accepts config file', async function () {
const data = await documentation([
'build fixture/sorting/input.js -c fixture/config.json'
]);
expect(normalize(data)).toMatchSnapshot();
});
test('accepts config file - reports failures', async function () {
try {
await documentation(
['build fixture/sorting/input.js -c fixture/config-bad.yml'],
{},
false
);
} catch (stderr) {
expect(stderr).toMatchSnapshot();
}
});
test('accepts config file - reports parse failures', async function () {
try {
await documentation(
['build fixture/sorting/input.js -c fixture/config-malformed.json'],
{},
false
);
} catch (stderr) {
expect(stderr.stderr.match(/SyntaxError/g)).toBeTruthy();
}
});
test('--shallow option', async function () {
const data = await documentation([
'build --shallow fixture/internal.input.js'
]);
expect(data.length).toBe(0);
});
test('when a file is specified both in a glob and explicitly, it is only documented once', async function () {
const data = await documentation([
'build fixture/simple.input.js fixture/simple.input.*'
]);
expect(data.length).toBe(1);
});
test('extension option', async function () {
const data = await documentation([
'build fixture/extension/index.otherextension ' +
'--requireExtension=otherextension --parseExtension=otherextension'
]);
expect(data.length).toBe(1);
});
test('extension option', function () {
return documentation(['build fixture/extension.jsx']);
});
describe('invalid arguments', function () {
test('bad -f option', async function () {
try {
await documentation(
['build -f DOES-NOT-EXIST fixture/internal.input.js'],
{},
false
);
} catch (err) {
expect(err).toBeTruthy();
}
});
test('html with no destination', async function () {
try {
await documentation(['build -f html fixture/internal.input.js']);
} catch (err) {
expect(
err
.toString()
.match(
/The HTML output mode requires a destination directory set with -o/
)
).toBeTruthy();
}
});
test('bad command', async function () {
try {
await documentation(['-f html fixture/internal.input.js'], {}, false);
} catch (err) {
expect(err.code).toBeTruthy();
}
});
});
const semver =
/\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z-]+(?:\.[\da-z-]+)*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?\b/gi;
test('--config', async function () {
const dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
fs.mkdirSync(dst);
const outputIndex = path.join(dst, 'index.html');
const data = await documentation(
[
'build -c fixture/html/documentation.yml -f html fixture/html/nested.input.js -o ' +
dst
],
{},
false
);
let output = fs.readFileSync(outputIndex, 'utf8');
const version = (await import('../package.json')).default.version;
output = output.replace(new RegExp(version.replace(/\./g, '\\.'), 'g'), '');
expect(output).toMatchSnapshot();
});
test('--version', async function () {
const output = await documentation(['--version'], {}, false);
expect(output).toBeTruthy();
});
describe('lint command', function () {
test('generates lint output', async function () {
try {
await documentation(['lint fixture/lint/lint.input.js'], {}, false);
} catch (err) {
const data = err.stderr.toString().split('\n').slice(2).join('\n');
expect(data).toMatchSnapshot();
}
});
test('generates no output on a good file', async function () {
const data = await documentation(
['lint fixture/simple.input.js'],
{},
false
);
expect(data).toBe('');
});
test('exposes syntax error on a bad file', async function () {
try {
await documentation(
['lint fixture/bad/syntax.input', '--parseExtension input'],
{},
false
);
} catch (err) {
expect(err.code > 0).toBeTruthy();
}
});
test('lint with no inputs', async function () {
try {
await documentation(
['lint'],
{
cwd: path.join(__dirname, 'fixture/bad')
},
false
);
} catch (err) {
expect(err.code > 0).toBeTruthy();
}
});
test('generates lint output with shallow', async function () {
const data = await documentation(
['lint fixture/lint/lint.input.shallow.js --shallow'],
{},
false
);
expect(data).toBe('');
});
});
test('given no files', async function () {
try {
await documentation(['build']);
} catch (err) {
expect(
err
.toString()
.match(
/documentation was given no files and was not run in a module directory/
)
).toBeTruthy();
}
});
test('with an invalid command', async function () {
try {
await documentation(['invalid'], {}, false);
} catch (err) {
expect(err).toBeTruthy();
}
});
test.skip('--access flag', async function () {
const data = await documentation(
['build --shallow fixture/internal.input.js -a public'],
{},
false
);
expect(data).toBe('[]');
});
test.skip('--infer-private flag', async function () {
const data = await documentation(
['build fixture/infer-private.input.js --infer-private ^_'],
{},
false
);
// This uses JSON.parse with a reviver used as a visitor.
JSON.parse(data, function (n, v) {
// Make sure we do not see any names that match `^_`.
if (n === 'name') {
expect(typeof v).toBe('string');
expect(!/_$/.test.skip(v)).toBeTruthy();
}
return v;
});
});
test.skip('write to file', async function () {
const dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
const data = await documentation(
['build --shallow fixture/internal.input.js -o ' + dst],
{},
false
);
expect(data).toBe('');
expect(fs.existsSync(dst)).toBeTruthy();
});
test.skip('write to html', async function () {
const dstDir = path.join(
os.tmpdir(),
(Date.now() + Math.random()).toString()
);
fs.mkdirSync(dstDir);
const data = await documentation(
['build --shallow fixture/internal.input.js -f html -o ' + dstDir],
{},
false
);
expect(data).toBe('');
expect(fs.existsSync(path.join(dstDir, 'index.html'))).toBeTruthy();
});
test.skip('write to html with custom theme', async function () {
const dstDir = path.join(
os.tmpdir(),
(Date.now() + Math.random()).toString()
);
fs.mkdirSync(dstDir);
const data = await documentation(
[
'build -t fixture/custom_theme --shallow fixture/internal.input.js -f html -o ' +
dstDir
],
{},
false
);
expect(data).toBe('');
expect(fs.readFileSync(path.join(dstDir, 'index.html'), 'utf8')).toBeTruthy();
});
test.skip('write to html, highlightAuto', function () {
const fixture = 'fixture/auto_lang_hljs/multilanguage.input.js';
const config = 'fixture/auto_lang_hljs/config.yml';
const dstDir = path.join(
os.tmpdir(),
(Date.now() + Math.random()).toString()
);
fs.mkdirSync(dstDir);
return documentation(
['build --shallow ' + fixture + ' -c ' + config + ' -f html -o ' + dstDir],
{},
false
).then(() => {
const result = fs.readFileSync(path.join(dstDir, 'index.html'), 'utf8');
expect(
result.indexOf('<span class="hljs-number">42</span>') > 0
).toBeTruthy();
expect(
result.indexOf('<span class="hljs-selector-attr">[data-foo]</span>') > 0
).toBeTruthy();
expect(
result.indexOf('<span class="hljs-attr">data-foo</span>') > 0
).toBeTruthy();
});
});
test.skip('fatal error', async function () {
try {
await documentation(
['build --shallow fixture/bad/syntax.input --parseExtension input'],
{},
false
);
} catch (err) {
expect(err.toString().match(/Unexpected token/)).toBeTruthy();
}
});
test.skip('build --document-exported', async function () {
const data = await documentation(
['build fixture/document-exported.input.js --document-exported -f md'],
{},
false
);
expect(data).toMatchSnapshot();
});
test.skip('build large file without error (no deoptimized styling error)', function () {
const dstFile =
path.join(os.tmpdir(), (Date.now() + Math.random()).toString()) + '.js';
let contents = '';
for (let i = 0; i < 4e4; i++) {
contents += '/* - */\n';
}
fs.writeFileSync(dstFile, contents, 'utf8');
return documentation(['build ' + dstFile], {}, false).then(() => {
fs.unlinkSync(dstFile);
});
});
test.skip('should use browser resolve', async function () {
const data = await documentation(['build fixture/resolve/index.js']);
expect(normalize(data)).toMatchSnapshot();
});
test.skip('should use node resolve', async function () {
const data = await documentation([
'build fixture/resolve/index.js --resolve node'
]);
expect(normalize(data)).toMatchSnapshot();
});