1
1
'use strict' ;
2
- var commander = require ( "../src/cli/utils/commander" ) . default ;
2
+ import commander from '../src/cli/utils/commander' ;
3
+ import definitions from '../src/cli/definitions/parse-server' ;
3
4
4
- var definitions = {
5
- " arg0" : " PROGRAM_ARG_0" ,
6
- " arg1" : {
7
- env : " PROGRAM_ARG_1" ,
5
+ var testDefinitions = {
6
+ ' arg0' : ' PROGRAM_ARG_0' ,
7
+ ' arg1' : {
8
+ env : ' PROGRAM_ARG_1' ,
8
9
required : true
9
10
} ,
10
- " arg2" : {
11
- env : " PROGRAM_ARG_2" ,
11
+ ' arg2' : {
12
+ env : ' PROGRAM_ARG_2' ,
12
13
action : function ( value ) {
13
- var value = parseInt ( value ) ;
14
- if ( ! Number . isInteger ( value ) ) {
15
- throw " arg2 is invalid" ;
14
+ var intValue = parseInt ( value ) ;
15
+ if ( ! Number . isInteger ( intValue ) ) {
16
+ throw ' arg2 is invalid' ;
16
17
}
17
- return value ;
18
+ return intValue ;
18
19
}
19
20
} ,
20
- " arg3" : { } ,
21
- " arg4" : {
22
- default : " arg4Value"
21
+ ' arg3' : { } ,
22
+ ' arg4' : {
23
+ default : ' arg4Value'
23
24
}
24
- }
25
+ } ;
25
26
26
- describe ( " commander additions" , ( ) => {
27
+ describe ( ' commander additions' , ( ) => {
27
28
afterEach ( ( done ) => {
28
29
commander . options = [ ] ;
29
30
delete commander . arg0 ;
@@ -32,107 +33,126 @@ describe("commander additions", () => {
32
33
delete commander . arg3 ;
33
34
delete commander . arg4 ;
34
35
done ( ) ;
35
- } )
36
+ } ) ;
36
37
37
- it ( " should load properly definitions from args" , ( done ) => {
38
- commander . loadDefinitions ( definitions ) ;
39
- commander . parse ( [ " node" , " ./CLI.spec.js" , " --arg0" , " arg0Value" , " --arg1" , " arg1Value" , " --arg2" , "2" , " --arg3" , " some" ] ) ;
40
- expect ( commander . arg0 ) . toEqual ( " arg0Value" ) ;
41
- expect ( commander . arg1 ) . toEqual ( " arg1Value" ) ;
38
+ it ( ' should load properly definitions from args' , ( done ) => {
39
+ commander . loadDefinitions ( testDefinitions ) ;
40
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' --arg0' , ' arg0Value' , ' --arg1' , ' arg1Value' , ' --arg2' , '2' , ' --arg3' , ' some' ] ) ;
41
+ expect ( commander . arg0 ) . toEqual ( ' arg0Value' ) ;
42
+ expect ( commander . arg1 ) . toEqual ( ' arg1Value' ) ;
42
43
expect ( commander . arg2 ) . toEqual ( 2 ) ;
43
- expect ( commander . arg3 ) . toEqual ( " some" ) ;
44
- expect ( commander . arg4 ) . toEqual ( " arg4Value" ) ;
44
+ expect ( commander . arg3 ) . toEqual ( ' some' ) ;
45
+ expect ( commander . arg4 ) . toEqual ( ' arg4Value' ) ;
45
46
done ( ) ;
46
47
} ) ;
47
48
48
- it ( " should load properly definitions from env" , ( done ) => {
49
- commander . loadDefinitions ( definitions ) ;
49
+ it ( ' should load properly definitions from env' , ( done ) => {
50
+ commander . loadDefinitions ( testDefinitions ) ;
50
51
commander . parse ( [ ] , {
51
- " PROGRAM_ARG_0" : " arg0ENVValue" ,
52
- " PROGRAM_ARG_1" : " arg1ENVValue" ,
53
- " PROGRAM_ARG_2" : "3" ,
52
+ ' PROGRAM_ARG_0' : ' arg0ENVValue' ,
53
+ ' PROGRAM_ARG_1' : ' arg1ENVValue' ,
54
+ ' PROGRAM_ARG_2' : '3' ,
54
55
} ) ;
55
- expect ( commander . arg0 ) . toEqual ( " arg0ENVValue" ) ;
56
- expect ( commander . arg1 ) . toEqual ( " arg1ENVValue" ) ;
56
+ expect ( commander . arg0 ) . toEqual ( ' arg0ENVValue' ) ;
57
+ expect ( commander . arg1 ) . toEqual ( ' arg1ENVValue' ) ;
57
58
expect ( commander . arg2 ) . toEqual ( 3 ) ;
58
- expect ( commander . arg4 ) . toEqual ( " arg4Value" ) ;
59
+ expect ( commander . arg4 ) . toEqual ( ' arg4Value' ) ;
59
60
done ( ) ;
60
61
} ) ;
61
62
62
- it ( " should load properly use args over env" , ( done ) => {
63
- commander . loadDefinitions ( definitions ) ;
64
- commander . parse ( [ " node" , " ./CLI.spec.js" , " --arg0" , " arg0Value" , " --arg4" , " anotherArg4" ] , {
65
- " PROGRAM_ARG_0" : " arg0ENVValue" ,
66
- " PROGRAM_ARG_1" : " arg1ENVValue" ,
67
- " PROGRAM_ARG_2" : "4" ,
63
+ it ( ' should load properly use args over env' , ( done ) => {
64
+ commander . loadDefinitions ( testDefinitions ) ;
65
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' --arg0' , ' arg0Value' , ' --arg4' , ' anotherArg4' ] , {
66
+ ' PROGRAM_ARG_0' : ' arg0ENVValue' ,
67
+ ' PROGRAM_ARG_1' : ' arg1ENVValue' ,
68
+ ' PROGRAM_ARG_2' : '4' ,
68
69
} ) ;
69
- expect ( commander . arg0 ) . toEqual ( " arg0Value" ) ;
70
- expect ( commander . arg1 ) . toEqual ( " arg1ENVValue" ) ;
70
+ expect ( commander . arg0 ) . toEqual ( ' arg0Value' ) ;
71
+ expect ( commander . arg1 ) . toEqual ( ' arg1ENVValue' ) ;
71
72
expect ( commander . arg2 ) . toEqual ( 4 ) ;
72
- expect ( commander . arg4 ) . toEqual ( " anotherArg4" ) ;
73
+ expect ( commander . arg4 ) . toEqual ( ' anotherArg4' ) ;
73
74
done ( ) ;
74
75
} ) ;
75
76
76
- it ( " should fail in action as port is invalid" , ( done ) => {
77
- commander . loadDefinitions ( definitions ) ;
77
+ it ( ' should fail in action as port is invalid' , ( done ) => {
78
+ commander . loadDefinitions ( testDefinitions ) ;
78
79
expect ( ( ) => {
79
- commander . parse ( [ " node" , " ./CLI.spec.js" , " --arg0" , " arg0Value" ] , {
80
- " PROGRAM_ARG_0" : " arg0ENVValue" ,
81
- " PROGRAM_ARG_1" : " arg1ENVValue" ,
82
- " PROGRAM_ARG_2" : " hello" ,
80
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' --arg0' , ' arg0Value' ] , {
81
+ ' PROGRAM_ARG_0' : ' arg0ENVValue' ,
82
+ ' PROGRAM_ARG_1' : ' arg1ENVValue' ,
83
+ ' PROGRAM_ARG_2' : ' hello' ,
83
84
} ) ;
84
- } ) . toThrow ( " arg2 is invalid" ) ;
85
+ } ) . toThrow ( ' arg2 is invalid' ) ;
85
86
done ( ) ;
86
87
} ) ;
87
88
88
- it ( " should not override config.json" , ( done ) => {
89
- commander . loadDefinitions ( definitions ) ;
90
- commander . parse ( [ " node" , " ./CLI.spec.js" , " --arg0" , " arg0Value" , " ./spec/configs/CLIConfig.json" ] , {
91
- " PROGRAM_ARG_0" : " arg0ENVValue" ,
92
- " PROGRAM_ARG_1" : " arg1ENVValue" ,
89
+ it ( ' should not override config.json' , ( done ) => {
90
+ commander . loadDefinitions ( testDefinitions ) ;
91
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' --arg0' , ' arg0Value' , ' ./spec/configs/CLIConfig.json' ] , {
92
+ ' PROGRAM_ARG_0' : ' arg0ENVValue' ,
93
+ ' PROGRAM_ARG_1' : ' arg1ENVValue' ,
93
94
} ) ;
94
95
let options = commander . getOptions ( ) ;
95
96
expect ( options . arg2 ) . toBe ( 8888 ) ;
96
- expect ( options . arg3 ) . toBe ( " hello" ) ; //config value
97
+ expect ( options . arg3 ) . toBe ( ' hello' ) ; //config value
97
98
expect ( options . arg4 ) . toBe ( '/1' ) ;
98
99
done ( ) ;
99
100
} ) ;
100
101
101
- it ( " should fail with invalid values in JSON" , ( done ) => {
102
- commander . loadDefinitions ( definitions ) ;
102
+ it ( ' should fail with invalid values in JSON' , ( done ) => {
103
+ commander . loadDefinitions ( testDefinitions ) ;
103
104
expect ( ( ) => {
104
- commander . parse ( [ " node" , " ./CLI.spec.js" , " --arg0" , " arg0Value" , " ./spec/configs/CLIConfigFail.json" ] , {
105
- " PROGRAM_ARG_0" : " arg0ENVValue" ,
106
- " PROGRAM_ARG_1" : " arg1ENVValue" ,
105
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' --arg0' , ' arg0Value' , ' ./spec/configs/CLIConfigFail.json' ] , {
106
+ ' PROGRAM_ARG_0' : ' arg0ENVValue' ,
107
+ ' PROGRAM_ARG_1' : ' arg1ENVValue' ,
107
108
} ) ;
108
- } ) . toThrow ( " arg2 is invalid" )
109
+ } ) . toThrow ( ' arg2 is invalid' ) ;
109
110
done ( ) ;
110
111
} ) ;
111
112
112
- it ( " should fail when too many apps are set" , ( done ) => {
113
- commander . loadDefinitions ( definitions ) ;
113
+ it ( ' should fail when too many apps are set' , ( done ) => {
114
+ commander . loadDefinitions ( testDefinitions ) ;
114
115
expect ( ( ) => {
115
- commander . parse ( [ " node" , " ./CLI.spec.js" , " ./spec/configs/CLIConfigFailTooManyApps.json" ] ) ;
116
- } ) . toThrow ( " Multiple apps are not supported" )
116
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' ./spec/configs/CLIConfigFailTooManyApps.json' ] ) ;
117
+ } ) . toThrow ( ' Multiple apps are not supported' ) ;
117
118
done ( ) ;
118
119
} ) ;
119
120
120
- it ( " should load config from apps" , ( done ) => {
121
- commander . loadDefinitions ( definitions ) ;
122
- commander . parse ( [ " node" , " ./CLI.spec.js" , " ./spec/configs/CLIConfigApps.json" ] ) ;
121
+ it ( ' should load config from apps' , ( done ) => {
122
+ commander . loadDefinitions ( testDefinitions ) ;
123
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' ./spec/configs/CLIConfigApps.json' ] ) ;
123
124
let options = commander . getOptions ( ) ;
124
- expect ( options . arg1 ) . toBe ( " my_app" ) ;
125
+ expect ( options . arg1 ) . toBe ( ' my_app' ) ;
125
126
expect ( options . arg2 ) . toBe ( 8888 ) ;
126
- expect ( options . arg3 ) . toBe ( " hello" ) ; //config value
127
+ expect ( options . arg3 ) . toBe ( ' hello' ) ; //config value
127
128
expect ( options . arg4 ) . toBe ( '/1' ) ;
128
129
done ( ) ;
129
130
} ) ;
130
131
131
- it ( " should fail when passing an invalid arguement" , ( done ) => {
132
- commander . loadDefinitions ( definitions ) ;
132
+ it ( ' should fail when passing an invalid arguement' , ( done ) => {
133
+ commander . loadDefinitions ( testDefinitions ) ;
133
134
expect ( ( ) => {
134
- commander . parse ( [ " node" , " ./CLI.spec.js" , " ./spec/configs/CLIConfigUnknownArg.json" ] ) ;
135
- } ) . toThrow ( 'error: unknown option myArg' )
135
+ commander . parse ( [ ' node' , ' ./CLI.spec.js' , ' ./spec/configs/CLIConfigUnknownArg.json' ] ) ;
136
+ } ) . toThrow ( 'error: unknown option myArg' ) ;
136
137
done ( ) ;
137
138
} ) ;
138
139
} ) ;
140
+
141
+ describe ( 'definitions' , ( ) => {
142
+ it ( 'should have valid types' , ( ) => {
143
+ for ( let key in definitions ) {
144
+ let definition = definitions [ key ] ;
145
+ expect ( typeof definition ) . toBe ( 'object' ) ;
146
+ if ( typeof definition . required !== 'undefined' ) {
147
+ expect ( typeof definition . env ) . toBe ( 'string' ) ;
148
+ }
149
+ expect ( typeof definition . help ) . toBe ( 'string' ) ;
150
+ if ( typeof definition . required !== 'undefined' ) {
151
+ expect ( typeof definition . required ) . toBe ( 'boolean' ) ;
152
+ }
153
+ if ( typeof definition . action !== 'undefined' ) {
154
+ expect ( typeof definition . action ) . toBe ( 'function' ) ;
155
+ }
156
+ }
157
+ } ) ;
158
+ } ) ;
0 commit comments