Skip to content

Commit 7df37de

Browse files
committed
Add CLI tests
1 parent 43befe1 commit 7df37de

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

test/test.cli.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var exec = require( 'child_process' ).exec;
25+
var tape = require( 'tape' );
26+
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
27+
var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
28+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
29+
var CLI_COMMANDS = require( './../bin/cli_commands.json' );
30+
31+
32+
// VARIABLES //
33+
34+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
35+
var opts = {
36+
'skip': IS_BROWSER || IS_WINDOWS
37+
};
38+
39+
40+
// FIXTURES //
41+
42+
var PKG_VERSION = require( './../package.json' ).version;
43+
44+
45+
// TESTS //
46+
47+
tape( 'command-line interface', function test( t ) {
48+
t.ok( true, __filename );
49+
t.end();
50+
});
51+
52+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
53+
var expected;
54+
var cmd;
55+
56+
expected = readFileSync( resolve( __dirname, '..', 'bin', 'usage.txt' ), {
57+
'encoding': 'utf8'
58+
});
59+
cmd = [
60+
process.execPath,
61+
fpath,
62+
'--help'
63+
];
64+
65+
exec( cmd.join( ' ' ), done );
66+
67+
function done( error, stdout, stderr ) {
68+
if ( error ) {
69+
t.fail( error.message );
70+
} else {
71+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
72+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
73+
}
74+
t.end();
75+
}
76+
});
77+
78+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
79+
var expected;
80+
var cmd;
81+
82+
expected = readFileSync( resolve( __dirname, '..', 'bin', 'usage.txt' ), {
83+
'encoding': 'utf8'
84+
});
85+
cmd = [
86+
process.execPath,
87+
fpath,
88+
'-h'
89+
];
90+
91+
exec( cmd.join( ' ' ), done );
92+
93+
function done( error, stdout, stderr ) {
94+
if ( error ) {
95+
t.fail( error.message );
96+
} else {
97+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
98+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
99+
}
100+
t.end();
101+
}
102+
});
103+
104+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
105+
var cmd = [
106+
process.execPath,
107+
fpath,
108+
'--version'
109+
];
110+
111+
exec( cmd.join( ' ' ), done );
112+
113+
function done( error, stdout, stderr ) {
114+
if ( error ) {
115+
t.fail( error.message );
116+
} else {
117+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
118+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
119+
}
120+
t.end();
121+
}
122+
});
123+
124+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
125+
var cmd = [
126+
process.execPath,
127+
fpath,
128+
'-V'
129+
];
130+
131+
exec( cmd.join( ' ' ), done );
132+
133+
function done( error, stdout, stderr ) {
134+
if ( error ) {
135+
t.fail( error.message );
136+
} else {
137+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
138+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
139+
}
140+
t.end();
141+
}
142+
});
143+
144+
tape( 'the command-line interface prints the help text for individual commands to `stderr`', function test( t ) {
145+
var command;
146+
var idx;
147+
148+
idx = 0;
149+
next();
150+
151+
function next() {
152+
var cmd;
153+
154+
command = CLI_COMMANDS[ idx ].command;
155+
156+
// Handle special case...
157+
if ( command === 'help [command]' ) {
158+
command = 'help';
159+
} else {
160+
command = 'help '+command;
161+
}
162+
cmd = [
163+
process.execPath,
164+
fpath,
165+
command
166+
];
167+
168+
exec( cmd.join( ' ' ), done );
169+
}
170+
171+
function done( error, stdout, stderr ) {
172+
if ( error ) {
173+
t.fail( error.message );
174+
} else {
175+
t.strictEqual( stdout.toString(), '', '`'+command+'` does not print to `stdout`' );
176+
t.strictEqual( stderr.toString().length > 0, true, '`'+command+'` prints to `stderr`' );
177+
}
178+
idx += 1;
179+
if ( idx < CLI_COMMANDS.length ) {
180+
return next();
181+
}
182+
t.end();
183+
}
184+
});

0 commit comments

Comments
 (0)