@@ -7,11 +7,27 @@ const crypto = require('crypto')
7
7
const render = require ( 'consolidate' ) . handlebars . render
8
8
const inquirer = require ( 'inquirer' )
9
9
const async = require ( 'async' )
10
+ const extend = Object . assign || require ( 'util' ) . _extend
10
11
const generate = require ( '../../lib/generate' )
11
12
12
13
const MOCK_TEMPLATE_REPO_PATH = './test/e2e/mock-template-repo'
13
14
const MOCK_TEMPLATE_BUILD_PATH = path . resolve ( './test/e2e/mock-template-build' )
14
15
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
+
15
31
describe ( 'vue-cli' , ( ) => {
16
32
const answers = {
17
33
name : 'vue-cli-test' ,
@@ -24,15 +40,8 @@ describe('vue-cli', () => {
24
40
pick : 'no'
25
41
}
26
42
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
-
35
43
it ( 'template generation' , done => {
44
+ monkeyPatchInquirer ( answers )
36
45
generate ( 'test' , MOCK_TEMPLATE_REPO_PATH , MOCK_TEMPLATE_BUILD_PATH , err => {
37
46
if ( err ) done ( err )
38
47
@@ -56,6 +65,7 @@ describe('vue-cli', () => {
56
65
} )
57
66
58
67
it ( 'avoid rendering files that do not have mustaches' , done => {
68
+ monkeyPatchInquirer ( answers )
59
69
const binFilePath = `${ MOCK_TEMPLATE_REPO_PATH } /template/bin.file`
60
70
const wstream = fs . createWriteStream ( binFilePath )
61
71
wstream . write ( crypto . randomBytes ( 100 ) )
@@ -79,4 +89,14 @@ describe('vue-cli', () => {
79
89
} )
80
90
} )
81
91
} )
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
+ } )
82
102
} )
0 commit comments