import * as yeoman from 'yeoman-environment' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import { YeomanUtil } from '../../extension/util/YeomanUtil' ; chai . should ( ) ; chai . use ( sinonChai ) ; describe ( 'YeomanUtil' , ( ) => { let mySandBox ; let mockEnv ; beforeEach ( ( ) => { mySandBox = sinon . createSandbox ( ) ; mockEnv = { registerStub : mySandBox . stub ( ) , run : mySandBox . stub ( ) . yields ( ) } ; mySandBox . stub ( yeoman , 'createEnv' ) . returns ( mockEnv ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; describe ( '#run' , ( ) => { it ( 'should run the specified Yeoman generator, and change working directory back on success' , async ( ) => { const cwd = process . cwd ( ) ; const chdirSpy = mySandBox . spy ( process , 'chdir' ) ; await YeomanUtil . run ( 'fabric:network' , { some : 'option' , another : 'test' } ) ; mockEnv . registerStub . should . have . callCount ( 4 ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric' , require . resolve ( 'generator-fabric' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:chaincode' , require . resolve ( 'generator-fabric/generators/chaincode' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:contract' , require . resolve ( 'generator-fabric/generators/contract' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:network' , require . resolve ( 'generator-fabric/generators/network' ) ) ; mockEnv . run . should . have . been . calledOnceWithExactly ( 'fabric:network' , { some : 'option' , another : 'test' } , sinon . match . func ) ; chdirSpy . should . have . been . calledOnceWithExactly ( cwd ) ; } ) ; it ( 'should run the specified Yeoman generator, and change working directory back on failure' , async ( ) => { const cwd = process . cwd ( ) ; const chdirSpy = mySandBox . spy ( process , 'chdir' ) ; mockEnv . run . yields ( new Error ( 'such error' ) ) ; await YeomanUtil . run ( 'fabric:network' , { some : 'option' , another : 'test' } ) . should . be . rejectedWith ( /such error/ ) ; mockEnv . registerStub . should . have . callCount ( 4 ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric' , require . resolve ( 'generator-fabric' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:chaincode' , require . resolve ( 'generator-fabric/generators/chaincode' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:contract' , require . resolve ( 'generator-fabric/generators/contract' ) ) ; mockEnv . registerStub . should . have . been . calledWithExactly ( sinon . match . func , 'fabric:network' , require . resolve ( 'generator-fabric/generators/network' ) ) ; mockEnv . run . should . have . been . calledOnceWithExactly ( 'fabric:network' , { some : 'option' , another : 'test' } , sinon . match . func ) ; chdirSpy . should . have . been . calledOnceWithExactly ( cwd ) ; } ) ; } ) ; } ) ;