@@ -6,6 +6,7 @@ import * as stubs from "./stubs";
66import * as constants from "./../lib/constants" ;
77import { ChildProcess } from "../lib/common/child-process" ;
88import * as ProjectServiceLib from "../lib/services/project-service" ;
9+ import { ProjectNameService } from "../lib/services/project-name-service" ;
910import * as ProjectDataServiceLib from "../lib/services/project-data-service" ;
1011import * as ProjectDataLib from "../lib/project-data" ;
1112import * as ProjectHelperLib from "../lib/common/project-helper" ;
@@ -21,11 +22,16 @@ import {assert} from "chai";
2122import { Options } from "../lib/options" ;
2223import { HostInfo } from "../lib/common/host-info" ;
2324import { ProjectTemplatesService } from "../lib/services/project-templates-service" ;
25+ import Future = require( "fibers/future" ) ;
2426
2527let mockProjectNameValidator = {
26- validate : ( ) => { return true ; }
28+ validate : ( ) => true
2729} ;
2830
31+ let dummyString : string = "dummyString" ;
32+ let hasPromptedForString = false ;
33+ let originalIsInteractive = helpers . isInteractive ;
34+
2935temp . track ( ) ;
3036
3137class ProjectIntegrationTest {
@@ -121,6 +127,7 @@ class ProjectIntegrationTest {
121127 this . testInjector . register ( "errors" , stubs . ErrorsStub ) ;
122128 this . testInjector . register ( 'logger' , stubs . LoggerStub ) ;
123129 this . testInjector . register ( "projectService" , ProjectServiceLib . ProjectService ) ;
130+ this . testInjector . register ( "projectNameService" , ProjectNameService ) ;
124131 this . testInjector . register ( "projectHelper" , ProjectHelperLib . ProjectHelper ) ;
125132 this . testInjector . register ( "projectTemplatesService" , ProjectTemplatesService ) ;
126133 this . testInjector . register ( "projectNameValidator" , mockProjectNameValidator ) ;
@@ -136,6 +143,15 @@ class ProjectIntegrationTest {
136143
137144 this . testInjector . register ( "options" , Options ) ;
138145 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+ } ) ;
139155 }
140156}
141157
@@ -299,6 +315,115 @@ describe("Project Service Tests", () => {
299315 projectIntegrationTest . createProject ( projectName ) . wait ( ) ;
300316 projectIntegrationTest . assertProject ( tempFolder , projectName , options . appid ) . wait ( ) ;
301317 } ) ;
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+
302427 } ) ;
303428} ) ;
304429
0 commit comments