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