Skip to content

Commit 308ac11

Browse files
committed
Update CLI
1 parent 43f5b3d commit 308ac11

4 files changed

Lines changed: 176 additions & 2 deletions

File tree

tools/links/id2uri/bin/cli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ function main() {
6565
// Create a command-line interface:
6666
cli = new CLI({
6767
'pkg': require( './../package.json' ),
68-
'options': require( './opts.json' ),
69-
'help': readFileSync( join( __dirname, 'usage.txt' ), {
68+
'options': require( './../etc/cli_opts.json' ),
69+
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
7070
'encoding': 'utf8'
7171
})
7272
});
73+
7374
// Get any provided command-line arguments:
7475
args = cli.args();
7576

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
var contains = require( '@stdlib/assert/contains' );
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 prints the URI corresponding to a provided id', opts, function test( t ) {
127+
var cmd;
128+
129+
cmd = [
130+
process.execPath,
131+
'-e',
132+
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'sublime-text\'; require( \''+fpath+'\' );"'
133+
];
134+
135+
exec( cmd.join( ' ' ), done );
136+
137+
function done( error, stdout, stderr ) {
138+
if ( error ) {
139+
t.fail( error.message );
140+
} else {
141+
t.strictEqual( stdout.toString(), 'https://www.sublimetext.com/\n', 'expected value' );
142+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
143+
}
144+
t.end();
145+
}
146+
});
147+
148+
tape( 'the command-line interface prints the URI corresponding to a provided id (interactive prompt)', opts, function test( t ) {
149+
var child;
150+
var cmd;
151+
152+
cmd = [
153+
process.execPath,
154+
'-e',
155+
'"process.stdin.isTTY = true; require( \''+fpath+'\' );"'
156+
];
157+
158+
child = exec( cmd.join( ' ' ), done );
159+
child.stdin.write( 'sublime-text\n' );
160+
child.stdin.write( '\n' );
161+
child.stdin.end();
162+
163+
function done( error, stdout, stderr ) {
164+
if ( error ) {
165+
t.fail( error.message );
166+
} else {
167+
stdout = stdout.toString();
168+
t.strictEqual( contains( stdout, 'https://www.sublimetext.com/' ), true, 'expected value' );
169+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
170+
}
171+
t.end();
172+
}
173+
});

0 commit comments

Comments
 (0)