@@ -234,6 +234,47 @@ describe('Unit: Command', function () {
234234 }
235235 } ) ;
236236
237+ it ( 'Changes directory if needed' , function ( ) {
238+ sinon . stub ( process , 'exit' ) . throws ( new Error ( 'exit_stub' ) ) ;
239+ const outStub = sinon . stub ( process . stderr , 'write' ) ;
240+ const chdirStub = sinon . stub ( process , 'chdir' ) . throws ( new Error ( 'chdir_stub' ) ) ;
241+ const Command = require ( modulePath ) ;
242+ class TestCommand extends Command { }
243+
244+ try {
245+ TestCommand . _run ( 'test' , { dir : '/path/to/ghost' , verbose : true } ) ;
246+ throw new Error ( 'Should have errored' ) ;
247+ } catch ( error ) {
248+ expect ( error ) . to . be . ok ;
249+ expect ( error . message ) . to . equal ( 'exit_stub' ) ;
250+ expect ( chdirStub . calledOnce ) . to . be . true ;
251+ expect ( chdirStub . args [ 0 ] [ 0 ] ) . to . equal ( '/path/to/ghost' ) ;
252+ expect ( outStub . calledOnce ) . to . be . true ;
253+ expect ( outStub . args [ 0 ] [ 0 ] ) . to . match ( / c h d i r _ s t u b / ) ;
254+ }
255+ } ) ;
256+
257+ it ( 'Creates & changes into directory if needed' , function ( ) {
258+ sinon . stub ( process . stderr , 'write' ) ;
259+ sinon . stub ( process , 'exit' ) . throws ( new Error ( 'exit_stub' ) ) ;
260+ sinon . stub ( process , 'chdir' ) . throws ( new Error ( 'chdir_stub' ) ) ;
261+ const fs = require ( 'fs-extra' ) ;
262+ const fsStub = sinon . stub ( fs , 'ensureDirSync' ) ;
263+ const Command = require ( modulePath ) ;
264+ class TestCommand extends Command { }
265+ TestCommand . ensureDir = true ;
266+
267+ try {
268+ TestCommand . _run ( 'test' , { dir : '/path/to/ghost' , verbose : true } ) ;
269+ throw new Error ( 'Should have errored' ) ;
270+ } catch ( error ) {
271+ expect ( error ) . to . be . ok ;
272+ expect ( error . message ) . to . equal ( 'exit_stub' ) ;
273+ expect ( fsStub . calledOnce ) . to . be . true ;
274+ expect ( fsStub . args [ 0 ] [ 0 ] ) . to . equal ( '/path/to/ghost' ) ;
275+ }
276+ } ) ;
277+
237278 it ( 'loads system and ui dependencies, calls run method' , function ( ) {
238279 const uiStub = sinon . stub ( ) . returns ( { ui : true } ) ;
239280 const setEnvironmentStub = sinon . stub ( ) ;
0 commit comments