git.iko.soy/typeable/remark-validate-links
on main help sign in
Refactor tests for changes in mdast
commitf8fc60db231efb5a3e5abfb4bc2e795d6bed5afd authorTitus Wormer <tituswormer@gmail.com> dateThu 24 Dec 2015, 17:13 (11 years ago) parenta93303c
3 files changed+72−119
index.js+1−1 package.json+2−2 test/index.js+69−116
index.js+1−1
@@ -121,9 +121,9 @@ function gatherReferences(file, project) {
121121 var filePath = file.filePath();
122122 var directory = file.directory;
123123 var space = file.namespace('mdast');
124− var ast = space.tree || space.ast;
125124 var getDefinition;
126125 var prefix = '';
126+ var ast = space.tree || /* istanbul ignore next */ space.ast;
127127
128128 getDefinition = definitions(ast);
129129
package.json+2−2
@@ -25,16 +25,16 @@
2525 "index.js"
2626 ],
2727 "devDependencies": {
28− "cept": "^1.0.0",
2928 "eslint": "^1.0.0",
29+ "hook-std": "^0.1.0",
3030 "istanbul": "^0.4.0",
3131 "jscs": "^2.0.0",
3232 "jscs-jsdoc": "^1.0.0",
3333 "mdast": "^2.2.2",
3434 "mdast-comment-config": "^1.0.0",
3535 "mdast-github": "^1.0.0",
36− "mdast-toc": "^1.0.0",
3736 "mdast-lint": "^1.0.0",
37+ "mdast-toc": "^1.0.0",
3838 "mocha": "^2.0.0"
3939 },
4040 "scripts": {
test/index.js+69−116
@@ -15,8 +15,8 @@
1515 */
1616
1717var assert = require('assert');
18+var hook = require('hook-std');
1819var fs = require('fs');
19−var cept = require('cept');
2020var mdast = require('mdast');
2121var cli = require('mdast/lib/cli');
2222var links = require('..');
@@ -27,59 +27,37 @@ var links = require('..');
2727
2828var equal = assert.strictEqual;
2929
30−/*
31− * Cache to store output and errors by mdast.
32− */
33−
34−var std = {
35− 'out': [],
36− 'err': []
37−};
38−
3930/**
40− * Factory to store a bound type.
31+ * Test the cli.
4132 *
42− * @param {string} type - `"out"` or `"err"`.
43− * @return {Function} - Stores the bound type.
33+ * @param {Object} config - Configuration.
34+ * @param {Function} callback - Completion handler.
4435 */
45−function queue(type) {
46− /**
47− * Store a bound type.
48− */
49− return function () {
50− std[type].push([].slice.call(arguments).join(''));
51− };
52−}
36+function test(config, callback) {
37+ var stdout = [];
38+ var stderr = [];
39+ var opts = {};
40+ var unhookStdout;
41+ var unhookStderr;
5342
54−/**
55− * Intercept both `console.log` to `std.out` and
56− * `console.error` to `std.err`;
57− *
58− * @return {Function} - Stores output.
59− */
60−function intercept() {
61− var stopOut = cept(console, 'log', queue('out'));
62− var stopErr = cept(console, 'error', queue('err'));
63−
64− /**
65− * Stops intercepting. Returns the captured `std`.
66− *
67− * @return {Object} - Output.
68− */
69− return function () {
70− var current = {
71− 'out': std.out.join('\n').trim(),
72− 'err': std.err.join('\n').trim()
73− };
74−
75− stopOut();
76− stopErr();
77−
78− std.out = [];
79− std.err = [];
80−
81− return current;
82− };
43+ opts.silent = true;
44+
45+ unhookStdout = hook.stdout(opts, [].push.bind(stdout));
46+ unhookStderr = hook.stderr(opts, [].push.bind(stderr));
47+
48+ cli(config, function (err) {
49+ unhookStdout();
50+ unhookStderr();
51+
52+ if (err) {
53+ return callback(err);
54+ }
55+
56+ return callback(null, {
57+ 'stdout': stdout.join(''),
58+ 'stderr': stderr.join('')
59+ });
60+ });
8361}
8462
8563/*
@@ -102,9 +80,7 @@ describe('mdast-validate-links', function () {
10280 });
10381
10482 it('should warn when used without slugs', function (done) {
105− var stop = intercept();
106−
107− cli({
83+ test({
10884 'files': 'example.md',
10985 'color': false,
11086 'detectRC': false,
@@ -112,10 +88,8 @@ describe('mdast-validate-links', function () {
11288 'plugins': [
11389 '../../../index.js'
11490 ]
115− }, function (err) {
116− var res = stop();
117−
118− equal(res.err.split('\n').slice(0, 2).join('\n'), [
91+ }, function (err, res) {
92+ equal(res.stderr.split('\n').slice(0, 2).join('\n'), [
11993 'example.md',
12094 ' 1:1 error Error: Missing slugs. Use for ' +
12195 'example `mdast-slug` to generate heading IDs'
@@ -126,9 +100,7 @@ describe('mdast-validate-links', function () {
126100 });
127101
128102 it('should ignore unfound files', function (done) {
129− var stop = intercept();
130−
131− cli({
103+ test({
132104 'files': ['definitions.md', 'FOOOO'],
133105 'color': false,
134106 'detectRC': false,
@@ -137,19 +109,18 @@ describe('mdast-validate-links', function () {
137109 '../../../node_modules/mdast-slug',
138110 '../../../index.js'
139111 ]
140− }, function (err) {
141− var res = stop();
112+ }, function (err, res) {
113+ equal(res.stdout, '');
142114
143− equal(res.out, '');
144−
145− equal(res.err, [
115+ equal(res.stderr, [
146116 'FOOOO',
147117 ' 1:1 error No such file or directory',
148118 '',
149119 'definitions.md',
150120 ' 5:12-5:21 warning Link to unknown heading: `world`',
151121 '',
152− '2 messages (✖ 1 error, ⚠ 1 warning)'
122+ '2 messages (✖ 1 error, ⚠ 1 warning)',
123+ ''
153124 ].join('\n'));
154125
155126 done(err);
@@ -157,9 +128,7 @@ describe('mdast-validate-links', function () {
157128 });
158129
159130 it('should work when not all files are given', function (done) {
160− var stop = intercept();
161−
162− cli({
131+ test({
163132 'files': 'example.md',
164133 'color': false,
165134 'detectRC': false,
@@ -168,10 +137,8 @@ describe('mdast-validate-links', function () {
168137 '../../../node_modules/mdast-slug',
169138 '../../../index.js'
170139 ]
171− }, function (err) {
172− var res = stop();
173−
174− equal(res.err, [
140+ }, function (err, res) {
141+ equal(res.stderr, [
175142 'example.md',
176143 ' 5:37-5:51 warning Link to unknown heading: `world`',
177144 ' 23:10-23:37 warning Link to unknown file: ' +
@@ -191,7 +158,8 @@ describe('mdast-validate-links', function () {
191158 ' 47:10-47:38 warning Link to unknown heading in ' +
192159 '`examples/world.md`: `hello`',
193160 '',
194− '⚠ 9 warnings'
161+ '⚠ 9 warnings',
162+ ''
195163 ].join('\n'));
196164
197165 done(err);
@@ -199,9 +167,7 @@ describe('mdast-validate-links', function () {
199167 });
200168
201169 it('should work when all files are given', function (done) {
202− var stop = intercept();
203−
204− cli({
170+ test({
205171 'files': [
206172 'example.md',
207173 'examples/example.md'
@@ -213,12 +179,10 @@ describe('mdast-validate-links', function () {
213179 '../../../node_modules/mdast-slug',
214180 '../../../index.js'
215181 ]
216− }, function (err) {
217− var res = stop();
182+ }, function (err, res) {
183+ equal(res.stdout, '');
218184
219− equal(res.out, '');
220−
221− equal(res.err, [
185+ equal(res.stderr, [
222186 'example.md',
223187 ' 5:37-5:51 warning Link to unknown heading: `world`',
224188 ' 23:10-23:37 warning Link to unknown file: ' +
@@ -250,7 +214,8 @@ describe('mdast-validate-links', function () {
250214 ' 35:10-35:32 warning Link to unknown heading in ' +
251215 '`world.md`: `hello`',
252216 '',
253− '⚠ 14 warnings'
217+ '⚠ 14 warnings',
218+ ''
254219 ].join('\n'));
255220
256221 done(err);
@@ -258,9 +223,7 @@ describe('mdast-validate-links', function () {
258223 });
259224
260225 it('should work with definitions', function (done) {
261− var stop = intercept();
262−
263− cli({
226+ test({
264227 'files': 'definitions.md',
265228 'color': false,
266229 'detectRC': false,
@@ -269,14 +232,13 @@ describe('mdast-validate-links', function () {
269232 '../../../node_modules/mdast-slug',
270233 '../../../index.js'
271234 ]
272− }, function (err) {
273− var res = stop();
274−
275− equal(res.err, [
235+ }, function (err, res) {
236+ equal(res.stderr, [
276237 'definitions.md',
277238 ' 5:12-5:21 warning Link to unknown heading: `world`',
278239 '',
279− '⚠ 1 warning'
240+ '⚠ 1 warning',
241+ ''
280242 ].join('\n'));
281243
282244 done(err);
@@ -284,9 +246,7 @@ describe('mdast-validate-links', function () {
284246 });
285247
286248 it('should work on GitHub URLs when given a repo', function (done) {
287− var stop = intercept();
288−
289− cli({
249+ test({
290250 'files': [
291251 'example.md',
292252 'examples/example.md'
@@ -300,12 +260,10 @@ describe('mdast-validate-links', function () {
300260 'repository': 'wooorm/test'
301261 }
302262 }
303− }, function (err) {
304− var res = stop();
263+ }, function (err, res) {
264+ equal(res.stdout, '');
305265
306− equal(res.out, '');
307−
308− equal(res.err, [
266+ equal(res.stderr, [
309267 'example.md',
310268 ' 5:37-5:51 warning Link to unknown heading: ' +
311269 '`world`',
@@ -370,7 +328,8 @@ describe('mdast-validate-links', function () {
370328 ' 39:10-39:73 warning Link to unknown file: ' +
371329 '`world.md`',
372330 '',
373− '⚠ 30 warnings'
331+ '⚠ 30 warnings',
332+ ''
374333 ].join('\n'));
375334
376335 done(err);
@@ -378,13 +337,11 @@ describe('mdast-validate-links', function () {
378337 });
379338
380339 it('should work on GitHub URLs when with package.json', function (done) {
381− var stop = intercept();
382−
383340 fs.writeFileSync('./package.json', JSON.stringify({
384341 'repository': 'wooorm/test'
385342 }, 0, 2));
386343
387− cli({
344+ test({
388345 'files': [
389346 'example.md',
390347 'examples/example.md'
@@ -396,12 +353,10 @@ describe('mdast-validate-links', function () {
396353 '../../../node_modules/mdast-slug',
397354 '../../../index.js'
398355 ]
399− }, function (err) {
400− var res = stop();
401−
356+ }, function (err, res) {
402357 fs.unlinkSync('./package.json');
403358
404− equal(res.err, [
359+ equal(res.stderr, [
405360 'example.md',
406361 ' 5:37-5:51 warning Link to unknown heading: ' +
407362 '`world`',
@@ -466,7 +421,8 @@ describe('mdast-validate-links', function () {
466421 ' 39:10-39:73 warning Link to unknown file: ' +
467422 '`world.md`',
468423 '',
469− '⚠ 30 warnings'
424+ '⚠ 30 warnings',
425+ ''
470426 ].join('\n'));
471427
472428 done(err);
@@ -474,9 +430,7 @@ describe('mdast-validate-links', function () {
474430 });
475431
476432 it('should suggest similar links', function (done) {
477− var stop = intercept();
478−
479− cli({
433+ test({
480434 'files': ['suggestions.md'],
481435 'color': false,
482436 'detectRC': false,
@@ -485,17 +439,16 @@ describe('mdast-validate-links', function () {
485439 '../../../node_modules/mdast-slug',
486440 '../../../index.js'
487441 ]
488− }, function (err) {
489− var res = stop();
490−
491− equal(res.err, [
442+ }, function (err, res) {
443+ equal(res.stderr, [
492444 'suggestions.md',
493445 ' 3:22-3:37 warning Link to unknown heading: `helloo`. ' +
494446 'Did you mean `hello`',
495447 ' 7:17-7:40 warning Link to unknown heading in ' +
496448 '`example.md`: `fiiiles`. Did you mean `files`',
497449 '',
498− '⚠ 2 warnings'
450+ '⚠ 2 warnings',
451+ ''
499452 ].join('\n'));
500453
501454 done(err);