Skip to content

Commit

Permalink
Update docs and add CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Dec 30, 2017
1 parent 0a9a065 commit 4e751ac
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/_tools/markdown/to-vdom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ console.log( toHTML( vtree ) );
### Usage

```bash
Usage: markdown-to-vdom [options] [markdown]
Usage: markdown-to-vdom [options] [<markdown>]

Options:

Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/_tools/markdown/to-vdom/bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function onRead( error, data ) {
var md;
if ( error ) {
process.exitCode = 1;
return console.error( error.message ); // eslint-disable-line no-console
return console.error( 'Error: %s', error.message ); // eslint-disable-line no-console
}
md = toVirtualDOM( data.toString() );
console.log( JSON.stringify( md ) ); // eslint-disable-line no-console
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Usage: markdown-to-vdom [options] [markdown]
Usage: markdown-to-vdom [options] [<markdown>]

Options:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tagName":"DIV","properties":{},"children":[{"tagName":"H1","properties":{"id":"user-content-beep"},"children":[{"text":"Beep"}],"key":"h-2","namespace":null,"count":1,"hasWidgets":false,"hasThunks":false,"descendantHooks":false}],"key":"h-1","namespace":null,"count":2,"hasWidgets":false,"hasThunks":false,"descendantHooks":false}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tagName":"DIV","properties":{},"children":[{"tagName":"H1","properties":{"id":"user-content-beep"},"children":[{"text":"Beep"}],"key":"h-2","namespace":null,"count":1,"hasWidgets":false,"hasThunks":false,"descendantHooks":false},{"text":"\n"},{"tagName":"BLOCKQUOTE","properties":{},"children":[{"text":"\n"},{"tagName":"P","properties":{},"children":[{"text":"Boop!"}],"key":"h-4","namespace":null,"count":1,"hasWidgets":false,"hasThunks":false,"descendantHooks":false},{"text":"\n"}],"key":"h-3","namespace":null,"count":4,"hasWidgets":false,"hasThunks":false,"descendantHooks":false}],"key":"h-1","namespace":null,"count":8,"hasWidgets":false,"hasThunks":false,"descendantHooks":false}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var resolve = require( 'path' ).resolve;
var proxyquire = require( 'proxyquire' );

var fpath = resolve( __dirname, '..', 'bin', 'cli' );

process.stdin.isTTY = false;

proxyquire( fpath, {
'@stdlib/utils/read-stdin': stdin
});

function stdin( clbk ) {
clbk( new Error( 'beep' ) );
}
214 changes: 214 additions & 0 deletions lib/node_modules/@stdlib/_tools/markdown/to-vdom/test/test.cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var exec = require( 'child_process' ).exec;
var tape = require( 'tape' );
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var replace = require( '@stdlib/string/replace' );


// VARIABLES //

var fpath = resolve( __dirname, '..', 'bin', 'cli' );
var opts = {
'skip': IS_BROWSER || IS_WINDOWS
};


// FIXTURES //

var PKG_VERSION = require( './../package.json' ).version;


// TESTS //

tape( 'command-line interface', function test( t ) {
t.ok( true, __filename );
t.end();
});

tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
var expected;
var cmd;

expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
});
cmd = [
process.execPath,
fpath,
'--help'
];

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
}
t.end();
}
});

tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
var expected;
var cmd;

expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
});
cmd = [
process.execPath,
fpath,
'-h'
];

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
}
t.end();
}
});

tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
var cmd = [
process.execPath,
fpath,
'--version'
];

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
}
t.end();
}
});

tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
var cmd = [
process.execPath,
fpath,
'-V'
];

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
}
t.end();
}
});

tape( 'the command-line interface prints a virtual DOM representation of a Markdown string', opts, function test( t ) {
var expected;
var file;
var cmd;

cmd = [
process.execPath,
'-e',
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'# Beep\'; require( \''+fpath+'\' );"'
];

file = resolve( __dirname, 'fixtures', 'expected1.txt' );
expected = readFileSync( file );

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), expected.toString(), 'expected value' );
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
}
t.end();
}
});

tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) {
var expected;
var file;
var cmd;

cmd = [
'echo \'# Beep\n\n> Boop!\'',
'|',
process.execPath,
fpath
];

file = resolve( __dirname, 'fixtures', 'expected2.txt' );
expected = readFileSync( file );

exec( cmd.join( ' ' ), done );

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), expected.toString(), 'expected value' );
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
}
t.end();
}
});

tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) {
var script;
var opts;
var cmd;

script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), {
'encoding': 'utf8'
});

// Replace single quotes with double quotes:
script = replace( script, '\'', '"' );

cmd = [
process.execPath,
'-e',
'\''+script+'\''
];

opts = {
'cwd': __dirname
};

exec( cmd.join( ' ' ), opts, done );

function done( error, stdout, stderr ) {
if ( error ) {
t.pass( error.message );
t.strictEqual( error.code, 1, 'expected exit code' );
}
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' );
t.end();
}
});

0 comments on commit 4e751ac

Please sign in to comment.