@@ -0,0 +1,384 @@
1+'use strict';
2+
3+/* eslint-env mocha */
4+
5+/*
6+ * Dependencies.
7+ */
8+
9+var assert = require('assert');
10+var fs = require('fs');
11+var cept = require('cept');
12+var mdast = require('mdast');
13+var cli = require('mdast/lib/cli');
14+var links = require('..');
15+
16+/*
17+ * Cache to store output and errors by mdast.
18+ */
19+
20+var std = {
21+ 'out': '',
22+ 'err': ''
23+};
24+
25+/**
26+ * Factory to store a bound type.
27+ *
28+ * @param {string} type - `"out"` or `"err"`.
29+ * @return {Function} - Stores the bound type.
30+ */
31+function queue(type) {
32+ /**
33+ * Store a bound type.
34+ */
35+ return function () {
36+ std[type] += [].slice.call(arguments).join('');
37+ };
38+}
39+
40+/**
41+ * Intercept both `console.log` to `std.out` and
42+ * `console.error` to `std.err`;
43+ *
44+ * @return {Function} - Stores output.
45+ */
46+function intercept() {
47+ var stopOut = cept(console, 'log', queue('out'));
48+ var stopErr = cept(console, 'error', queue('err'));
49+
50+ /**
51+ * Stops intercepting. Returns the captured `std`.
52+ *
53+ * @return {Object} - Output.
54+ */
55+ return function () {
56+ var current = {
57+ 'out': std.out.trim(),
58+ 'err': std.err.trim()
59+ };
60+
61+ stopOut();
62+ stopErr();
63+
64+ std.out = '';
65+ std.err = '';
66+
67+ return current;
68+ };
69+}
70+
71+/*
72+ * Tests.
73+ */
74+
75+describe('mdast-validate-links', function () {
76+ beforeEach(function () {
77+ process.chdir('./test/fixtures');
78+ });
79+
80+ afterEach(function () {
81+ process.chdir('../..');
82+ });
83+
84+ it('should throw an error when not on the CLI', function () {
85+ assert.throws(function () {
86+ mdast().use(links);
87+ }, /Error: mdast-validate-links only works on the CLI/);
88+ });
89+
90+ it('should warn when used without slugs', function (done) {
91+ var stop = intercept();
92+
93+ cli({
94+ 'files': 'example.md',
95+ 'color': false,
96+ 'detectRC': false,
97+ 'detectIgnore': false,
98+ 'plugins': ['./index.js']
99+ }, function (err) {
100+ var res = stop();
101+
102+ assert.equal(res.out, '');
103+
104+ assert.equal(res.err, [
105+ 'example.md',
106+ ' 0:0 error Missing slugs. Use for example ' +
107+ '`mdast-slug` to generate heading IDs',
108+ '',
109+ '✖ 1 problem (1 error, 0 warnings)'
110+ ].join('\n'));
111+
112+ done(err);
113+ });
114+ });
115+
116+ it('should ignore unfound files', function (done) {
117+ var stop = intercept();
118+
119+ cli({
120+ 'files': ['definitions.md', 'FOOOO'],
121+ 'color': false,
122+ 'detectRC': false,
123+ 'detectIgnore': false,
124+ 'plugins': ['slug', './index.js']
125+ }, function (err) {
126+ var res = stop();
127+
128+ assert.equal(res.out, '');
129+
130+ assert.equal(res.err, [
131+ 'definitions.md',
132+ ' 5:12 warning Link to unknown heading: `world`',
133+ '',
134+ '✖ 1 problem (0 errors, 1 warning)',
135+ '',
136+ 'FOOOO',
137+ ' 0:0 error No such file or directory',
138+ '',
139+ '✖ 1 problem (1 error, 0 warnings)'
140+ ].join('\n'));
141+
142+ done(err);
143+ });
144+ });
145+
146+ it('should work when not all files are given', function (done) {
147+ var stop = intercept();
148+
149+ cli({
150+ 'files': 'example.md',
151+ 'color': false,
152+ 'detectRC': false,
153+ 'detectIgnore': false,
154+ 'plugins': ['slug', './index.js']
155+ }, function (err) {
156+ var res = stop();
157+
158+ assert.equal(res.err, [
159+ 'example.md',
160+ ' 5:37 warning Link to unknown heading: `world`',
161+ ' 21:10 warning Link to unknown file: `examples/world.md`',
162+ ' 23:10 warning Link to unknown file: `examples/world.md`',
163+ ' 27:8 warning Link to unknown file: ' +
164+ '`examples/example.md`',
165+ ' 29:8 warning Link to unknown file: ' +
166+ '`examples/example.md`',
167+ ' 35:10 warning Link to unknown file: ' +
168+ '`examples/example.md`',
169+ ' 37:10 warning Link to unknown file: ' +
170+ '`examples/example.md`',
171+ ' 43:10 warning Link to unknown file: `examples/world.md`',
172+ ' 45:10 warning Link to unknown file: `examples/world.md`',
173+ '',
174+ '✖ 9 problems (0 errors, 9 warnings)'
175+ ].join('\n'));
176+
177+ done(err);
178+ });
179+ });
180+
181+ it('should work when all files are given', function (done) {
182+ var stop = intercept();
183+
184+ cli({
185+ 'files': [
186+ 'example.md',
187+ 'examples/example.md'
188+ ],
189+ 'color': false,
190+ 'detectRC': false,
191+ 'detectIgnore': false,
192+ 'plugins': ['slug', './index.js']
193+ }, function (err) {
194+ var res = stop();
195+
196+ assert.equal(res.out, '');
197+
198+ assert.equal(res.err, [
199+ 'example.md',
200+ ' 5:37 warning Link to unknown heading: `world`',
201+ ' 21:10 warning Link to unknown file: `examples/world.md`',
202+ ' 23:10 warning Link to unknown file: `examples/world.md`',
203+ ' 35:10 warning Link to unknown heading in ' +
204+ '`examples/example.md`: `world`',
205+ ' 37:10 warning Link to unknown heading in ' +
206+ '`examples/example.md`: `world`',
207+ ' 43:10 warning Link to unknown file: `examples/world.md`',
208+ ' 45:10 warning Link to unknown file: `examples/world.md`',
209+ '',
210+ '✖ 7 problems (0 errors, 7 warnings)',
211+ '',
212+ 'examples/example.md',
213+ ' 5:37 warning Link to unknown heading: `world`',
214+ ' 19:10 warning Link to unknown file: `world.md`',
215+ ' 29:10 warning Link to unknown heading in ' +
216+ '`example.md`: `world`',
217+ ' 35:10 warning Link to unknown file: `world.md`',
218+ '',
219+ '✖ 4 problems (0 errors, 4 warnings)'
220+ ].join('\n'));
221+
222+ done(err);
223+ });
224+ });
225+
226+ it('should work with definitions', function (done) {
227+ var stop = intercept();
228+
229+ cli({
230+ 'files': 'definitions.md',
231+ 'color': false,
232+ 'detectRC': false,
233+ 'detectIgnore': false,
234+ 'plugins': ['slug', './index.js']
235+ }, function (err) {
236+ var res = stop();
237+
238+ assert.equal(res.err, [
239+ 'definitions.md',
240+ ' 5:12 warning Link to unknown heading: `world`',
241+ '',
242+ '✖ 1 problem (0 errors, 1 warning)'
243+ ].join('\n'));
244+
245+ done(err);
246+ });
247+ });
248+
249+ it('should work on GitHub URLs when given a repo', function (done) {
250+ var stop = intercept();
251+
252+ cli({
253+ 'files': [
254+ 'example.md',
255+ 'examples/example.md'
256+ ],
257+ 'color': false,
258+ 'detectRC': false,
259+ 'detectIgnore': false,
260+ 'plugins': {
261+ 'slug': null,
262+ './index.js': {
263+ 'repository': 'wooorm/test'
264+ }
265+ }
266+ }, function (err) {
267+ var res = stop();
268+
269+ assert.equal(res.out, '');
270+
271+ assert.equal(res.err, [
272+ 'example.md',
273+ ' 5:37 warning Link to unknown heading: `world`',
274+ ' 17:34 warning Link to unknown file: `examples/world.md`',
275+ ' 19:12 warning Link to unknown file: `examples/world.md`',
276+ ' 21:10 warning Link to unknown file: `examples/world.md`',
277+ ' 23:10 warning Link to unknown file: `examples/world.md`',
278+ ' 35:10 warning Link to unknown heading in ' +
279+ '`examples/example.md`: `world`',
280+ ' 37:10 warning Link to unknown heading in ' +
281+ '`examples/example.md`: `world`',
282+ ' 39:10 warning Link to unknown heading in ' +
283+ '`examples/example.md`: `world`',
284+ ' 41:10 warning Link to unknown heading in ' +
285+ '`examples/example.md`: `world`',
286+ ' 43:10 warning Link to unknown file: `examples/world.md`',
287+ ' 45:10 warning Link to unknown file: `examples/world.md`',
288+ ' 47:10 warning Link to unknown file: `examples/world.md`',
289+ ' 49:10 warning Link to unknown file: `examples/world.md`',
290+ '',
291+ '✖ 13 problems (0 errors, 13 warnings)',
292+ '',
293+ 'examples/example.md',
294+ ' 5:37 warning Link to unknown heading: `world`',
295+ ' 15:34 warning Link to unknown file: `world.md`',
296+ ' 17:12 warning Link to unknown file: `world.md`',
297+ ' 19:10 warning Link to unknown file: `world.md`',
298+ ' 29:10 warning Link to unknown heading in ' +
299+ '`example.md`: `world`',
300+ ' 31:10 warning Link to unknown heading in ' +
301+ '`example.md`: `world`',
302+ ' 33:10 warning Link to unknown heading in ' +
303+ '`example.md`: `world`',
304+ ' 35:10 warning Link to unknown file: `world.md`',
305+ ' 37:10 warning Link to unknown file: `world.md`',
306+ ' 39:10 warning Link to unknown file: `world.md`',
307+ '',
308+ '✖ 10 problems (0 errors, 10 warnings)'
309+ ].join('\n'));
310+
311+ done(err);
312+ });
313+ });
314+
315+ it('should work on GitHub URLs when with package.json', function (done) {
316+ var stop = intercept();
317+
318+ fs.writeFileSync('./package.json', JSON.stringify({
319+ 'repository': 'wooorm/test'
320+ }, 0, 2));
321+
322+ cli({
323+ 'files': [
324+ 'example.md',
325+ 'examples/example.md'
326+ ],
327+ 'color': false,
328+ 'detectRC': false,
329+ 'detectIgnore': false,
330+ 'plugins': [
331+ '../../../node_modules/mdast-slug',
332+ '../../../index.js'
333+ ]
334+ }, function (err) {
335+ var res = stop();
336+
337+ fs.unlinkSync('./package.json');
338+
339+ assert.equal(res.out, '');
340+
341+ assert.equal(res.err, [
342+ 'example.md',
343+ ' 5:37 warning Link to unknown heading: `world`',
344+ ' 17:34 warning Link to unknown file: `examples/world.md`',
345+ ' 19:12 warning Link to unknown file: `examples/world.md`',
346+ ' 21:10 warning Link to unknown file: `examples/world.md`',
347+ ' 23:10 warning Link to unknown file: `examples/world.md`',
348+ ' 35:10 warning Link to unknown heading in ' +
349+ '`examples/example.md`: `world`',
350+ ' 37:10 warning Link to unknown heading in ' +
351+ '`examples/example.md`: `world`',
352+ ' 39:10 warning Link to unknown heading in ' +
353+ '`examples/example.md`: `world`',
354+ ' 41:10 warning Link to unknown heading in ' +
355+ '`examples/example.md`: `world`',
356+ ' 43:10 warning Link to unknown file: `examples/world.md`',
357+ ' 45:10 warning Link to unknown file: `examples/world.md`',
358+ ' 47:10 warning Link to unknown file: `examples/world.md`',
359+ ' 49:10 warning Link to unknown file: `examples/world.md`',
360+ '',
361+ '✖ 13 problems (0 errors, 13 warnings)',
362+ '',
363+ 'examples/example.md',
364+ ' 5:37 warning Link to unknown heading: `world`',
365+ ' 15:34 warning Link to unknown file: `world.md`',
366+ ' 17:12 warning Link to unknown file: `world.md`',
367+ ' 19:10 warning Link to unknown file: `world.md`',
368+ ' 29:10 warning Link to unknown heading in ' +
369+ '`example.md`: `world`',
370+ ' 31:10 warning Link to unknown heading in ' +
371+ '`example.md`: `world`',
372+ ' 33:10 warning Link to unknown heading in ' +
373+ '`example.md`: `world`',
374+ ' 35:10 warning Link to unknown file: `world.md`',
375+ ' 37:10 warning Link to unknown file: `world.md`',
376+ ' 39:10 warning Link to unknown file: `world.md`',
377+ '',
378+ '✖ 10 problems (0 errors, 10 warnings)'
379+ ].join('\n'));
380+
381+ done(err);
382+ });
383+ });
384+});