@@ -6,6 +6,7 @@ import * as stubs from "./stubs";
6
6
import * as constants from "./../lib/constants" ;
7
7
import { ChildProcess } from "../lib/common/child-process" ;
8
8
import * as ProjectServiceLib from "../lib/services/project-service" ;
9
+ import { ProjectNameService } from "../lib/services/project-name-service" ;
9
10
import * as ProjectDataServiceLib from "../lib/services/project-data-service" ;
10
11
import * as ProjectDataLib from "../lib/project-data" ;
11
12
import * as ProjectHelperLib from "../lib/common/project-helper" ;
@@ -26,6 +27,10 @@ let mockProjectNameValidator = {
26
27
validate : ( ) => { return true ; }
27
28
} ;
28
29
30
+ let dummyString : string = "dummyString" ;
31
+ let hasPromptedForString = false ;
32
+ let originalIsInteractive = helpers . isInteractive ;
33
+
29
34
temp . track ( ) ;
30
35
31
36
class ProjectIntegrationTest {
@@ -121,6 +126,7 @@ class ProjectIntegrationTest {
121
126
this . testInjector . register ( "errors" , stubs . ErrorsStub ) ;
122
127
this . testInjector . register ( 'logger' , stubs . LoggerStub ) ;
123
128
this . testInjector . register ( "projectService" , ProjectServiceLib . ProjectService ) ;
129
+ this . testInjector . register ( "projectNameService" , ProjectNameService ) ;
124
130
this . testInjector . register ( "projectHelper" , ProjectHelperLib . ProjectHelper ) ;
125
131
this . testInjector . register ( "projectTemplatesService" , ProjectTemplatesService ) ;
126
132
this . testInjector . register ( "projectNameValidator" , mockProjectNameValidator ) ;
@@ -136,6 +142,19 @@ class ProjectIntegrationTest {
136
142
137
143
this . testInjector . register ( "options" , Options ) ;
138
144
this . testInjector . register ( "hostInfo" , HostInfo ) ;
145
+ this . testInjector . register ( "prompter" , {
146
+ confirm : ( message : string ) : IFuture < boolean > => {
147
+ return ( ( ) => {
148
+ return true ;
149
+ } ) . future < boolean > ( ) ( ) ;
150
+ } ,
151
+ getString : ( message : string ) : IFuture < string > => {
152
+ return ( ( ) => {
153
+ hasPromptedForString = true ;
154
+ return dummyString ;
155
+ } ) . future < string > ( ) ( ) ;
156
+ }
157
+ } ) ;
139
158
}
140
159
}
141
160
@@ -273,6 +292,153 @@ describe("Project Service Tests", () => {
273
292
projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
274
293
projectIntegrationTest . assertProject ( tempFolder , projectName , options . appid ) . wait ( ) ;
275
294
} ) ;
295
+
296
+ describe ( "project name validation tests" , ( ) => {
297
+ let validProjectName = "valid" ;
298
+ let invalidProjectName = "1invalid" ;
299
+
300
+ beforeEach ( ( ) => {
301
+ hasPromptedForString = false ;
302
+ } )
303
+
304
+ afterEach ( ( ) => {
305
+ helpers . isInteractive = originalIsInteractive ;
306
+ } ) ;
307
+
308
+ it ( "creates project when is interactive and incorrect name is specified and the --force option is set" , ( ) => {
309
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
310
+ let tempFolder = temp . mkdirSync ( "project" ) ;
311
+ let projectName = invalidProjectName ;
312
+ helpers . isInteractive = ( ) => { return true ; } ;
313
+
314
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
315
+ options . force = true ;
316
+
317
+ options . path = tempFolder ;
318
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
319
+
320
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
321
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
322
+ } ) ;
323
+
324
+ it ( "creates project when is interactive and incorrect name is specified and the user confirms to use the incorrect name" , ( ) => {
325
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
326
+ let tempFolder = temp . mkdirSync ( "project" ) ;
327
+ let projectName = invalidProjectName ;
328
+ helpers . isInteractive = ( ) => { return true ; } ;
329
+
330
+ let prompter = projectIntegrationTest . testInjector . resolve ( "prompter" ) ;
331
+ prompter . confirm = ( message : string ) : IFuture < boolean > => {
332
+ return ( ( ) => {
333
+ return true ;
334
+ } ) . future < boolean > ( ) ( ) ;
335
+ } ;
336
+
337
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
338
+
339
+ options . path = tempFolder ;
340
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
341
+
342
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
343
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
344
+ } ) ;
345
+
346
+ it ( "prompts for new name when is interactive and incorrect name is specified and the user does not confirm to use the incorrect name" , ( ) => {
347
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
348
+ let tempFolder = temp . mkdirSync ( "project" ) ;
349
+ let projectName = invalidProjectName ;
350
+ helpers . isInteractive = ( ) => { return true ; } ;
351
+
352
+ let prompter = projectIntegrationTest . testInjector . resolve ( "prompter" ) ;
353
+ prompter . confirm = ( message : string ) : IFuture < boolean > => {
354
+ return ( ( ) => {
355
+ return false ;
356
+ } ) . future < boolean > ( ) ( ) ;
357
+ } ;
358
+
359
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
360
+
361
+ options . path = tempFolder ;
362
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
363
+
364
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
365
+ assert . isTrue ( hasPromptedForString ) ;
366
+ } ) ;
367
+
368
+ it ( "creates project when is interactive and incorrect name is specified and the user does not confirm to use the incorrect name and enters incorrect name again several times and then enters correct name" , ( ) => {
369
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
370
+ let tempFolder = temp . mkdirSync ( "project" ) ;
371
+ let projectName = invalidProjectName ;
372
+ helpers . isInteractive = ( ) => { return true ; } ;
373
+
374
+ let prompter = projectIntegrationTest . testInjector . resolve ( "prompter" ) ;
375
+ prompter . confirm = ( message : string ) : IFuture < boolean > => {
376
+ return ( ( ) => {
377
+ return false ;
378
+ } ) . future < boolean > ( ) ( ) ;
379
+ } ;
380
+
381
+ let incorrectInputsLimit = 20 ;
382
+ let incorrectInputsCount = 0 ;
383
+
384
+ prompter . getString = ( message : string ) : IFuture < string > => {
385
+ return ( ( ) => {
386
+ if ( incorrectInputsCount < incorrectInputsLimit ) {
387
+ incorrectInputsCount ++ ;
388
+ }
389
+ else {
390
+ hasPromptedForString = true ;
391
+
392
+ return validProjectName ;
393
+ }
394
+
395
+ return projectName ;
396
+ } ) . future < string > ( ) ( ) ;
397
+ } ;
398
+
399
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
400
+
401
+ options . path = tempFolder ;
402
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
403
+
404
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
405
+ assert . isTrue ( hasPromptedForString ) ;
406
+ } ) ;
407
+
408
+ it ( "does not create project when is not interactive and incorrect name is specified" , ( ) => {
409
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
410
+ let tempFolder = temp . mkdirSync ( "project" ) ;
411
+ let projectName = invalidProjectName ;
412
+ helpers . isInteractive = ( ) => { return false ; } ;
413
+
414
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
415
+ options . force = false ;
416
+
417
+ options . path = tempFolder ;
418
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
419
+
420
+ assert . throws ( ( ) => {
421
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
422
+ } ) ;
423
+ } ) ;
424
+
425
+ it ( "creates project when is not interactive and incorrect name is specified and the --force option is set" , ( ) => {
426
+ let projectIntegrationTest = new ProjectIntegrationTest ( ) ;
427
+ let tempFolder = temp . mkdirSync ( "project" ) ;
428
+ let projectName = invalidProjectName ;
429
+ helpers . isInteractive = ( ) => { return false ; } ;
430
+
431
+ let options : IOptions = projectIntegrationTest . testInjector . resolve ( "options" ) ;
432
+ options . force = true ;
433
+
434
+ options . path = tempFolder ;
435
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
436
+
437
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
438
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
439
+ } ) ;
440
+ } ) ;
441
+
276
442
} ) ;
277
443
} ) ;
278
444
0 commit comments