UNPKG

5.28 kBJavaScriptView Raw
1
2/**
3 * This cli.js test file tests the `gnode` wrapper executable via
4 * `child_process.spawn()`. Generator syntax is *NOT* enabled for these
5 * test cases.
6 */
7
8var path = require('path');
9var assert = require('assert');
10var semver = require('semver');
11var spawn = require('child_process').spawn;
12
13// node executable
14var node = process.execPath || process.argv[0];
15var gnode = path.resolve(__dirname, '..', 'bin', 'gnode');
16
17// chdir() to the "test" dir, so that relative test filenames work as expected
18process.chdir(path.resolve(__dirname, 'cli'));
19
20describe('command line interface', function () {
21
22 this.slow(1000);
23 this.timeout(2000);
24
25 cli([ '-v' ], 'should output the version number', function (child, done) {
26 buffer(child.stdout, function (err, data) {
27 assert(semver.valid(data.trim()));
28 done();
29 });
30 });
31
32 cli([ '--help' ], 'should output the "help" display', function (child, done) {
33 buffer(child.stdout, function (err, data) {
34 assert(/^Usage\: node/.test(data));
35 done();
36 });
37 });
38
39 cli([ 'check.js' ], 'should quit with a SUCCESS exit code', function (child, done) {
40 child.on('exit', function (code) {
41 assert(code == 0, 'gnode quit with exit code: ' + code);
42 done();
43 });
44 });
45
46 cli([ 'nonexistant.js' ], 'should quit with a FAILURE exit code', function (child, done) {
47 child.on('exit', function (code) {
48 assert(code != 0, 'gnode quit with exit code: ' + code);
49 done();
50 });
51 });
52
53 cli([ 'argv.js', '1', 'foo' ], 'should have a matching `process.argv`', function (child, done) {
54 buffer(child.stdout, function (err, data) {
55 if (err) return done(err);
56 data = JSON.parse(data);
57 assert('argv.js' == path.basename(data[1]));
58 assert('1' == data[2]);
59 assert('foo' == data[3]);
60 done();
61 });
62 });
63
64 cli([ '--harmony_generators', 'check.js' ], 'should not output the "unrecognized flag" warning', function (child, done) {
65 var async = 2;
66 buffer(child.stderr, function (err, data) {
67 if (err) return done(err);
68 assert(!/unrecognized flag/.test(data), 'got stderr data: ' + JSON.stringify(data));
69 --async || done();
70 });
71 child.on('exit', function (code) {
72 assert(code == 0, 'gnode quit with exit code: ' + code);
73 --async || done();
74 });
75 });
76
77 cli([], 'should work properly over stdin', function (child, done) {
78 child.stdin.end(
79 'var assert = require("assert");' +
80 'function *test () {};' +
81 'var t = test();' +
82 'assert("function" == typeof t.next);' +
83 'assert("function" == typeof t.throw);'
84 );
85 child.on('exit', function (code) {
86 assert(code == 0, 'gnode quit with exit code: ' + code);
87 done();
88 });
89 });
90
91 if (!/^v0.8/.test(process.version)) cli(['-p', 'function *test () {yield 3}; test().next().value;'], 'should print result with -p', function (child, done) {
92 var async = 2
93 buffer(child.stdout, function (err, data) {
94 if (err) return done(err);
95 assert('3' == data.trim(), 'gnode printed ' + data);
96 --async || done();
97 });
98 child.on('exit', function (code) {
99 assert(code == 0, 'gnode quit with exit code: ' + code);
100 --async || done();
101 });
102 });
103
104 cli(['-e', 'function *test () {yield 3}; console.log(test().next().value);'], 'should print result with -p', function (child, done) {
105 var async = 2
106 buffer(child.stdout, function (err, data) {
107 if (err) return done(err);
108 assert('3' == data.trim(), 'expected 3, got: ' + data);
109 --async || done();
110 });
111
112 child.on('exit', function (code) {
113 assert(code == 0, 'gnode quit with exit code: ' + code);
114 --async || done();
115 });
116 });
117
118 cli(['--harmony_generators', '-e', 'function *test () {yield 3}; console.log(test().next().value);'], 'should print result with -e', function (child, done) {
119 var async = 2
120 buffer(child.stdout, function (err, data) {
121 if (err) return done(err);
122 assert('3' == data.trim(), 'gnode printed ' + data);
123 --async || done();
124 });
125 child.on('exit', function (code) {
126 assert(code == 0, 'gnode quit with exit code: ' + code);
127 --async || done();
128 });
129 });
130
131 cli(['-e', 'console.log(JSON.stringify(process.argv))', 'a', 'b', 'c'], 'should pass additional arguments after -e', function (child, done) {
132 var async = 2
133 buffer(child.stdout, function (err, data) {
134 if (err) return done(err);
135 data = JSON.parse(data)
136 assert.deepEqual(['a', 'b', 'c'], data.slice(2))
137 --async || done();
138 });
139 child.on('exit', function (code) {
140 assert(code == 0, 'gnode quit with exit code: ' + code);
141 --async || done();
142 });
143 });
144});
145
146
147function cli (argv, name, fn) {
148 describe('gnode ' + argv.join(' '), function () {
149 it(name, function (done) {
150 var child = spawn(node, [ gnode ].concat(argv));
151 fn(child, done);
152 });
153 });
154}
155
156function buffer (stream, fn) {
157 var buffers = '';
158 stream.setEncoding('utf8');
159 stream.on('data', ondata);
160 stream.on('end', onend);
161 stream.on('error', onerror);
162
163 function ondata (b) {
164 buffers += b;
165 }
166 function onend () {
167 stream.removeListener('error', onerror);
168 fn(null, buffers);
169 }
170 function onerror (err) {
171 fn(err);
172 }
173}