diff --git a/bin/cmd.js b/bin/cmd.js index 9d08b9f..c80c4c7 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -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 ', '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(); +); diff --git a/package.json b/package.json index 2fc3684..a63044d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/markdown-doctest.test.js b/test/markdown-doctest.test.js index 0c91bd6..238a739 100644 --- a/test/markdown-doctest.test.js +++ b/test/markdown-doctest.test.js @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -110,9 +110,9 @@ 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', () => { @@ -120,7 +120,7 @@ describe('runTests', () => { const config = { regexRequire: { 'lo(.*)': function (fullPath, matchedName) { - assert.equal(matchedName, 'dash'); + assert.strictEqual(matchedName, 'dash'); return { range: () => [] @@ -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: { @@ -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', () => { @@ -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); }); }); diff --git a/yarn.lock b/yarn.lock index a6dba72..63bbf48 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1316,6 +1316,11 @@ commander@2.20.0: 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"