-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathjsdox.js
169 lines (139 loc) · 5.98 KB
/
jsdox.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
var exec = require('child_process').exec;
var expect = require('expect.js');
var fs = require('fs');
var bin = 'bin/jsdox';
describe('jsdox', function() {
it('prints an error if an input file or directory is not supplied', function(done) {
expectOutputFromCommand(bin, 'Error', done, true);
});
it('generates non-empty output markdown files from the fixtures/ files', function(done) {
var cmd = bin + ' fixtures/**.js -o sample_output';
exec(cmd, function(err, stdout, stderr) {
expect(stderr).to.be.empty();
fs.readdirSync('sample_output').forEach(function(outputFile) {
if (!fs.statSync('sample_output/' + outputFile).isDirectory()) {
var content = fs.readFileSync('sample_output/' + outputFile).toString();
expect(content).not.to.be.empty();
}
});
done();
});
});
it('generates non-empty output markdown files from the fixtures/ and the fixtures/under files', function(done) {
this.timeout(5000);
var cmd = bin + ' fixtures/ -o sample_output -r';
//in case an old index.md is here
try {
fs.unlinkSync('sample_output/index.md');
} catch(err) {}
exec(cmd, function(err, stdout, stderr) {
expect(stderr).to.be.empty();
var nbFiles = 0;
fs.readdirSync('sample_output').forEach(function(outputFile) {
if (!fs.statSync('sample_output/' + outputFile).isDirectory()) {
var content = fs.readFileSync('sample_output/' + outputFile).toString();
expect(content).not.to.be.empty();
nbFiles += 1;
}
});
expect(nbFiles).to.be(9);
done();
});
});
it('generates non-empty output markdown files from the fixtures/ and the fixtures/under and' +
' the fixtures/under_grandparent/under_parent files and an under and an under_grandparent/under_parent directory in outputs', function(done) {
this.timeout(5000);
var cmd = bin + ' fixtures/ -o sample_output --rr -i';
exec(cmd, function(err, stdout, stderr) {
expect(stderr).to.be.empty();
var nbFilesA = 0;
var nbFilesB = 0;
var nbFilesC = 0;
fs.readdirSync('sample_output/fixtures').forEach(function(outputFile) {
if (!fs.statSync('sample_output/fixtures/' + outputFile).isDirectory()) {
if (!fs.statSync('sample_output/' + outputFile).isDirectory()) {
var content = fs.readFileSync('sample_output/fixtures/' + outputFile).toString();
expect(content).not.to.be.empty();
nbFilesA += 1;
//clean for future tests
fs.unlinkSync('sample_output/fixtures/' + outputFile);
}
}
});
expect(nbFilesA).to.be(7);
fs.readdirSync('sample_output/fixtures/under').forEach(function(outputFile) {
if (!fs.statSync('sample_output/fixtures/under/' + outputFile).isDirectory()) {
var content = fs.readFileSync('sample_output/fixtures/under/' + outputFile).toString();
expect(content).not.to.be.empty();
nbFilesB += 1;
fs.unlinkSync('sample_output/fixtures/under/' + outputFile);
}
});
expect(nbFilesB).to.be(2);
fs.readdirSync('sample_output/fixtures/under_grandparent/under_parent').forEach(function(outputFile) {
if (!fs.statSync('sample_output/fixtures/under_grandparent/under_parent/' + outputFile).isDirectory()) {
var content = fs.readFileSync('sample_output/fixtures/under_grandparent/under_parent/' + outputFile).toString();
expect(content).not.to.be.empty();
nbFilesC += 1;
fs.unlinkSync('sample_output/fixtures/under_grandparent/under_parent/' + outputFile);
}
});
expect(nbFilesC).to.be(1);
fs.rmdirSync('sample_output/fixtures/under_grandparent/under_parent/');
fs.rmdirSync('sample_output/fixtures/under_grandparent/');
fs.rmdirSync('sample_output/fixtures/under/');
fs.rmdirSync('sample_output/fixtures/');
done();
});
});
it('generates non-empty output markdown files from the fixtures/ and the fixtures/under files and index.md', function(done) {
this.timeout(5000);
var cmd = bin + ' fixtures/ -o sample_output -r -i';
exec(cmd, function(err, stdout, stderr) {
expect(stderr).to.be.empty();
var nbFiles = 0;
var hasIndex = false;
fs.readdirSync('sample_output').forEach(function(outputFile) {
if (fs.lstatSync('sample_output/' + outputFile).isFile()) {
var content = fs.readFileSync('sample_output/' + outputFile).toString();
expect(content).not.to.be.empty();
nbFiles += 1;
hasIndex = hasIndex || (outputFile === 'index.md');
}
});
expect(nbFiles).to.be(10);
expect(hasIndex).to.be(true);
//clean index for other tests
fs.unlinkSync('sample_output/index.md');
done();
});
});
describe('cli options', function() {
it('prints the help menu with the -H option', function(done) {
expectOutputFromCommand(bin + ' -H', 'Usage:', done);
});
it('prints the version with the -v option', function(done) {
expectOutputFromCommand(bin + ' -v', require('../package.json').version, done);
});
it('accepts a custom template directory with the -t option');
describe('-o option', function() {
it('converts an input file to an output markdown file');
it('converts an input directory of files to an output directory of markdown files');
});
});
});
/**
* Helper for asserting that the output from running jsdox from the cli
* contains a given string
* @param {String} cmd - The command to execute
* @param {String} output - The string that should be in the output
* @param {Function} done - Executed when the exec is finished
* @param {Boolean} isError - Whether or not to check stderr instead
*/
function expectOutputFromCommand(cmd, output, done, isError) {
exec(cmd, function(err, stdout, stderr) {
var stream = isError ? stderr : stdout;
expect(stream.indexOf(output) !== -1).to.be(true);
done();
});
}