@@ -3,14 +3,20 @@ import * as stubs from "./stubs";
3
3
import { CreatePluginCommand } from "../lib/commands/plugin/create-plugin" ;
4
4
import { assert } from "chai" ;
5
5
import 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 ( ) ;
6
11
7
12
interface IPacoteOutput {
8
13
packageName : string ;
9
14
destinationDirectory : string ;
10
15
}
11
16
12
17
const originalIsInteractive = helpers . isInteractive ;
13
- const dummyArgs = [ "dummyProjectName" ] ;
18
+ const dummyProjectName = "dummyProjectName" ;
19
+ const dummyArgs = [ dummyProjectName ] ;
14
20
const dummyUser = "devUsername" ;
15
21
const dummyName = "devPlugin" ;
16
22
const dummyPacote : IPacoteOutput = { packageName : "" , destinationDirectory : "" } ;
@@ -142,5 +148,62 @@ describe("Plugin create command tests", () => {
142
148
options . pluginName = dummyName ;
143
149
await createPluginCommand . execute ( dummyArgs ) ;
144
150
} ) ;
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
+ } ) ;
145
208
} ) ;
146
209
} ) ;
0 commit comments