Get all tape tests correct

This commit is contained in:
Tyler Long
2017-04-11 21:52:19 +08:00
parent bc94fc3d6d
commit fc96360e17
3 changed files with 109 additions and 114 deletions

View File

@@ -1,90 +1,89 @@
var fs = require('fs') var fs = require('fs'),
, exec = require('child_process').exec exec = require('child_process').exec,
, chalk = require('chalk') chalk = require('chalk'),
, which = require('which') which = require('which'),
, parseArgs = require('minimist') parseArgs = require('minimist'),
, semver = require('semver') semver = require('semver'),
, path = require('path') path = require('path')
var PHANTOM_VERSION = "^2.1.0" var PHANTOM_VERSION = '^2.1.0'
var info = chalk.blue.bold var info = chalk.blue.bold,
, note = chalk.green.bold note = chalk.green.bold
module.exports = function() { module.exports = (function () {
return new cli() return new cli()
}() }())
function cli(options) { function cli (options) {
this.options = { this.options = {
alias: { alias: {
help: 'h' help: 'h',
, png: 'p' png: 'p',
, outputDir: 'o' outputDir: 'o',
, outputSuffix: 'O' outputSuffix: 'O',
, svg: 's' svg: 's',
, verbose: 'v' verbose: 'v',
, phantomPath: 'e' phantomPath: 'e',
, sequenceConfig: 'c' sequenceConfig: 'c',
, ganttConfig: 'g' ganttConfig: 'g',
, css: 't' css: 't',
, width: 'w' width: 'w'
} },
, 'boolean': ['help', 'png', 'svg', 'verbose'] 'boolean': ['help', 'png', 'svg', 'verbose'],
, 'string': ['outputDir', 'outputSuffix'] 'string': ['outputDir', 'outputSuffix']
} }
this.errors = [] this.errors = []
this.message = null this.message = null
this.helpMessage = [ this.helpMessage = [,
, info('Usage: mermaid [options] <file>...') info('Usage: mermaid [options] <file>...'),
, "" '',
, "file The mermaid description file to be rendered" 'file The mermaid description file to be rendered',
, "" '',
, "Options:" 'Options:',
, " -s --svg Output SVG instead of PNG (experimental)" ' -s --svg Output SVG instead of PNG (experimental)',
, " -p --png If SVG was selected, and you also want PNG, set this flag" ' -p --png If SVG was selected, and you also want PNG, set this flag',
, " -o --outputDir Directory to save files, will be created automatically, defaults to `cwd`" ' -o --outputDir Directory to save files, will be created automatically, defaults to `cwd`',
, " -O --outputSuffix Suffix to output filenames in front of '.svg' or '.png', defaults to ''" " -O --outputSuffix Suffix to output filenames in front of '.svg' or '.png', defaults to ''",
, " -e --phantomPath Specify the path to the phantomjs executable" ' -e --phantomPath Specify the path to the phantomjs executable',
, " -t --css Specify the path to a CSS file to be included when processing output" ' -t --css Specify the path to a CSS file to be included when processing output',
, " -c --sequenceConfig Specify the path to the file with the configuration to be applied in the sequence diagram" ' -c --sequenceConfig Specify the path to the file with the configuration to be applied in the sequence diagram',
, " -g --ganttConfig Specify the path to the file with the configuration to be applied in the gantt diagram" ' -g --ganttConfig Specify the path to the file with the configuration to be applied in the gantt diagram',
, " -h --help Show this message" ' -h --help Show this message',
, " -v --verbose Show logging" ' -v --verbose Show logging',
, " -w --width width of the generated png (number)" ' -w --width width of the generated png (number)',
, " --version Print version and quit" ' --version Print version and quit'
] ]
return this return this
} }
cli.prototype.parse = function(argv, next) { cli.prototype.parse = function (argv, next) {
var options = parseArgs(argv, this.options) this.errors = [] // clear errors
, phantom var options = parseArgs(argv, this.options),
phantom
if (options.version) { if (options.version) {
var pkg = require('../package.json') var pkg = require('../package.json')
this.message = "" + pkg.version this.message = '' + pkg.version
next(null, this.message) next(null, this.message)
} } else if (options.help) {
else if (options.help) {
this.message = this.helpMessage.join('\n') this.message = this.helpMessage.join('\n')
next(null, this.message) next(null, this.message)
} } else {
else {
options.files = options._ options.files = options._
if (!options.files.length) { if (!options.files.length) {
this.errors.push(new Error("You must specify at least one source file.")) this.errors.push(new Error('You must specify at least one source file.'))
} }
// ensure that parameter-expecting options have parameters // ensure that parameter-expecting options have parameters
;['outputDir', 'outputSuffix', 'phantomPath', 'sequenceConfig', 'ganttConfig', 'css'].forEach(function(i) { ;['outputDir', 'outputSuffix', 'phantomPath', 'sequenceConfig', 'ganttConfig', 'css'].forEach(function (i) {
if(typeof options[i] !== 'undefined') { if (typeof options[i] !== 'undefined') {
if (typeof options[i] !== 'string' || options[i].length < 1) { if (typeof options[i] !== 'string' || options[i].length < 1) {
this.errors.push(new Error(i + " expects a value.")) this.errors.push(new Error(i + ' expects a value.'))
} }
} }
}.bind(this)) }.bind(this))
@@ -92,15 +91,13 @@ cli.prototype.parse = function(argv, next) {
// set svg/png flags appropriately // set svg/png flags appropriately
if (options.svg && !options.png) { if (options.svg && !options.png) {
options.png = false options.png = false
} } else {
else {
options.png = true options.png = true
} }
if (options.sequenceConfig) { if (options.sequenceConfig) {
try { try {
fs.accessSync(options.sequenceConfig, fs.R_OK) fs.accessSync(options.sequenceConfig, fs.R_OK)
} catch (err) { } catch (err) {
this.errors.push(err) this.errors.push(err)
} }
@@ -111,7 +108,6 @@ cli.prototype.parse = function(argv, next) {
if (options.ganttConfig) { if (options.ganttConfig) {
try { try {
fs.accessSync(options.ganttConfig, fs.R_OK) fs.accessSync(options.ganttConfig, fs.R_OK)
} catch (err) { } catch (err) {
this.errors.push(err) this.errors.push(err)
} }
@@ -122,23 +118,22 @@ cli.prototype.parse = function(argv, next) {
if (options.css) { if (options.css) {
try { try {
fs.accessSync(options.css, fs.R_OK) fs.accessSync(options.css, fs.R_OK)
} catch (err) { } catch (err) {
this.errors.push(err) this.errors.push(err)
} }
} else { } else {
options.css = path.join(__dirname, '..', 'dist', 'mermaid.css') options.css = path.join(__dirname, '..', 'dist', 'mermaid.css')
} }
// set svg/png flags appropriately // set svg/png flags appropriately
if(!options.width){ if (!options.width) {
options.width = 1200; options.width = 1200
} }
this.checkPhantom = createCheckPhantom(options.phantomPath) this.checkPhantom = createCheckPhantom(options.phantomPath)
this.checkPhantom(function(err, path) { this.checkPhantom(function (err, path) {
if(err) { if (err) {
this.errors.push(err) this.errors.push(err)
} }
options.phantomPath = path options.phantomPath = path
@@ -151,13 +146,13 @@ cli.prototype.parse = function(argv, next) {
} }
} }
function createCheckPhantom(_phantomPath) { function createCheckPhantom (_phantomPath) {
var phantomPath = _phantomPath var phantomPath = _phantomPath,
, phantomVersion phantomVersion
return function checkPhantom(_next) { return function checkPhantom (_next) {
var next = _next || function() {} var next = _next || function () {},
, err err
if (typeof phantomPath === 'undefined') { if (typeof phantomPath === 'undefined') {
try { try {
@@ -171,10 +166,10 @@ function createCheckPhantom(_phantomPath) {
phantomPath = null phantomPath = null
err = new Error( err = new Error(
[ [
"Cannot find phantomjs in your PATH. If phantomjs is installed" 'Cannot find phantomjs in your PATH. If phantomjs is installed',
, "you may need to specify its path manually with the '-e' option." "you may need to specify its path manually with the '-e' option.",
, "Run this executable with '--help' or view the README for more" "Run this executable with '--help' or view the README for more",
, "details." 'details.'
].join('\n') ].join('\n')
) )
@@ -186,19 +181,17 @@ function createCheckPhantom(_phantomPath) {
} }
// If we have phantompath, see if its version satisfies our requirements // If we have phantompath, see if its version satisfies our requirements
exec('"' + phantomPath + '" --version', function(err, stdout, stderr) { exec('"' + phantomPath + '" --version', function (err, stdout, stderr) {
if (err) { if (err) {
next(new Error("Could not find phantomjs at the specified path.")) next(new Error('Could not find phantomjs at the specified path.'))
} } else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
next(new Error( next(new Error(
'mermaid requires phantomjs ' 'mermaid requires phantomjs ' +
+ PHANTOM_VERSION PHANTOM_VERSION +
+ ' to be installed, found version ' ' to be installed, found version ' +
+ stdout stdout
)) ))
} } else {
else {
next(null, phantomPath) next(null, phantomPath)
} }
}) })

View File

@@ -102,6 +102,19 @@ test('setting a css source file succeeds', function (t) {
}) })
}) })
test('setting an output directory incorrectly causes an error', function (t) {
t.plan(1)
const cli = require(cliPath)
const argv = ['-o']
cli.parse(argv, function (err) {
t.ok(err, 'an error is raised')
t.end()
})
})
test('a callback function is called after parsing', function (t) { test('a callback function is called after parsing', function (t) {
t.plan(3) t.plan(3)
@@ -116,16 +129,3 @@ test('a callback function is called after parsing', function (t) {
t.end() t.end()
}) })
}) })
test('setting an output directory incorrectly causes an error', function (t) {
t.plan(1)
const cli = require(cliPath)
const argv = ['-o']
cli.parse(argv, function (err) {
t.ok(err, 'an error is raised')
t.end()
})
})

View File

@@ -37,6 +37,7 @@ function execCmd (cmd, verify) {
function verifyNoError (t) { function verifyNoError (t) {
return function (error, stdout, stderr) { return function (error, stdout, stderr) {
t.ok(!error, 'no error')
t.notOk(stderr, 'no stderr') t.notOk(stderr, 'no stderr')
t.end() t.end()
} }
@@ -44,25 +45,26 @@ function verifyNoError (t) {
function verifyError (t) { function verifyError (t) {
return function (error, stdout, stderr) { return function (error, stdout, stderr) {
t.ok(stderr, 'should get error') t.ok(!error, 'no error')
t.ok(stderr, 'should get stderr')
t.end() t.end()
} }
} }
test('mermaid cli help', function (t) { test('mermaid cli help', function (t) {
t.plan(1) t.plan(2)
const args = ['--help'] const args = ['--help']
execMermaid(args.join(' '), verifyNoError(t)) execMermaid(args.join(' '), verifyNoError(t))
}) })
test('mermaid cli help', function (t) { test('mermaid cli help', function (t) {
t.plan(1) t.plan(2)
const args = ['--badopt'] const args = ['--badopt']
execMermaid(args.join(' '), verifyError(t)) execMermaid(args.join(' '), verifyError(t))
}) })
test.skip('sequence syntax error', function (t) { test.skip('sequence syntax error', function (t) {
t.plan(1) t.plan(2)
const args = ['--svg', const args = ['--svg',
testDir + 'sequence_err.mmd' testDir + 'sequence_err.mmd'
] ]
@@ -71,7 +73,7 @@ test.skip('sequence syntax error', function (t) {
['', 'fo', 'tspan', 'old'].forEach(function (textPlacement) { ['', 'fo', 'tspan', 'old'].forEach(function (textPlacement) {
test('sequence svg text placelment: ' + textPlacement, function (t) { test('sequence svg text placelment: ' + textPlacement, function (t) {
t.plan(1) t.plan(2)
const args = ['--svg', const args = ['--svg',
'--outputDir=' + testDir, '--outputDir=' + testDir,
'--outputSuffix=' + (textPlacement ? '_' + textPlacement : '') + '.actual', '--outputSuffix=' + (textPlacement ? '_' + textPlacement : '') + '.actual',
@@ -83,7 +85,7 @@ test.skip('sequence syntax error', function (t) {
}) })
test('sequence png', function (t) { test('sequence png', function (t) {
t.plan(1) t.plan(2)
const args = ['--png', const args = ['--png',
testDir + 'sequence_text.mmd' testDir + 'sequence_text.mmd'
] ]
@@ -91,7 +93,7 @@ test('sequence png', function (t) {
}) })
test('flowchart svg text', function (t) { test('flowchart svg text', function (t) {
t.plan(1) t.plan(2)
const args = ['--svg', const args = ['--svg',
'--css=dist/mermaid.css', '--css=dist/mermaid.css',
'--width=500', '--width=500',
@@ -102,7 +104,7 @@ test('flowchart svg text', function (t) {
['svg', 'png'].forEach(function (format) { ['svg', 'png'].forEach(function (format) {
test('flowchart ' + format + 'text2', function (t) { test('flowchart ' + format + 'text2', function (t) {
t.plan(1) t.plan(2)
const args = ['--' + format, const args = ['--' + format,
'--css=dist/mermaid.forest.css', '--css=dist/mermaid.forest.css',
'--width=500', '--width=500',
@@ -113,7 +115,7 @@ test('flowchart svg text', function (t) {
}) })
test('gantt axis formatter svg', function (t) { test('gantt axis formatter svg', function (t) {
t.plan(1) t.plan(2)
const args = ['--svg', const args = ['--svg',
'--css=dist/mermaid.css', '--css=dist/mermaid.css',
'--width=500', '--width=500',
@@ -124,7 +126,7 @@ test('gantt axis formatter svg', function (t) {
}) })
test('gitgraph sample svg', function (t) { test('gitgraph sample svg', function (t) {
t.plan(1) t.plan(2)
const args = ['-s', '-v', const args = ['-s', '-v',
'--width=500', '--width=500',
testDir + 'gitgraph.mmd' testDir + 'gitgraph.mmd'
@@ -133,7 +135,7 @@ test('gitgraph sample svg', function (t) {
}) })
test('load sample.html in phantomjs and save screenshot png', function (t) { test('load sample.html in phantomjs and save screenshot png', function (t) {
t.plan(1) t.plan(2)
execPhantomjsToLoadHtmlSaveScreenshotPng(testDir + 'samples.html', execPhantomjsToLoadHtmlSaveScreenshotPng(testDir + 'samples.html',
verifyNoError(t)) verifyNoError(t))
}) })