forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
317 lines (271 loc) · 9.08 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
'use strict';
var test = require('tap').test,
path = require('path'),
os = require('os'),
exec = require('child_process').exec,
tmp = require('tmp'),
fs = require('fs-extra');
function documentation(args, options, callback, parseJSON) {
if (!callback) {
callback = options;
options = {};
}
if (!options.cwd) {
options.cwd = __dirname;
}
options.maxBuffer = 1024 * 1024;
args.unshift(path.join(__dirname, '../bin/documentation.js'));
exec(args.join(' '), options, function (err, stdout, stderr) {
if (err) {
return callback(err, stdout, stderr);
}
if (parseJSON === false) {
callback(err, stdout, stderr);
} else {
callback(err, JSON.parse(stdout), stderr);
}
});
}
function normalize(result) {
result.forEach(function (item) {
item.context.file = '[path]';
});
return result;
}
var options = { timeout: 1000 * 120 };
test('documentation binary', function (t) {
documentation(['build fixture/simple.input.js'], function (err, data) {
t.error(err);
t.equal(data.length, 1, 'simple has no dependencies');
t.end();
});
}, options);
test('defaults to parsing package.json main', function (t) {
documentation(['build'], { cwd: path.join(__dirname, '..') }, function (err, data) {
t.error(err);
t.ok(data.length, 'we document ourself');
t.end();
});
}, options);
test('polyglot mode', function (t) {
documentation(['build fixture/polyglot/blend.cpp --polyglot'],
function (err, data) {
t.ifError(err);
if (process.env.UPDATE) {
fs.writeFileSync(
path.resolve(__dirname,
'fixture',
'polyglot/blend.json'), JSON.stringify(normalize(data), null, 2), 'utf8');
}
var expected = fs.readFileSync(
path.resolve(__dirname,
'fixture',
'polyglot/blend.json'), 'utf8');
t.deepEqual(
normalize(data),
JSON.parse(expected),
'parsed C++ file');
t.end();
});
}, options);
test('accepts config file', function (t) {
documentation(['build fixture/sorting/input.js -c fixture/config.json'],
function (err, data) {
t.error(err);
if (process.env.UPDATE) {
fs.writeFileSync(
path.resolve(__dirname,
'fixture',
'sorting/output.json'), JSON.stringify(normalize(data), null, 2), 'utf8');
}
var expected = fs.readFileSync(
path.resolve(__dirname,
'fixture',
'sorting/output.json'), 'utf8');
t.deepEqual(
normalize(data),
JSON.parse(expected),
'respected sort order from config file');
t.end();
});
}, options);
test('--shallow option', function (t) {
documentation(['build --shallow fixture/internal.input.js'], function (err, data) {
t.error(err);
t.equal(data.length, 0, 'should not check dependencies');
t.end();
});
}, options);
test('external modules option', function (t) {
documentation(['build fixture/external.input.js ' +
'--external=external --external=external/node_modules'], function (err, data) {
t.ifError(err);
t.equal(data.length, 2, 'Includes external file');
t.end();
});
});
test('extension option', function (t) {
documentation(['build fixture/extension/index.otherextension ' +
'--extension=otherextension'], function (err, data) {
t.ifError(err);
t.equal(data.length, 1, 'includes a file with an arbitrary extension');
t.end();
});
});
test('invalid arguments', function (group) {
group.test('bad -f option', function (t) {
documentation(['build -f DOES-NOT-EXIST fixture/internal.input.js'], function (err) {
t.ok(err, 'returns error');
t.end();
});
}, options);
group.test('html with no destination', function (t) {
documentation(['build -f html fixture/internal.input.js'], function (err) {
t.ok(err.toString()
.match(/The HTML output mode requires a destination directory set with -o/),
'needs dest for html');
t.end();
});
}, options);
group.test('bad command', function (t) {
documentation(['-f html fixture/internal.input.js'], function (err, stdout, stderr) {
t.ok(err.code, 'exits nonzero');
t.ok(stderr.match(/Unknown command/), 'reports unknown command');
t.end();
});
});
group.end();
});
test('--version', function (t) {
documentation(['--version'], {}, function (err, output) {
t.ok(output, 'outputs version');
t.end();
}, false);
}, options);
test('lint command', function (group) {
group.test('generates lint output', function (t) {
documentation(['lint fixture/lint/lint.input.js'], function (err, data) {
var output = path.join(__dirname, 'fixture/lint/lint.output.js');
data = data.toString().split('\n').slice(2).join('\n');
if (process.env.UPDATE) {
fs.writeFileSync(output, data);
}
t.equal(err.code, 1);
t.equal(data, fs.readFileSync(output, 'utf8'), 'outputs lint');
t.end();
});
}, options);
group.test('generates no output on a good file', function (t) {
documentation(['lint fixture/simple.input.js'], {}, function (err, data) {
t.equal(err, null);
t.equal(data, '', 'no output');
t.end();
}, false);
}, options);
group.test('exposes syntax error on a bad file', function (t) {
documentation(['lint fixture/bad/syntax.input.js'], {}, function (err, data) {
t.ok(err.code > 0, 'exits with a > 0 exit code');
t.end();
}, false);
}, options);
group.end();
});
test('given no files', function (t) {
documentation(['build'], function (err) {
t.ok(err.toString()
.match(/documentation was given no files and was not run in a module directory/),
'no files given');
t.end();
});
}, options);
test('with an invalid command', function (t) {
documentation(['invalid'], function (err) {
t.ok(err, 'returns error');
t.end();
});
}, options);
test('--access flag', function (t) {
documentation(['build --shallow fixture/internal.input.js -a public'], {}, function (err, data) {
t.error(err);
t.equal(data, '[]');
t.end();
}, false);
});
test('--private flag', function (t) {
documentation(['build fixture/internal.input.js --private'], {}, function (err, data) {
t.error(err);
t.ok(data.length > 2, 'outputs docs');
t.end();
}, false);
});
test('--infer-private flag', function (t) {
documentation(['build fixture/infer-private.input.js --infer-private ^_'], {}, function (err, data) {
t.error(err);
// 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') {
t.equal(typeof v, 'string');
t.ok(!/_$/.test(v));
}
return v;
});
t.end();
}, false);
});
test('write to file', function (t) {
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
documentation(['build --shallow fixture/internal.input.js -o ' + dst], {}, function (err, data) {
t.error(err);
t.equal(data, '');
t.ok(fs.existsSync(dst), 'created file');
t.end();
}, false);
}, options);
test('write to html', function (t) {
var dstDir = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
fs.mkdirSync(dstDir);
documentation(['build --shallow fixture/internal.input.js -f html -o ' + dstDir], {},
function (err, data) {
t.error(err);
t.equal(data, '');
t.ok(fs.existsSync(path.join(dstDir, 'index.html')), 'created index.html');
t.end();
}, false);
}, options);
test('write to html with custom theme', function (t) {
var dstDir = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
fs.mkdirSync(dstDir);
documentation(['build -t fixture/custom_theme --shallow fixture/internal.input.js -f html -o ' + dstDir], {},
function (err, data) {
t.error(err);
t.equal(data, '');
t.ok(fs.readFileSync(path.join(dstDir, 'index.html'), 'utf8'), 'Hello world');
t.end();
}, false);
}, options);
test('write to html, highlightAuto', function (t) {
var fixture = 'fixture/auto_lang_hljs/multilanguage.input.js',
config = 'fixture/auto_lang_hljs/config.yml',
dstDir = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
fs.mkdirSync(dstDir);
documentation(['build --shallow ' + fixture + ' -c ' + config + ' -f html -o ' + dstDir], {},
function (err) {
t.ifErr(err);
var result = fs.readFileSync(path.join(dstDir, 'index.html'), 'utf8');
t.ok(result.indexOf('<span class="hljs-number">42</span>') > 0,
'javascript is recognized by highlightjs');
t.ok(result.indexOf('<span class="hljs-selector-attr">[data-foo]</span>') > 0,
'css is recognized by highlightjs');
t.ok(result.indexOf('<span class="hljs-attr">data-foo</span>') > 0,
'html is recognized by highlightjs');
t.end();
}, false);
}, options);
test('fatal error', function (t) {
documentation(['build --shallow fixture/bad/syntax.input.js'], {},
function (err) {
t.ok(err.toString().match(/Unexpected token/), 'reports syntax error');
t.end();
}, false);
}, options);