@@ -7,11 +7,13 @@ const sinon = require('sinon');
7
7
const proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
8
8
const logSymbols = require ( 'log-symbols' ) ;
9
9
const streamTestUtils = require ( '../../utils/stream' ) ;
10
- const modulePath = '../../../lib/ui'
11
- const UI = require ( modulePath ) ;
10
+ const EventEmitter = require ( 'events' ) ;
11
+
12
+ const modulePath = '../../../lib/ui' ;
12
13
13
14
describe ( 'Unit: UI' , function ( ) {
14
15
it ( 'can be created successfully' , function ( ) {
16
+ const UI = require ( modulePath ) ;
15
17
const ui = new UI ( ) ;
16
18
17
19
expect ( ui ) . to . be . ok ;
@@ -21,6 +23,7 @@ describe('Unit: UI', function () {
21
23
let ui ;
22
24
23
25
before ( function ( ) {
26
+ const UI = require ( modulePath ) ;
24
27
ui = new UI ( ) ;
25
28
} ) ;
26
29
@@ -69,6 +72,7 @@ describe('Unit: UI', function () {
69
72
} ) ;
70
73
71
74
it ( '#table creates a pretty looking table' , function ( done ) {
75
+ const UI = require ( modulePath ) ;
72
76
const ui = new UI ( ) ;
73
77
const ctx = { log : sinon . stub ( ) } ;
74
78
const expectTable = [
@@ -98,6 +102,7 @@ describe('Unit: UI', function () {
98
102
let ui ;
99
103
100
104
before ( function ( ) {
105
+ const UI = require ( modulePath ) ;
101
106
ui = new UI ( ) ;
102
107
} ) ;
103
108
@@ -143,6 +148,7 @@ describe('Unit: UI', function () {
143
148
} ) ;
144
149
145
150
it ( '#confirm calls prompt' , function ( done ) {
151
+ const UI = require ( modulePath ) ;
146
152
const ui = new UI ( ) ;
147
153
const ctx = { prompt : sinon . stub ( ) } ;
148
154
const testA = {
@@ -204,6 +210,7 @@ describe('Unit: UI', function () {
204
210
205
211
// @todo : Is this an acceptable way to test
206
212
it ( 'passes context through' , function ( ) {
213
+ const UI = require ( modulePath ) ;
207
214
const ui = new UI ( ) ;
208
215
const context = {
209
216
write : 'tests' ,
@@ -222,6 +229,7 @@ describe('Unit: UI', function () {
222
229
} ) ;
223
230
224
231
it ( 'ignores context if requested' , function ( ) {
232
+ const UI = require ( modulePath ) ;
225
233
const ui = new UI ( ) ;
226
234
const tasks = [ {
227
235
title : 'test' ,
@@ -235,29 +243,37 @@ describe('Unit: UI', function () {
235
243
} ) ;
236
244
237
245
it ( '#sudo runs a sudo command' , function ( done ) {
238
- const execa = require ( 'execa' ) ;
239
- const ctx = {
240
- log : sinon . stub ( ) ,
241
- noSpin : sinon . stub ( ) . callsFake ( ( run ) => run ( ) )
242
- }
243
- const UI = proxyquire ( modulePath , { execa : execa } ) ;
246
+ const shellStub = sinon . stub ( ) ;
247
+ const UI = proxyquire ( modulePath , { execa : { shell : shellStub } } ) ;
244
248
const ui = new UI ( ) ;
245
- const eCall = new RegExp ( `sudo ${ process . argv . slice ( 0 , 2 ) . join ( ' ' ) } restart` ) ;
246
- sinon . stub ( execa , 'shell' ) ;
247
249
248
- ui . sudo . bind ( ctx ) ( 'ghost restart' ) ;
250
+ const logStub = sinon . stub ( ui , 'log' ) ;
251
+ const promptStub = sinon . stub ( ui , 'prompt' ) . resolves ( { password : 'password' } ) ;
252
+ const stderr = new EventEmitter ( ) ;
249
253
250
- expect ( ctx . log . calledOnce ) . to . be . true ;
251
- expect ( execa . shell . calledOnce ) . to . be . true ;
252
- expect ( execa . shell . firstCall . args [ 0 ] ) . to . match ( eCall ) ;
253
- expect ( execa . shell . firstCall . args [ 1 ] . stdio ) . to . equal ( 'inherit' ) ;
254
- done ( ) ;
254
+ const eCall = new RegExp ( `sudo -S -p '#node-sudo-passwd#' -E -u ghost ${ process . argv . slice ( 0 , 2 ) . join ( ' ' ) } -v` ) ;
255
+
256
+ const stdin = streamTestUtils . getWritableStream ( ( output ) => {
257
+ expect ( output ) . to . equal ( 'password\n' ) ;
258
+ expect ( logStub . calledOnce ) . to . be . true ;
259
+ expect ( logStub . calledWithExactly ( 'Running sudo command: ghost -v' , 'gray' ) ) . to . be . true ;
260
+ expect ( shellStub . calledOnce ) . to . be . true ;
261
+ expect ( shellStub . args [ 0 ] [ 0 ] ) . to . match ( eCall ) ;
262
+ expect ( shellStub . args [ 0 ] [ 1 ] ) . to . deep . equal ( { cwd : '/var/foo' } ) ;
263
+ expect ( promptStub . calledOnce ) . to . be . true ;
264
+ done ( ) ;
265
+ } ) ;
266
+ shellStub . returns ( { stdin : stdin , stderr : stderr } ) ;
267
+
268
+ ui . sudo ( 'ghost -v' , { cwd : '/var/foo' , sudoArgs : [ '-E -u ghost' ] } ) ;
269
+ stderr . emit ( 'data' , '#node-sudo-passwd#' ) ;
255
270
} ) ;
256
271
257
272
describe ( '#noSpin' , function ( ) {
258
273
let ui ;
259
274
260
275
before ( function ( ) {
276
+ const UI = require ( modulePath ) ;
261
277
ui = new UI ( ) ;
262
278
} ) ;
263
279
@@ -300,6 +316,7 @@ describe('Unit: UI', function () {
300
316
} ) ;
301
317
stdout . on ( 'error' , done ) ;
302
318
319
+ const UI = require ( modulePath ) ;
303
320
const ui = new UI ( { stdout : stdout } ) ;
304
321
ui . log ( 'test' ) ;
305
322
} ) ;
@@ -314,6 +331,7 @@ describe('Unit: UI', function () {
314
331
} ) ;
315
332
stdout . on ( 'error' , done ) ;
316
333
334
+ const UI = require ( modulePath ) ;
317
335
const ui = new UI ( { stdout : stdout } ) ;
318
336
ui . log ( 'test' , 'green' ) ;
319
337
} ) ;
@@ -323,6 +341,7 @@ describe('Unit: UI', function () {
323
341
stdout : { write : sinon . stub ( ) } ,
324
342
stderr : { write : sinon . stub ( ) }
325
343
} ;
344
+ const UI = require ( modulePath ) ;
326
345
const ui = new UI ( ) ;
327
346
328
347
ui . log . bind ( ctx ) ( 'Error' , null , true ) ;
@@ -337,6 +356,7 @@ describe('Unit: UI', function () {
337
356
} ) ;
338
357
339
358
it ( 'resets spinner' , function ( done ) {
359
+ const UI = require ( modulePath ) ;
340
360
const ui = new UI ( ) ;
341
361
const write = sinon . stub ( )
342
362
const ctx = {
@@ -363,6 +383,7 @@ describe('Unit: UI', function () {
363
383
364
384
describe ( '#logVerbose' , function ( ) {
365
385
it ( 'passes through options to log method when verbose is set' , function ( ) {
386
+ const UI = require ( modulePath ) ;
366
387
const ui = new UI ( { verbose : true } ) ;
367
388
const logStub = sinon . stub ( ui , 'log' ) ;
368
389
@@ -372,6 +393,7 @@ describe('Unit: UI', function () {
372
393
} ) ;
373
394
374
395
it ( 'does not call log when verbose is false' , function ( ) {
396
+ const UI = require ( modulePath ) ;
375
397
const ui = new UI ( { verbose : false } ) ;
376
398
const logStub = sinon . stub ( ui , 'log' ) ;
377
399
@@ -389,6 +411,7 @@ describe('Unit: UI', function () {
389
411
} ) ;
390
412
stdout . on ( 'error' , done ) ;
391
413
414
+ const UI = require ( modulePath ) ;
392
415
const ui = new UI ( { stdout : stdout } ) ;
393
416
ui . success ( 'test' ) ;
394
417
} ) ;
@@ -402,6 +425,7 @@ describe('Unit: UI', function () {
402
425
} ) ;
403
426
stdout . on ( 'error' , done ) ;
404
427
428
+ const UI = require ( modulePath ) ;
405
429
const ui = new UI ( { stdout : stdout } ) ;
406
430
ui . fail ( 'test' ) ;
407
431
} ) ;
@@ -411,6 +435,7 @@ describe('Unit: UI', function () {
411
435
let ui ;
412
436
413
437
before ( function ( ) {
438
+ const UI = require ( modulePath ) ;
414
439
ui = new UI ( ) ;
415
440
} ) ;
416
441
@@ -582,6 +607,7 @@ describe('Unit: UI', function () {
582
607
environment : 'Earth'
583
608
} ;
584
609
const SPACES = ' ' ;
610
+ const UI = require ( modulePath ) ;
585
611
const ui = new UI ( ) ;
586
612
const expected = [ 'Debug Information:' ,
587
613
`${ SPACES } Node Version: ${ process . version } ` ,
0 commit comments