|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// MODULES // |
| 4 | + |
| 5 | +var resolve = require( 'path' ).resolve; |
| 6 | +var exec = require( 'child_process' ).exec; |
| 7 | +var tape = require( 'tape' ); |
| 8 | +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); |
| 9 | +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); |
| 10 | +var readFileSync = require( '@stdlib/fs/read-file' ).sync; |
| 11 | + |
| 12 | + |
| 13 | +// VARIABLES // |
| 14 | + |
| 15 | +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); |
| 16 | +var opts = { |
| 17 | + 'skip': IS_BROWSER || IS_WINDOWS |
| 18 | +}; |
| 19 | + |
| 20 | + |
| 21 | +// FIXTURES // |
| 22 | + |
| 23 | +var PKG_VERSION = require( './../package.json' ).version; |
| 24 | + |
| 25 | + |
| 26 | +// TESTS // |
| 27 | + |
| 28 | +tape( 'command-line interface', function test( t ) { |
| 29 | + t.ok( true, __filename ); |
| 30 | + t.end(); |
| 31 | +}); |
| 32 | + |
| 33 | +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { |
| 34 | + var expected; |
| 35 | + var cmd; |
| 36 | + |
| 37 | + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { |
| 38 | + 'encoding': 'utf8' |
| 39 | + }); |
| 40 | + cmd = [ |
| 41 | + process.execPath, |
| 42 | + fpath, |
| 43 | + '--help' |
| 44 | + ]; |
| 45 | + |
| 46 | + exec( cmd.join( ' ' ), done ); |
| 47 | + |
| 48 | + function done( error, stdout, stderr ) { |
| 49 | + if ( error ) { |
| 50 | + t.fail( error.message ); |
| 51 | + } else { |
| 52 | + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); |
| 53 | + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); |
| 54 | + } |
| 55 | + t.end(); |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { |
| 60 | + var expected; |
| 61 | + var cmd; |
| 62 | + |
| 63 | + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { |
| 64 | + 'encoding': 'utf8' |
| 65 | + }); |
| 66 | + cmd = [ |
| 67 | + process.execPath, |
| 68 | + fpath, |
| 69 | + '-h' |
| 70 | + ]; |
| 71 | + |
| 72 | + exec( cmd.join( ' ' ), done ); |
| 73 | + |
| 74 | + function done( error, stdout, stderr ) { |
| 75 | + if ( error ) { |
| 76 | + t.fail( error.message ); |
| 77 | + } else { |
| 78 | + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); |
| 79 | + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); |
| 80 | + } |
| 81 | + t.end(); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { |
| 86 | + var cmd = [ |
| 87 | + process.execPath, |
| 88 | + fpath, |
| 89 | + '--version' |
| 90 | + ]; |
| 91 | + |
| 92 | + exec( cmd.join( ' ' ), done ); |
| 93 | + |
| 94 | + function done( error, stdout, stderr ) { |
| 95 | + if ( error ) { |
| 96 | + t.fail( error.message ); |
| 97 | + } else { |
| 98 | + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); |
| 99 | + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); |
| 100 | + } |
| 101 | + t.end(); |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { |
| 106 | + var cmd = [ |
| 107 | + process.execPath, |
| 108 | + fpath, |
| 109 | + '-V' |
| 110 | + ]; |
| 111 | + |
| 112 | + exec( cmd.join( ' ' ), done ); |
| 113 | + |
| 114 | + function done( error, stdout, stderr ) { |
| 115 | + if ( error ) { |
| 116 | + t.fail( error.message ); |
| 117 | + } else { |
| 118 | + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); |
| 119 | + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); |
| 120 | + } |
| 121 | + t.end(); |
| 122 | + } |
| 123 | +}); |
| 124 | + |
| 125 | +tape( 'the command-line interface prints the list of words', opts, function test( t ) { |
| 126 | + var expected; |
| 127 | + var cmd; |
| 128 | + |
| 129 | + cmd = [ |
| 130 | + process.execPath, |
| 131 | + fpath |
| 132 | + ]; |
| 133 | + |
| 134 | + expected = readFileSync( resolve( __dirname, '..', 'data', 'words.txt' ) ); |
| 135 | + |
| 136 | + exec( cmd.join( ' ' ), done ); |
| 137 | + |
| 138 | + function done( error, stdout, stderr ) { |
| 139 | + if ( error ) { |
| 140 | + t.fail( error.message ); |
| 141 | + } else { |
| 142 | + stdout = stdout.toString(); |
| 143 | + expected = expected.toString(); |
| 144 | + t.strictEqual( stdout, expected, 'prints words' ); |
| 145 | + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); |
| 146 | + } |
| 147 | + t.end(); |
| 148 | + } |
| 149 | +}); |
0 commit comments