Skip to content

Commit

Permalink
Refactor tests for changes in mdast
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 24, 2015
1 parent a93303c commit f8fc60d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 119 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ function gatherReferences(file, project) {
var filePath = file.filePath();
var directory = file.directory;
var space = file.namespace('mdast');
var ast = space.tree || space.ast;
var getDefinition;
var prefix = '';
var ast = space.tree || /* istanbul ignore next */ space.ast;

getDefinition = definitions(ast);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
"index.js"
],
"devDependencies": {
"cept": "^1.0.0",
"eslint": "^1.0.0",
"hook-std": "^0.1.0",
"istanbul": "^0.4.0",
"jscs": "^2.0.0",
"jscs-jsdoc": "^1.0.0",
"mdast": "^2.2.2",
"mdast-comment-config": "^1.0.0",
"mdast-github": "^1.0.0",
"mdast-toc": "^1.0.0",
"mdast-lint": "^1.0.0",
"mdast-toc": "^1.0.0",
"mocha": "^2.0.0"
},
"scripts": {
Expand Down
185 changes: 69 additions & 116 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

var assert = require('assert');
var hook = require('hook-std');
var fs = require('fs');
var cept = require('cept');
var mdast = require('mdast');
var cli = require('mdast/lib/cli');
var links = require('..');
Expand All @@ -27,59 +27,37 @@ var links = require('..');

var equal = assert.strictEqual;

/*
* Cache to store output and errors by mdast.
*/

var std = {
'out': [],
'err': []
};

/**
* Factory to store a bound type.
* Test the cli.
*
* @param {string} type - `"out"` or `"err"`.
* @return {Function} - Stores the bound type.
* @param {Object} config - Configuration.
* @param {Function} callback - Completion handler.
*/
function queue(type) {
/**
* Store a bound type.
*/
return function () {
std[type].push([].slice.call(arguments).join(''));
};
}
function test(config, callback) {
var stdout = [];
var stderr = [];
var opts = {};
var unhookStdout;
var unhookStderr;

/**
* Intercept both `console.log` to `std.out` and
* `console.error` to `std.err`;
*
* @return {Function} - Stores output.
*/
function intercept() {
var stopOut = cept(console, 'log', queue('out'));
var stopErr = cept(console, 'error', queue('err'));

/**
* Stops intercepting. Returns the captured `std`.
*
* @return {Object} - Output.
*/
return function () {
var current = {
'out': std.out.join('\n').trim(),
'err': std.err.join('\n').trim()
};

stopOut();
stopErr();

std.out = [];
std.err = [];

return current;
};
opts.silent = true;

unhookStdout = hook.stdout(opts, [].push.bind(stdout));
unhookStderr = hook.stderr(opts, [].push.bind(stderr));

cli(config, function (err) {
unhookStdout();
unhookStderr();

if (err) {
return callback(err);
}

return callback(null, {
'stdout': stdout.join(''),
'stderr': stderr.join('')
});
});
}

/*
Expand All @@ -102,20 +80,16 @@ describe('mdast-validate-links', function () {
});

it('should warn when used without slugs', function (done) {
var stop = intercept();

cli({
test({
'files': 'example.md',
'color': false,
'detectRC': false,
'detectIgnore': false,
'plugins': [
'../../../index.js'
]
}, function (err) {
var res = stop();

equal(res.err.split('\n').slice(0, 2).join('\n'), [
}, function (err, res) {
equal(res.stderr.split('\n').slice(0, 2).join('\n'), [
'example.md',
' 1:1 error Error: Missing slugs. Use for ' +
'example `mdast-slug` to generate heading IDs'
Expand All @@ -126,9 +100,7 @@ describe('mdast-validate-links', function () {
});

it('should ignore unfound files', function (done) {
var stop = intercept();

cli({
test({
'files': ['definitions.md', 'FOOOO'],
'color': false,
'detectRC': false,
Expand All @@ -137,29 +109,26 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();
}, function (err, res) {
equal(res.stdout, '');

equal(res.out, '');

equal(res.err, [
equal(res.stderr, [
'FOOOO',
' 1:1 error No such file or directory',
'',
'definitions.md',
' 5:12-5:21 warning Link to unknown heading: `world`',
'',
'2 messages (✖ 1 error, ⚠ 1 warning)'
'2 messages (✖ 1 error, ⚠ 1 warning)',
''
].join('\n'));

done(err);
});
});

it('should work when not all files are given', function (done) {
var stop = intercept();

cli({
test({
'files': 'example.md',
'color': false,
'detectRC': false,
Expand All @@ -168,10 +137,8 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();

equal(res.err, [
}, function (err, res) {
equal(res.stderr, [
'example.md',
' 5:37-5:51 warning Link to unknown heading: `world`',
' 23:10-23:37 warning Link to unknown file: ' +
Expand All @@ -191,17 +158,16 @@ describe('mdast-validate-links', function () {
' 47:10-47:38 warning Link to unknown heading in ' +
'`examples/world.md`: `hello`',
'',
'⚠ 9 warnings'
'⚠ 9 warnings',
''
].join('\n'));

done(err);
});
});

it('should work when all files are given', function (done) {
var stop = intercept();

cli({
test({
'files': [
'example.md',
'examples/example.md'
Expand All @@ -213,12 +179,10 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();
}, function (err, res) {
equal(res.stdout, '');

equal(res.out, '');

equal(res.err, [
equal(res.stderr, [
'example.md',
' 5:37-5:51 warning Link to unknown heading: `world`',
' 23:10-23:37 warning Link to unknown file: ' +
Expand Down Expand Up @@ -250,17 +214,16 @@ describe('mdast-validate-links', function () {
' 35:10-35:32 warning Link to unknown heading in ' +
'`world.md`: `hello`',
'',
'⚠ 14 warnings'
'⚠ 14 warnings',
''
].join('\n'));

done(err);
});
});

it('should work with definitions', function (done) {
var stop = intercept();

cli({
test({
'files': 'definitions.md',
'color': false,
'detectRC': false,
Expand All @@ -269,24 +232,21 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();

equal(res.err, [
}, function (err, res) {
equal(res.stderr, [
'definitions.md',
' 5:12-5:21 warning Link to unknown heading: `world`',
'',
'⚠ 1 warning'
'⚠ 1 warning',
''
].join('\n'));

done(err);
});
});

it('should work on GitHub URLs when given a repo', function (done) {
var stop = intercept();

cli({
test({
'files': [
'example.md',
'examples/example.md'
Expand All @@ -300,12 +260,10 @@ describe('mdast-validate-links', function () {
'repository': 'wooorm/test'
}
}
}, function (err) {
var res = stop();
}, function (err, res) {
equal(res.stdout, '');

equal(res.out, '');

equal(res.err, [
equal(res.stderr, [
'example.md',
' 5:37-5:51 warning Link to unknown heading: ' +
'`world`',
Expand Down Expand Up @@ -370,21 +328,20 @@ describe('mdast-validate-links', function () {
' 39:10-39:73 warning Link to unknown file: ' +
'`world.md`',
'',
'⚠ 30 warnings'
'⚠ 30 warnings',
''
].join('\n'));

done(err);
});
});

it('should work on GitHub URLs when with package.json', function (done) {
var stop = intercept();

fs.writeFileSync('./package.json', JSON.stringify({
'repository': 'wooorm/test'
}, 0, 2));

cli({
test({
'files': [
'example.md',
'examples/example.md'
Expand All @@ -396,12 +353,10 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();

}, function (err, res) {
fs.unlinkSync('./package.json');

equal(res.err, [
equal(res.stderr, [
'example.md',
' 5:37-5:51 warning Link to unknown heading: ' +
'`world`',
Expand Down Expand Up @@ -466,17 +421,16 @@ describe('mdast-validate-links', function () {
' 39:10-39:73 warning Link to unknown file: ' +
'`world.md`',
'',
'⚠ 30 warnings'
'⚠ 30 warnings',
''
].join('\n'));

done(err);
});
});

it('should suggest similar links', function (done) {
var stop = intercept();

cli({
test({
'files': ['suggestions.md'],
'color': false,
'detectRC': false,
Expand All @@ -485,17 +439,16 @@ describe('mdast-validate-links', function () {
'../../../node_modules/mdast-slug',
'../../../index.js'
]
}, function (err) {
var res = stop();

equal(res.err, [
}, function (err, res) {
equal(res.stderr, [
'suggestions.md',
' 3:22-3:37 warning Link to unknown heading: `helloo`. ' +
'Did you mean `hello`',
' 7:17-7:40 warning Link to unknown heading in ' +
'`example.md`: `fiiiles`. Did you mean `files`',
'',
'⚠ 2 warnings'
'⚠ 2 warnings',
''
].join('\n'));

done(err);
Expand Down

0 comments on commit f8fc60d

Please sign in to comment.