Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mocha test to check validity of all external links #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "7"

branches:
only:
- master
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "awesome-campus-expert",
"version": "1.0.0",
"description": "A curated list of awesome resources for GitHub Campus Experts",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha './test/**/*_spec.js'"
},
"repository": {
"type": "git",
"url": "git+https://github.com/campus-experts/awesome-campus-expert.git"
},
"author": "GitHub Campus Experts",
"license": "UNLICENSED",
"bugs": {
"url": "https://github.com/campus-experts/awesome-campus-expert/issues"
},
"homepage": "https://github.com/campus-experts/awesome-campus-expert#readme",
"devDependencies": {
"async": "^2.4.0",
"link-check": "^4.0.2",
"lodash": "^4.17.4",
"markdown-link-extractor": "^1.1.0",
"mocha": "^3.4.1",
"should": "^11.2.1"
}
}
33 changes: 33 additions & 0 deletions test/list_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const path = require('path');
const fs = require('fs');
const should = require('should');
const checkExternalLinks = require('./utils/external-link-checker');

describe('Awesome list', function() {

describe('links', function() {

it('should all be valid', function(done) {
// Each link will be tested over http which
// can take quite a while.
this.timeout(15000);

const listPath = path.resolve(__dirname, '../readme.md');

fs.readFile(listPath, 'utf8', function(err, markdown) {
if (err) return done(err);

checkExternalLinks(markdown, function(err, results) {
if (err) return done(err);

results.forEach(function(result) {
result.statusCode.should.equal(200);
result.status.should.equal('alive');
});

done();
});
});
});
});
});
22 changes: 22 additions & 0 deletions test/utils/external-link-checker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');
const _ = require('lodash');
const async = require('async');
const linkCheck = require('link-check');
const markdownLinkExtractor = require('markdown-link-extractor');

module.exports = function checkExternalLinks(markdown, opts, callback) {
if (arguments.length === 2 && typeof opts === 'function') {
callback = opts;
opts = {};
}

let links = _.uniq(markdownLinkExtractor(markdown));
// linkCheck will only check external links
let externalLinks = links.filter(function(link) {
return link.startsWith('http');
});

async.mapLimit(externalLinks, 2, function(link, callback) {
linkCheck(link, opts, callback);
}, callback);
};