/
cli-test.js
605 lines (492 loc) · 19.8 KB
/
cli-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
'use strict';
var expect = require('chai').expect;
var MockUI = require('console-ui/mock');
var MockAnalytics = require('../../helpers/mock-analytics');
var CLI = require('../../../lib/cli/cli');
var td = require('testdouble');
var ui;
var analytics;
var commands = {};
var argv;
var isWithinProject;
// helper to similate running the CLI
function ember(args) {
return new CLI({
ui: ui,
analytics: analytics,
testing: true
}).run({
tasks: {},
commands: commands,
cliArgs: args || [],
settings: {},
project: {
isEmberCLIProject: function() { // similate being inside or outside of a project
return isWithinProject;
},
hasDependencies: function() {
return true;
},
blueprintLookupPaths: function() {
return [];
}
}
});
}
function stubCallHelp() {
return td.replace(CLI.prototype, 'callHelp', td.function());
}
function stubValidateAndRunHelp(name) {
var stub = stubValidateAndRun(name);
td.when(stub(), {ignoreExtraArgs: true}).thenReturn('callHelp');
return stub;
}
function stubValidateAndRun(name) {
commands[name] = require('../../../lib/commands/' + name);
return td.replace(commands[name].prototype, 'validateAndRun', td.function());
}
function stubRun(name) {
commands[name] = require('../../../lib/commands/' + name);
return td.replace(commands[name].prototype, 'run', td.function());
}
beforeEach(function() {
ui = new MockUI();
analytics = new MockAnalytics();
argv = [];
commands = { };
isWithinProject = true;
});
afterEach(function() {
td.reset();
delete process.env.EMBER_ENV;
commands = argv = ui = undefined;
});
describe('Unit: CLI', function() {
this.timeout(10000);
it('exists', function() {
expect(CLI).to.be.ok;
});
it('ember', function() {
var help = stubValidateAndRun('help');
return ember().then(function() {
td.verify(help(), {ignoreExtraArgs: true, times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
/*
it('logError', function() {
var cli = new CLI({
ui: ui,
analytics: analytics,
testing: true
});
var error = new Error('Error message!');
var expected = {exitCode: 1, ui: ui, error: error};
expect(cli.logError(error)).to.eql(expected, 'expected error object');
});
*/
it('callHelp', function() {
var cli = new CLI({
ui: ui,
analytics: analytics,
testing: true
});
var init = stubValidateAndRun('init');
var help = stubValidateAndRun('help');
var helpOptions = {
environment: {
tasks: {},
commands: commands,
cliArgs: [],
settings: {},
project: {
isEmberCLIProject: function() { // similate being inside or outside of a project
return isWithinProject;
},
hasDependencies: function() {
return true;
},
blueprintLookupPaths: function() {
return [];
}
}
},
commandName: 'init',
commandArgs: []
};
cli.callHelp(helpOptions);
td.verify(help(), {ignoreExtraArgs: true, times: 1});
td.verify(init(), {ignoreExtraArgs: true, times: 0});
});
describe('help', function() {
['--help', '-h'].forEach(function(command) {
it('ember ' + command, function() {
var help = stubValidateAndRun('help');
return ember([command]).then(function() {
td.verify(help(), {ignoreExtraArgs: true, times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
it('ember new ' + command, function() {
var help = stubCallHelp();
var newCommand = stubValidateAndRunHelp('new');
return ember(['new', command]).then(function() {
td.verify(help(), {ignoreExtraArgs: true, times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
td.verify(newCommand(), {ignoreExtraArgs: true, times: 1});
});
});
});
});
['--version', '-v'].forEach(function(command) {
it('ember ' + command, function() {
var version = stubValidateAndRun('version');
return ember([command]).then(function() {
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
td.verify(version(), {ignoreExtraArgs: true, times: 1});
});
});
});
describe('server', function() {
['server','s'].forEach(function(command) {
it('ember ' + command + ' --port 9999', function() {
var server = stubRun('serve');
return ember([command, '--port', '9999']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.port, 'port').to.equal(9999);
});
});
it('ember ' + command + ' --host localhost', function() {
var server = stubRun('serve');
return ember(['server', '--host', 'localhost']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.host, 'host').to.equal('localhost');
});
});
it('ember ' + command + ' --port 9292 --host localhost', function() {
var server = stubRun('serve');
return ember([command, '--port', '9292', '--host', 'localhost']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.host, 'host').to.equal('localhost');
expect(captor.value.port, 'port').to.equal(9292);
});
});
it('ember ' + command + ' --proxy http://localhost:3000/', function() {
var server = stubRun('serve');
return ember([command, '--proxy', 'http://localhost:3000/']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.proxy, 'proxy').to.equal('http://localhost:3000/');
});
});
it('ember ' + command + ' --proxy https://localhost:3009/ --insecure-proxy', function () {
var server = stubRun('serve');
return ember([command, '--insecure-proxy']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.insecureProxy, 'insecureProxy').to.equal(true);
});
});
it('ember ' + command + ' --proxy https://localhost:3009/ --no-insecure-proxy', function () {
var server = stubRun('serve');
return ember([command, '--no-insecure-proxy']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.insecureProxy, 'insecureProxy').to.equal(false);
});
});
it('ember ' + command + ' --watcher events', function() {
var server = stubRun('serve');
return ember([command, '--watcher', 'events']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.watcher, 'watcher').to.match(/node|polling|watchman/);
});
});
it('ember ' + command + ' --watcher polling', function() {
var server = stubRun('serve');
return ember([command, '--watcher', 'polling']).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.watcher, 'watcher').to.equal('polling');
});
});
it('ember ' + command, function() {
var server = stubRun('serve');
return ember([command]).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.watcher, 'watcher').to.match(/node|polling|watchman/);
});
});
['production', 'development', 'foo'].forEach(function(env) {
it('ember ' + command + ' --environment ' + env, function() {
var server = stubRun('serve');
return ember([command, '--environment', env]).then(function() {
var captor = td.matchers.captor();
td.verify(server(captor.capture()), {ignoreExtraArgs: true, times: 1});
expect(captor.value.environment, 'environment').to.equal(env);
});
});
});
['development', 'foo'].forEach(function(env) {
it('ember ' + command + ' --environment ' + env, function() {
var server = stubRun('serve');
process.env.EMBER_ENV = 'production';
return ember([command, '--environment', env]).then(function() {
td.verify(server(), {ignoreExtraArgs: true, times: 1});
expect(process.env.EMBER_ENV).to.equal('production', 'uses EMBER_ENV over environment');
});
});
});
['production', 'development', 'foo'].forEach(function(env) {
it('EMBER_ENV=' + env + ' ember ' + command, function() {
var server = stubRun('serve');
process.env.EMBER_ENV = env;
return ember([command]).then(function() {
td.verify(server(), {ignoreExtraArgs: true, times: 1});
expect(process.env.EMBER_ENV).to.equal(env, 'correct environment');
});
});
});
});
});
describe('generate', function() {
['generate', 'g'].forEach(function(command) {
it('ember ' + command + ' foo bar baz', function() {
var generate = stubRun('generate');
return ember([command, 'foo', 'bar', 'baz']).then(function() {
var captor = td.matchers.captor();
td.verify(generate(captor.capture(), ['foo', 'bar', 'baz']), {times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
});
});
describe('init', function() {
['init'].forEach(function(command) {
it('ember ' + command, function() {
var init = stubValidateAndRun('init');
return ember([command]).then(function() {
td.verify(init(), {ignoreExtraArgs: true, times: 1});
});
});
it('ember ' + command + ' <app-name>', function() {
var init = stubRun('init');
return ember([command, 'my-blog']).then(function() {
var captor = td.matchers.captor();
td.verify(init(captor.capture(), ['my-blog']), {times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
});
});
describe('new', function() {
it('ember new', function() {
isWithinProject = false;
var newCommand = stubRun('new');
return ember(['new']).then(function() {
td.verify(newCommand(), {ignoreExtraArgs: true, times: 1});
});
});
it('ember new MyApp', function() {
isWithinProject = false;
var newCommand = stubRun('new');
return ember(['new', 'MyApp']).then(function() {
td.verify(newCommand(td.matchers.anything(), ['MyApp']), {times: 1});
});
});
});
describe('build', function() {
['build','b'].forEach(function(command) {
it('ember ' + command, function() {
var build = stubRun('build');
return ember([command]).then(function() {
var captor = td.matchers.captor();
td.verify(build(captor.capture()), {ignoreExtraArgs: true, times: 1});
var options = captor.value;
expect(options.watch).to.equal(false, 'expected the default watch flag to be false');
expect(options.suppressSizes).to.equal(false, 'expected the default supress-sizes flag to be false');
});
});
it('ember ' + command + ' --disable-analytics', function() {
var build = stubRun('build');
return ember([command, '--disable-analytics']).then(function() {
var captor = td.matchers.captor();
td.verify(build(captor.capture()), {ignoreExtraArgs: true, times: 1});
var options = captor.value;
expect(options.disableAnalytics).to.equal(true, 'expected the disableAnalytics flag to be true');
});
});
it('ember ' + command + ' --watch', function() {
var build = stubRun('build');
return ember([command, '--watch']).then(function() {
var captor = td.matchers.captor();
td.verify(build(captor.capture()), {ignoreExtraArgs: true, times: 1});
var options = captor.value;
expect(options.watch).to.equal(true, 'expected the watch flag to be true');
});
});
it('ember ' + command + ' --suppress-sizes', function() {
var build = stubRun('build');
return ember([command, '--suppress-sizes']).then(function () {
var captor = td.matchers.captor();
td.verify(build(captor.capture()), {ignoreExtraArgs: true, times: 1});
var options = captor.value;
expect(options.suppressSizes).to.equal(true, 'expected the suppressSizes flag to be true');
});
});
['production', 'development', 'baz'].forEach(function(env) {
it('ember ' + command + ' --environment ' + env, function() {
var build = stubRun('build');
return ember([command, '--environment', env]).then(function() {
var captor = td.matchers.captor();
td.verify(build(captor.capture()), {ignoreExtraArgs: true, times: 1});
var options = captor.value;
expect(options.environment).to.equal(env, 'correct environment');
});
});
});
['development', 'baz'].forEach(function(env) {
it('EMBER_ENV=production ember ' + command + ' --environment ' + env, function() {
var build = stubRun('build');
process.env.EMBER_ENV = 'production';
return ember([command, '--environment', env]).then(function() {
td.verify(build(), {ignoreExtraArgs: true, times: 1});
expect(process.env.EMBER_ENV).to.equal('production', 'uses EMBER_ENV over environment');
});
});
});
['production', 'development', 'baz'].forEach(function(env) {
it('EMBER_ENV=' + env + ' ember ' + command + ' ', function() {
var build = stubRun('build');
process.env.EMBER_ENV = env;
return ember([command]).then(function() {
td.verify(build(), {ignoreExtraArgs: true, times: 1});
expect(process.env.EMBER_ENV).to.equal(env, 'correct environment');
});
});
});
});
});
it('ember <valid command>', function() {
var help = stubValidateAndRun('help');
var serve = stubValidateAndRun('serve');
return ember(['serve']).then(function() {
td.verify(help(), {ignoreExtraArgs: true, times: 0});
td.verify(serve(), {ignoreExtraArgs: true, times: 1});
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
it.skip('ember <valid command with args>', function() {
var help = stubValidateAndRun('help');
var serve = stubValidateAndRun('serve');
return ember(['serve', 'lorem', 'ipsum', 'dolor', '--flag1=one']).then(function() {
var args = serve.calledWith[0][0].cliArgs;
expect(help.called).to.equal(0, 'expected the help command NOT to be run');
expect(serve.called).to.equal(1, 'expected the foo command to be run');
expect(args).to.deep.equal(['serve', 'lorem', 'ipsum', 'dolor'], 'expects correct arguments');
expect(serve.calledWith[0].length).to.equal(2, 'expect foo to receive a total of 4 args');
var output = ui.output.trim();
expect(output).to.equal('', 'expected no extra output');
});
});
it('ember <invalid command>', function() {
var help = stubValidateAndRun('help');
return ember(['unknownCommand']).then(function() {
expect(false).to.be.ok;
}).catch(function(error) {
expect(help.called, 'help command was executed').to.not.be.ok;
expect(error.name).to.equal('SilentError');
expect(error.message).to.equal('The specified command unknownCommand is invalid. For available options, see `ember help`.');
});
});
describe.skip('default options config file', function() {
it('reads default options from .ember-cli file', function() {
var defaults = ['--output', process.cwd()];
var build = stubValidateAndRun('build');
return ember(['build'], defaults).then(function() {
var options = build.calledWith[0][1].cliOptions;
expect(options.output).to.equal(process.cwd());
});
});
});
describe('logError', function() {
it('returns error status code in production', function() {
var cli = new CLI({
ui: new MockUI(),
testing: false
});
expect(cli.logError('foo')).to.equal(1);
});
it('does not throw an error in production', function() {
var cli = new CLI({
ui: new MockUI(),
testing: false
});
var invokeError = cli.logError.bind(cli, new Error('foo'));
expect(invokeError).to.not.throw();
});
it('throws error in testing', function() {
var cli = new CLI({
ui: new MockUI(),
testing: true
});
var invokeError = cli.logError.bind(cli, new Error('foo'));
expect(invokeError).to.throw(Error, 'foo');
});
});
describe('Global command options', function() {
var verboseCommand = function(args) {
return ember(['fake-command', '--verbose'].concat(args));
};
describe('--verbose', function() {
describe('option parsing', function() {
afterEach(function() {
delete process.env.EMBER_VERBOSE_FAKE_OPTION_1;
delete process.env.EMBER_VERBOSE_FAKE_OPTION_2;
});
it('sets process.env.EMBER_VERBOSE_${NAME} for each space delimited option', function() {
return verboseCommand(['fake_option_1', 'fake_option_2']).then(function() {
expect(false).to.be.true;
}).catch(function(error) {
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_1).to.be.ok;
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_2).to.be.ok;
expect(error.name).to.equal('SilentError');
expect(error.message).to.equal('The specified command fake-command is invalid. For available options, see `ember help`.');
});
});
it('ignores verbose options after --', function() {
return verboseCommand(['fake_option_1', '--fake-option', 'fake_option_2']).then(function() {
expect(false).to.be.true;
}).catch(function(error) {
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_1).to.be.ok;
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_2).to.not.be.ok;
expect(error.name).to.equal('SilentError');
expect(error.message).to.equal('The specified command fake-command is invalid. For available options, see `ember help`.');
});
});
it('ignores verbose options after -', function() {
return verboseCommand(['fake_option_1', '-f', 'fake_option_2']).then(function() {
expect(false).to.be.true;
}).catch(function(error) {
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_1).to.be.ok;
expect(process.env.EMBER_VERBOSE_FAKE_OPTION_2).to.not.be.ok;
expect(error.name).to.equal('SilentError');
expect(error.message).to.equal('The specified command fake-command is invalid. For available options, see `ember help`.');
});
});
});
});
});
});