-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathbin-readme.js
101 lines (84 loc) · 3.24 KB
/
bin-readme.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
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, callback);
}
test('readme command', function (group) {
var fixtures = path.join(__dirname, 'fixture/readme');
var sourceFile = path.join(fixtures, 'index.js');
tmp.dir({unsafeCleanup: true}, function (err, d) {
group.error(err);
fs.copySync(path.join(fixtures, 'README.input.md'), path.join(d, 'README.md'));
fs.copySync(path.join(fixtures, 'index.js'), path.join(d, 'index.js'));
// run tests after setting up temp dir
group.test('--diff-only: changes needed', function (t) {
t.error(err);
var before = fs.readFileSync(path.join(d, 'README.md'), 'utf-8');
documentation(['readme index.js --diff-only -s API'], {cwd: d}, function (err, stdout, stderr) {
var after = fs.readFileSync(path.join(d, 'README.md'), 'utf-8');
t.ok(err);
t.notEqual(err.code, 0, 'exit nonzero');
t.same(after, before, 'readme unchanged');
t.end();
});
});
var expectedFile = path.join(fixtures, 'README.output.md');
var expected = fs.readFileSync(expectedFile, 'utf-8');
group.test('updates README.md', function (t) {
documentation(['readme index.js -s API'], {cwd: d}, function (err, stdout) {
t.error(err);
var actual = fs.readFileSync(path.join(d, 'README.md'), 'utf-8');
t.same(actual, expected, 'generated readme output');
t.end();
});
});
group.test('--readme-file', function (t) {
fs.copySync(path.join(fixtures, 'README.input.md'), path.join(d, 'other.md'));
documentation(['readme index.js -s API --readme-file other.md'], {cwd: d}, function (err, stdout) {
t.error(err);
var actual = fs.readFileSync(path.join(d, 'other.md'), 'utf-8');
t.same(actual, expected, 'generated readme output');
t.end();
});
});
group.test('--diff-only: changes NOT needed', function (t) {
t.error(err);
fs.copySync(path.join(fixtures, 'README.output.md'), path.join(d, 'uptodate.md'));
documentation(['readme index.js --diff-only -s API --readme-file uptodate.md'],
{cwd: d}, function (err, stdout, stderr) {
t.error(err);
t.match(stdout, 'is up to date.');
t.end();
});
});
group.test('requires -s option', function (t) {
documentation(['readme index.js'], {cwd: d}, function (err, stdout, stderr) {
t.ok(err);
t.ok(err.code !== 0, 'exit nonzero');
t.match(stderr, 'Missing required argument: s');
t.end();
});
});
group.test('errors if specified readme section is missing', function (t) {
documentation(['readme index.js -s DUMMY'], {cwd: d}, function (err, stdout, stderr) {
t.ok(err);
t.ok(err.code !== 0, 'exit nonzero');
t.end();
});
});
group.end();
});
});