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 409397a commit 0a9a065
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/_tools/markdown/to-html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function done( error, html ) {
### Usage

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

Options:

Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/_tools/markdown/to-html/bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function onRead( error, data ) {
function done( error, out ) {
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
}
console.log( out ); // eslint-disable-line no-console
} // end FUNCTION done()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

Options:

Expand Down
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' ) );
}
216 changes: 216 additions & 0 deletions lib/node_modules/@stdlib/_tools/markdown/to-html/test/test.cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
'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 generates an HTML string from Markdown', opts, function test( t ) {
var expected;
var cmd;

cmd = [
process.execPath,
'-e',
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'# Beep\'; require( \''+fpath+'\' );"'
];
expected = '<h1 id="beep">Beep</h1>\n\n';

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

function done( error, stdout, stderr ) {
console.log( error );
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), expected, '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 cmd;

cmd = [
'echo \'# Beep\n\n> Boop!\'',
'|',
process.execPath,
fpath
];
expected = [
'<h1 id="beep">Beep</h1>',
'<blockquote>',
'<p>Boop!</p>',
'</blockquote>',
'',
''
].join( '\n' );

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

function done( error, stdout, stderr ) {
if ( error ) {
t.fail( error.message );
} else {
t.strictEqual( stdout.toString(), expected, '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 0a9a065

Please sign in to comment.