Skip to content

Commit bd0d070

Browse files
committed
💯
1 parent 1e4dd63 commit bd0d070

File tree

4 files changed

+151
-10
lines changed

4 files changed

+151
-10
lines changed

bin/documentation.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ documentation(parsedArgs.inputs, parsedArgs.options, function (err, result) {
3030
});
3131

3232
formatter(result, formatterOptions, function (err, output) {
33-
if (err) {
34-
throw err;
35-
}
36-
3733
if (outputLocation !== 'stdout') {
3834
if (parsedArgs.formatter === 'html') {
3935
streamArray(output).pipe(vfs.dest(outputLocation));
@@ -45,7 +41,3 @@ documentation(parsedArgs.inputs, parsedArgs.options, function (err, result) {
4541
}
4642
});
4743
});
48-
49-
/*
50-
.pipe(argv.g ? github() : new PassThrough({ objectMode: true }))
51-
*/

test/bin.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
var test = require('tap').test,
44
path = require('path'),
5+
os = require('os'),
56
exec = require('child_process').exec,
67
fs = require('fs');
78

8-
function documentation(args, options, callback) {
9+
function documentation(args, options, callback, parseJSON) {
910
if (!callback) {
1011
callback = options;
1112
options = {};
@@ -21,7 +22,11 @@ function documentation(args, options, callback) {
2122
if (err) {
2223
return callback(err, stdout, stderr);
2324
}
24-
callback(err, JSON.parse(stdout), stderr);
25+
if (parseJSON === false) {
26+
callback(err, stdout, stderr);
27+
} else {
28+
callback(err, JSON.parse(stdout), stderr);
29+
}
2530
});
2631
}
2732

@@ -77,3 +82,63 @@ test('--shallow option', function (t) {
7782
t.end();
7883
});
7984
});
85+
86+
test('bad -f option', function (t) {
87+
documentation(['-f DOES-NOT-EXIST fixture/internal.input.js'], function (err, data) {
88+
t.ok(err.toString().match(/Formatter not found/), 'reports bad formatter');
89+
t.end();
90+
});
91+
});
92+
93+
test('html with no destination', function (t) {
94+
documentation(['-f html fixture/internal.input.js'], function (err, data) {
95+
t.ok(err.toString()
96+
.match(/The HTML output mode requires a destination directory set with -o/),
97+
'needs dest for html');
98+
t.end();
99+
});
100+
});
101+
102+
test('given no files', function (t) {
103+
documentation([''], function (err, data) {
104+
t.ok(err.toString()
105+
.match(/documentation was given no files and was not run in a module directory/),
106+
'no files given');
107+
t.end();
108+
});
109+
});
110+
111+
test('write to file', function (t) {
112+
113+
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
114+
115+
documentation(['--shallow fixture/internal.input.js -o ' + dst], {}, function (err, data) {
116+
t.error(err);
117+
t.equal(data, '');
118+
t.ok(fs.existsSync(dst), 'created file');
119+
t.end();
120+
}, false);
121+
});
122+
123+
test('write to html', function (t) {
124+
125+
var dstDir = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
126+
fs.mkdirSync(dstDir);
127+
128+
documentation(['--shallow fixture/internal.input.js -f html -o ' + dstDir], {},
129+
function (err, data) {
130+
t.error(err);
131+
t.equal(data, '');
132+
t.ok(fs.existsSync(path.join(dstDir, 'index.html')), 'created index.html');
133+
t.end();
134+
}, false);
135+
});
136+
137+
test('fatal error', function (t) {
138+
139+
documentation(['--shallow fixture/bad/syntax.input.js'], {},
140+
function (err, data) {
141+
t.ok(err.toString().match(/Unexpected token/), 'reports syntax error');
142+
t.end();
143+
}, false);
144+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[
2+
{
3+
"description": "This function returns the number one.",
4+
"tags": [
5+
{
6+
"title": "returns",
7+
"description": "numberone",
8+
"lineNumber": 2,
9+
"type": {
10+
"type": "NameExpression",
11+
"name": "Number"
12+
}
13+
}
14+
],
15+
"loc": {
16+
"start": {
17+
"line": 1,
18+
"column": 0
19+
},
20+
"end": {
21+
"line": 4,
22+
"column": 3
23+
}
24+
},
25+
"context": {
26+
"loc": {
27+
"start": {
28+
"line": 5,
29+
"column": 0
30+
},
31+
"end": {
32+
"line": 8,
33+
"column": 2
34+
}
35+
},
36+
"code": "module.exports = function () {\n // this returns 1\n return 1;\n};",
37+
"path": "test/fixture/simple.input.js",
38+
"github": "https://github.com/documentationjs/documentation/blob/1e4dd63875058fdb540e9cd0141606eebd3a2f65/test/fixture/simple.input.js#L5-L8"
39+
},
40+
"errors": [
41+
"memberof reference to module not found"
42+
],
43+
"returns": [
44+
{
45+
"title": "returns",
46+
"description": "numberone",
47+
"lineNumber": 2,
48+
"type": {
49+
"type": "NameExpression",
50+
"name": "Number"
51+
}
52+
}
53+
],
54+
"name": "exports",
55+
"kind": "function",
56+
"memberof": "module",
57+
"scope": "static",
58+
"members": {
59+
"instance": [],
60+
"static": []
61+
},
62+
"events": [],
63+
"path": [
64+
"exports"
65+
]
66+
}
67+
]

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ var test = require('tap').test,
1313

1414
var UPDATE = !!process.env.UPDATE;
1515

16+
if (fs.existsSync(path.join(__dirname, '../.git'))) {
17+
test('git option', function (t) {
18+
var file = path.join(__dirname, './fixture/simple.input.js');
19+
documentation([file], { github: true }, function (err, result) {
20+
t.ifError(err);
21+
normalize(result);
22+
var outputfile = file.replace('.input.js', '.output.github.json');
23+
if (UPDATE) {
24+
fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
25+
}
26+
var expect = require(outputfile);
27+
t.deepEqual(result, expect);
28+
t.end();
29+
});
30+
});
31+
}
32+
1633
test('external modules option', function (t) {
1734
documentation([
1835
path.join(__dirname, 'fixture', 'external.input.js')

0 commit comments

Comments
 (0)