@@ -3,14 +3,20 @@ import * as stubs from "./stubs";
33import { CreatePluginCommand } from "../lib/commands/plugin/create-plugin" ;
44import { assert } from "chai" ;
55import helpers = require( "../lib/common/helpers" ) ;
6+ import * as sinon from "sinon" ;
7+ import temp = require( "temp" ) ;
8+ import * as path from "path" ;
9+ import * as util from "util" ;
10+ temp . track ( ) ;
611
712interface IPacoteOutput {
813 packageName : string ;
914 destinationDirectory : string ;
1015}
1116
1217const originalIsInteractive = helpers . isInteractive ;
13- const dummyArgs = [ "dummyProjectName" ] ;
18+ const dummyProjectName = "dummyProjectName" ;
19+ const dummyArgs = [ dummyProjectName ] ;
1420const dummyUser = "devUsername" ;
1521const dummyName = "devPlugin" ;
1622const dummyPacote : IPacoteOutput = { packageName : "" , destinationDirectory : "" } ;
@@ -142,5 +148,62 @@ describe("Plugin create command tests", () => {
142148 options . pluginName = dummyName ;
143149 await createPluginCommand . execute ( dummyArgs ) ;
144150 } ) ;
151+
152+ describe ( "when fails" , ( ) => {
153+ let sandbox : sinon . SinonSandbox ;
154+ let fsSpy : sinon . SinonSpy ;
155+ let projectPath : string ;
156+
157+ beforeEach ( ( ) => {
158+ sandbox = sinon . sandbox . create ( ) ;
159+ const workingPath = temp . mkdirSync ( "test_plugin" ) ;
160+ options . path = workingPath ;
161+ projectPath = path . join ( workingPath , dummyProjectName ) ;
162+ const fsService = testInjector . resolve ( "fs" ) ;
163+ fsSpy = sandbox . spy ( fsService , "deleteDirectory" ) ;
164+ } ) ;
165+
166+ afterEach ( ( ) => {
167+ sandbox . restore ( ) ;
168+ } ) ;
169+
170+ it ( "downloadPackage, should remove projectDir" , async ( ) => {
171+ const errorMessage = "Test fail" ;
172+ const pacoteService = testInjector . resolve ( "pacoteService" ) ;
173+ sandbox . stub ( pacoteService , "extractPackage" ) . callsFake ( ( ) => {
174+ return Promise . reject ( new Error ( errorMessage ) ) ;
175+ } ) ;
176+
177+ const executePromise = createPluginCommand . execute ( dummyArgs ) ;
178+
179+ await assert . isRejected ( executePromise , errorMessage ) ;
180+ assert ( fsSpy . calledWith ( projectPath ) ) ;
181+ } ) ;
182+
183+ it ( "setupSeed, should remove projectDir" , async ( ) => {
184+ const errorMessage = "Test fail" ;
185+ const npmService = testInjector . resolve ( "npm" ) ;
186+ sandbox . stub ( npmService , "install" ) . callsFake ( ( ) => {
187+ return Promise . reject ( new Error ( errorMessage ) ) ;
188+ } ) ;
189+
190+ const executePromise = createPluginCommand . execute ( dummyArgs ) ;
191+
192+ await assert . isRejected ( executePromise , errorMessage ) ;
193+ assert ( fsSpy . calledWith ( projectPath ) ) ;
194+ } ) ;
195+
196+ it ( "ensurePachageDir should not remove projectDir" , async ( ) => {
197+ const fsService = testInjector . resolve ( "fs" ) ;
198+ sandbox . stub ( fsService , "isEmptyDir" ) . callsFake ( ( ) => {
199+ return false ;
200+ } ) ;
201+
202+ const executePromise = createPluginCommand . execute ( dummyArgs ) ;
203+
204+ await assert . isRejected ( executePromise , util . format ( createPluginCommand . pathAlreadyExistsMessageTemplate , projectPath ) ) ;
205+ assert ( fsSpy . notCalled ) ;
206+ } ) ;
207+ } ) ;
145208 } ) ;
146209} ) ;
0 commit comments