@@ -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" ;
@@ -21,11 +22,16 @@ import {assert} from "chai";
21
22
import { Options } from "../lib/options" ;
22
23
import { HostInfo } from "../lib/common/host-info" ;
23
24
import { ProjectTemplatesService } from "../lib/services/project-templates-service" ;
25
+ import Future = require( "fibers/future" ) ;
24
26
25
27
let mockProjectNameValidator = {
26
- validate : ( ) => { return true ; }
28
+ validate : ( ) => true
27
29
} ;
28
30
31
+ let dummyString : string = "dummyString" ;
32
+ let hasPromptedForString = false ;
33
+ let originalIsInteractive = helpers . isInteractive ;
34
+
29
35
temp . track ( ) ;
30
36
31
37
class ProjectIntegrationTest {
@@ -121,6 +127,7 @@ class ProjectIntegrationTest {
121
127
this . testInjector . register ( "errors" , stubs . ErrorsStub ) ;
122
128
this . testInjector . register ( 'logger' , stubs . LoggerStub ) ;
123
129
this . testInjector . register ( "projectService" , ProjectServiceLib . ProjectService ) ;
130
+ this . testInjector . register ( "projectNameService" , ProjectNameService ) ;
124
131
this . testInjector . register ( "projectHelper" , ProjectHelperLib . ProjectHelper ) ;
125
132
this . testInjector . register ( "projectTemplatesService" , ProjectTemplatesService ) ;
126
133
this . testInjector . register ( "projectNameValidator" , mockProjectNameValidator ) ;
@@ -136,6 +143,15 @@ class ProjectIntegrationTest {
136
143
137
144
this . testInjector . register ( "options" , Options ) ;
138
145
this . testInjector . register ( "hostInfo" , HostInfo ) ;
146
+ this . testInjector . register ( "prompter" , {
147
+ confirm : ( message : string ) : IFuture < boolean > => Future . fromResult ( true ) ,
148
+ getString : ( message : string ) : IFuture < string > => {
149
+ return ( ( ) => {
150
+ hasPromptedForString = true ;
151
+ return dummyString ;
152
+ } ) . future < string > ( ) ( ) ;
153
+ }
154
+ } ) ;
139
155
}
140
156
}
141
157
@@ -299,6 +315,115 @@ describe("Project Service Tests", () => {
299
315
projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
300
316
projectIntegrationTest . assertProject ( tempFolder , projectName , options . appid ) . wait ( ) ;
301
317
} ) ;
318
+
319
+ describe ( "project name validation tests" , ( ) => {
320
+ let validProjectName = "valid" ;
321
+ let invalidProjectName = "1invalid" ;
322
+ let projectIntegrationTest : ProjectIntegrationTest ;
323
+ let tempFolder : string ;
324
+ let options : IOptions ;
325
+ let prompter : IPrompter ;
326
+
327
+ beforeEach ( ( ) => {
328
+ hasPromptedForString = false ;
329
+ helpers . isInteractive = ( ) => true ;
330
+ projectIntegrationTest = new ProjectIntegrationTest ( ) ;
331
+ tempFolder = temp . mkdirSync ( "project" ) ;
332
+ options = projectIntegrationTest . testInjector . resolve ( "options" ) ;
333
+ prompter = projectIntegrationTest . testInjector . resolve ( "prompter" ) ;
334
+ } ) ;
335
+
336
+ afterEach ( ( ) => {
337
+ helpers . isInteractive = originalIsInteractive ;
338
+ } ) ;
339
+
340
+ it ( "creates project when is interactive and incorrect name is specified and the --force option is set" , ( ) => {
341
+ let projectName = invalidProjectName ;
342
+
343
+ options . force = true ;
344
+ options . path = tempFolder ;
345
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
346
+
347
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
348
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
349
+ } ) ;
350
+
351
+ it ( "creates project when is interactive and incorrect name is specified and the user confirms to use the incorrect name" , ( ) => {
352
+ let projectName = invalidProjectName ;
353
+ prompter . confirm = ( message : string ) : IFuture < boolean > => Future . fromResult ( true ) ;
354
+
355
+ options . path = tempFolder ;
356
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
357
+
358
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
359
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
360
+ } ) ;
361
+
362
+ it ( "prompts for new name when is interactive and incorrect name is specified and the user does not confirm to use the incorrect name" , ( ) => {
363
+ let projectName = invalidProjectName ;
364
+
365
+ prompter . confirm = ( message : string ) : IFuture < boolean > => Future . fromResult ( false ) ;
366
+
367
+ options . path = tempFolder ;
368
+
369
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
370
+ assert . isTrue ( hasPromptedForString ) ;
371
+ } ) ;
372
+
373
+ 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" , ( ) => {
374
+ let projectName = invalidProjectName ;
375
+
376
+ prompter . confirm = ( message : string ) : IFuture < boolean > => Future . fromResult ( false ) ;
377
+
378
+ let incorrectInputsLimit = 5 ;
379
+ let incorrectInputsCount = 0 ;
380
+
381
+ prompter . getString = ( message : string ) : IFuture < string > => {
382
+ return ( ( ) => {
383
+ if ( incorrectInputsCount < incorrectInputsLimit ) {
384
+ incorrectInputsCount ++ ;
385
+ }
386
+ else {
387
+ hasPromptedForString = true ;
388
+
389
+ return validProjectName ;
390
+ }
391
+
392
+ return projectName ;
393
+ } ) . future < string > ( ) ( ) ;
394
+ } ;
395
+
396
+ options . path = tempFolder ;
397
+
398
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
399
+ assert . isTrue ( hasPromptedForString ) ;
400
+ } ) ;
401
+
402
+ it ( "does not create project when is not interactive and incorrect name is specified" , ( ) => {
403
+ let projectName = invalidProjectName ;
404
+ helpers . isInteractive = ( ) => false ;
405
+
406
+ options . force = false ;
407
+ options . path = tempFolder ;
408
+
409
+ assert . throws ( ( ) => {
410
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
411
+ } ) ;
412
+ } ) ;
413
+
414
+ it ( "creates project when is not interactive and incorrect name is specified and the --force option is set" , ( ) => {
415
+ let projectName = invalidProjectName ;
416
+ helpers . isInteractive = ( ) => false ;
417
+
418
+ options . force = true ;
419
+ options . path = tempFolder ;
420
+
421
+ projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
422
+ options . copyFrom = projectIntegrationTest . getNpmPackagePath ( "tns-template-hello-world" ) . wait ( ) ;
423
+ projectIntegrationTest . assertProject ( tempFolder , projectName , `org.nativescript.${ projectName } ` ) . wait ( ) ;
424
+ } ) ;
425
+ } ) ;
426
+
302
427
} ) ;
303
428
} ) ;
304
429
0 commit comments