Skip to content

Commit 1eed018

Browse files
committed
Add test snippet for a CLI which accepts stdin
1 parent f6b5a6c commit 1eed018

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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 replace = require( '@stdlib/string/replace' );
11+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
12+
13+
14+
// VARIABLES //
15+
16+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
17+
var opts = {
18+
'skip': IS_BROWSER || IS_WINDOWS
19+
};
20+
21+
22+
// FIXTURES //
23+
24+
var PKG_VERSION = require( './../package.json' ).version;
25+
26+
27+
// TESTS //
28+
29+
tape( 'command-line interface', function test( t ) {
30+
t.ok( true, __filename );
31+
t.end();
32+
});
33+
34+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
35+
var expected;
36+
var cmd;
37+
38+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
39+
'encoding': 'utf8'
40+
});
41+
cmd = [
42+
process.execPath,
43+
fpath,
44+
'--help'
45+
];
46+
47+
exec( cmd.join( ' ' ), done );
48+
49+
function done( error, stdout, stderr ) {
50+
if ( error ) {
51+
t.fail( error.message );
52+
} else {
53+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
54+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
55+
}
56+
t.end();
57+
}
58+
});
59+
60+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
61+
var expected;
62+
var cmd;
63+
64+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
65+
'encoding': 'utf8'
66+
});
67+
cmd = [
68+
process.execPath,
69+
fpath,
70+
'-h'
71+
];
72+
73+
exec( cmd.join( ' ' ), done );
74+
75+
function done( error, stdout, stderr ) {
76+
if ( error ) {
77+
t.fail( error.message );
78+
} else {
79+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
80+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
81+
}
82+
t.end();
83+
}
84+
});
85+
86+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
87+
var cmd = [
88+
process.execPath,
89+
fpath,
90+
'--version'
91+
];
92+
93+
exec( cmd.join( ' ' ), done );
94+
95+
function done( error, stdout, stderr ) {
96+
if ( error ) {
97+
t.fail( error.message );
98+
} else {
99+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
100+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
101+
}
102+
t.end();
103+
}
104+
});
105+
106+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
107+
var cmd = [
108+
process.execPath,
109+
fpath,
110+
'-V'
111+
];
112+
113+
exec( cmd.join( ' ' ), done );
114+
115+
function done( error, stdout, stderr ) {
116+
if ( error ) {
117+
t.fail( error.message );
118+
} else {
119+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
120+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
121+
}
122+
t.end();
123+
}
124+
});
125+
126+
tape( 'the command-line interface does TODO', opts, function test( t ) {
127+
var opts;
128+
var cmd;
129+
130+
cmd = [
131+
process.execPath,
132+
'-e',
133+
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'TODO\'; require( \''+fpath+'\' );"'
134+
];
135+
opts = {};
136+
137+
exec( cmd.join( ' ' ), opts, done );
138+
139+
function done( error, stdout, stderr ) {
140+
if ( error ) {
141+
t.fail( error.message );
142+
} else {
143+
t.strictEqual( stdout.toString(), 'TODO', 'prints expected results to stdout' );
144+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
145+
}
146+
t.end();
147+
}
148+
});
149+
150+
// TODO: add additional tests
151+
152+
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 ) {
153+
var script;
154+
var opts;
155+
var cmd;
156+
157+
script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), {
158+
'encoding': 'utf8'
159+
});
160+
161+
// Replace single quotes with double quotes:
162+
script = replace( script, '\'', '"' );
163+
164+
cmd = [
165+
process.execPath,
166+
'-e',
167+
'\''+script+'\''
168+
];
169+
170+
opts = {
171+
'cwd': __dirname
172+
};
173+
174+
exec( cmd.join( ' ' ), opts, done );
175+
176+
function done( error, stdout, stderr ) {
177+
if ( error ) {
178+
t.pass( error.message );
179+
t.strictEqual( error.code, 1, 'expected exit code' );
180+
}
181+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
182+
t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' );
183+
t.end();
184+
}
185+
});

0 commit comments

Comments
 (0)