@@ -7,11 +7,27 @@ const crypto = require('crypto')
77const render = require ( 'consolidate' ) . handlebars . render
88const inquirer = require ( 'inquirer' )
99const async = require ( 'async' )
10+ const extend = Object . assign || require ( 'util' ) . _extend
1011const generate = require ( '../../lib/generate' )
1112
1213const MOCK_TEMPLATE_REPO_PATH = './test/e2e/mock-template-repo'
1314const MOCK_TEMPLATE_BUILD_PATH = path . resolve ( './test/e2e/mock-template-build' )
1415
16+ function monkeyPatchInquirer ( answers ) {
17+ // monkey patch inquirer
18+ inquirer . prompt = ( questions , cb ) => {
19+ const key = questions [ 0 ] . name
20+ const _answers = { }
21+ const validate = questions [ 0 ] . validate
22+ const valid = validate ( answers [ key ] )
23+ if ( valid !== true ) {
24+ throw new Error ( valid )
25+ }
26+ _answers [ key ] = answers [ key ]
27+ cb ( _answers )
28+ } ;
29+ }
30+
1531describe ( 'vue-cli' , ( ) => {
1632 const answers = {
1733 name : 'vue-cli-test' ,
@@ -24,15 +40,8 @@ describe('vue-cli', () => {
2440 pick : 'no'
2541 }
2642
27- // monkey patch inquirer
28- inquirer . prompt = ( questions , cb ) => {
29- const key = questions [ 0 ] . name
30- const _answers = { }
31- _answers [ key ] = answers [ key ]
32- cb ( _answers )
33- }
34-
3543 it ( 'template generation' , done => {
44+ monkeyPatchInquirer ( answers )
3645 generate ( 'test' , MOCK_TEMPLATE_REPO_PATH , MOCK_TEMPLATE_BUILD_PATH , err => {
3746 if ( err ) done ( err )
3847
@@ -56,6 +65,7 @@ describe('vue-cli', () => {
5665 } )
5766
5867 it ( 'avoid rendering files that do not have mustaches' , done => {
68+ monkeyPatchInquirer ( answers )
5969 const binFilePath = `${ MOCK_TEMPLATE_REPO_PATH } /template/bin.file`
6070 const wstream = fs . createWriteStream ( binFilePath )
6171 wstream . write ( crypto . randomBytes ( 100 ) )
@@ -79,4 +89,14 @@ describe('vue-cli', () => {
7989 } )
8090 } )
8191 } )
92+
93+ it ( 'validate input value' , done => {
94+ // deep copy
95+ var invalidName = extend ( { } , answers , { name : 'INVALID-NAME' } )
96+ monkeyPatchInquirer ( invalidName )
97+ generate ( 'INVALID-NAME' , MOCK_TEMPLATE_REPO_PATH , MOCK_TEMPLATE_BUILD_PATH , err => {
98+ expect ( err ) . to . be . an ( 'error' ) ;
99+ done ( )
100+ } )
101+ } )
82102} )
0 commit comments