Skip to content

Commit

Permalink
Merge pull request #53 from Simonwep/master
Browse files Browse the repository at this point in the history
Use commander as CLI Library and add --config option
  • Loading branch information
Widdershin authored Feb 10, 2020
2 parents 6a897f1 + 454c99a commit f4bf6e8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 83 deletions.
102 changes: 49 additions & 53 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,73 @@
#! /usr/bin/env node
'use strict';

var doctest = require('..');

var fs = require('fs');

var glob = require('glob');

var CONFIG_FILEPATH = process.cwd() + '/.markdown-doctest-setup.js';
var DEFAULT_GLOB = '**/*.+(md|markdown)';
var DEFAULT_IGNORE = [
const {version} = require('../package');
const doctest = require('..');
const program = require('commander');
const path = require('path');
const glob = require('glob');
const fs = require('fs');

const DEFAULT_GLOB = '**/*.+(md|markdown)';
const DEFAULT_IGNORE = [
'**/node_modules/**',
'**/bower_components/**'
];

function displayHelp () {
const helpText = [
'Usage: markdown-doctest [glob]',
'Options:',
' -h, --help output help text'
];

console.log(helpText.join('\n'));
}

function main () {
var userGlob = process.argv[2];
var config = {require: {}};

if (process.argv.indexOf('--help') !== -1 || process.argv.indexOf('-h') !== -1) {
displayHelp();

process.exitCode = 0;

return;
}

if (fs.existsSync(CONFIG_FILEPATH)) {
// Config
const config = {
require: {},
globals: {},
ignore: []
};

// Setup commander
program
.name('markdown-doctest')
.description('Test all the code in your markdown docs!')
.version(version, '-v, --version', 'output the current version')
.helpOption('-h, --help', 'output usage informations')
.option('-c, --config <path>', 'custom config location', path.join(process.cwd(), '/.markdown-doctest-setup.js'))
.parse(process.argv);

// Parse config file
if (program.config) {
const configPath = path.resolve(program.config);

if (fs.existsSync(configPath)) {
try {
config = require(CONFIG_FILEPATH);

// Apply custom settings
Object.assign(config, require(configPath));
} catch (e) {
console.log('Error running .markdown-doctest-setup.js:');
console.error(e);
process.exitCode = 1;
return;
console.error(`Cannot resolve "${configPath}"`);
process.exit(1);
}
}
}

var ignoredDirectories = config.ignore || [];

glob(
userGlob || DEFAULT_GLOB,
{ignore: DEFAULT_IGNORE.concat(ignoredDirectories)},
run
);
// Resolve files
glob(
program.args[0] || DEFAULT_GLOB,
{
ignore: [...config.ignore, ...DEFAULT_IGNORE]
},
(err, files) => {

function run (err, files) {
if (err) {
console.trace(err);
}

var results = doctest.runTests(files, config);
// Run tests
const results = doctest.runTests(files, config);

console.log('\n');

doctest.printResults(results);

var failures = results.filter(function (result) { return result.status === 'fail'; });

// Exit with error-code if any test failed
const failures = results.filter(result => result.status === 'fail');
if (failures.length > 0) {
process.exitCode = 1;
process.exit(1);
}
}
}

main();
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"chalk": "^2.4.2",
"commander": "^4.0.1",
"glob": "^7.1.4"
},
"babel": {
Expand Down
60 changes: 30 additions & 30 deletions test/markdown-doctest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('runTests', () => {

const passingResults = results.filter(result => result.status === 'pass');

assert.equal(passingResults.length, 1);
assert.strictEqual(passingResults.length, 1);
});

it('fail', () => {
Expand All @@ -29,8 +29,8 @@ describe('runTests', () => {
const passingResults = results.filter(result => result.status === 'pass');
const failingResults = results.filter(result => result.status === 'fail');

assert.equal(passingResults.length, 1, JSON.stringify(results, null, 2));
assert.equal(failingResults.length, 2);
assert.strictEqual(passingResults.length, 1, JSON.stringify(results, null, 2));
assert.strictEqual(failingResults.length, 2);
});

it('skip', () => {
Expand All @@ -42,9 +42,9 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 1);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 1);
assert.strictEqual(passingResults.length, 1);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 1);
});

it('config', () => {
Expand All @@ -61,9 +61,9 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 1, results[0].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 1, results[0].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});

it('globals', () => {
Expand All @@ -80,9 +80,9 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 1, results[0].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 1, results[0].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});

it('es6', () => {
Expand All @@ -95,9 +95,9 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 2, results[0].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 2, results[0].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});

it('joins tests', () => {
Expand All @@ -110,17 +110,17 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 3, results[1].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 3, results[1].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});

it('supports regex imports', () => {
const files = [getTestFilePath('require-override.md')];
const config = {
regexRequire: {
'lo(.*)': function (fullPath, matchedName) {
assert.equal(matchedName, 'dash');
assert.strictEqual(matchedName, 'dash');

return {
range: () => []
Expand All @@ -135,16 +135,16 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 1, results[0].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 1, results[0].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});

it('runs the beforeEach hook prior to each example', () => {
const files = [getTestFilePath('before-each.md')];
const a = {
value: 0
}
};

const config = {
globals: {
Expand All @@ -160,11 +160,11 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 3, results[0].stack);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 3, results[0].stack);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);

assert.equal(a.value, 1);
assert.strictEqual(a.value, 1);
});

it('ignores json examples', () => {
Expand All @@ -177,8 +177,8 @@ describe('runTests', () => {
const failingResults = results.filter(result => result.status === 'fail');
const skippedResults = results.filter(result => result.status === 'skip');

assert.equal(passingResults.length, 0);
assert.equal(failingResults.length, 0);
assert.equal(skippedResults.length, 0);
assert.strictEqual(passingResults.length, 0);
assert.strictEqual(failingResults.length, 0);
assert.strictEqual(skippedResults.length, 0);
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==

commander@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-4.0.1.tgz#b67622721785993182e807f4883633e6401ba53c"
integrity sha512-IPF4ouhCP+qdlcmCedhxX4xiGBPyigb8v5NeUp+0LyhwLgxMqyp3S0vl7TAPfS/hiP7FC3caI/PB9lTmP8r1NA==

component-emitter@^1.2.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
Expand Down

0 comments on commit f4bf6e8

Please sign in to comment.