1+ 'use strict' ;
12var commander = require ( "../src/cli/utils/commander" ) . default ;
23
34var definitions = {
@@ -11,7 +12,7 @@ var definitions = {
1112 action : function ( value ) {
1213 var value = parseInt ( value ) ;
1314 if ( ! Number . isInteger ( value ) ) {
14- throw "port is invalid" ;
15+ throw "arg2 is invalid" ;
1516 }
1617 return value ;
1718 }
@@ -23,7 +24,7 @@ var definitions = {
2324}
2425
2526describe ( "commander additions" , ( ) => {
26-
27+
2728 afterEach ( ( done ) => {
2829 commander . options = [ ] ;
2930 delete commander . arg0 ;
@@ -33,7 +34,7 @@ describe("commander additions", () => {
3334 delete commander . arg4 ;
3435 done ( ) ;
3536 } )
36-
37+
3738 it ( "should load properly definitions from args" , ( done ) => {
3839 commander . loadDefinitions ( definitions ) ;
3940 commander . parse ( [ "node" , "./CLI.spec.js" , "--arg0" , "arg0Value" , "--arg1" , "arg1Value" , "--arg2" , "2" , "--arg3" , "some" ] ) ;
@@ -44,7 +45,7 @@ describe("commander additions", () => {
4445 expect ( commander . arg4 ) . toEqual ( "arg4Value" ) ;
4546 done ( ) ;
4647 } ) ;
47-
48+
4849 it ( "should load properly definitions from env" , ( done ) => {
4950 commander . loadDefinitions ( definitions ) ;
5051 commander . parse ( [ ] , {
@@ -58,7 +59,7 @@ describe("commander additions", () => {
5859 expect ( commander . arg4 ) . toEqual ( "arg4Value" ) ;
5960 done ( ) ;
6061 } ) ;
61-
62+
6263 it ( "should load properly use args over env" , ( done ) => {
6364 commander . loadDefinitions ( definitions ) ;
6465 commander . parse ( [ "node" , "./CLI.spec.js" , "--arg0" , "arg0Value" , "--arg4" , "anotherArg4" ] , {
@@ -72,7 +73,7 @@ describe("commander additions", () => {
7273 expect ( commander . arg4 ) . toEqual ( "anotherArg4" ) ;
7374 done ( ) ;
7475 } ) ;
75-
76+
7677 it ( "should fail in action as port is invalid" , ( done ) => {
7778 commander . loadDefinitions ( definitions ) ;
7879 expect ( ( ) => {
@@ -81,7 +82,58 @@ describe("commander additions", () => {
8182 "PROGRAM_ARG_1" : "arg1ENVValue" ,
8283 "PROGRAM_ARG_2" : "hello" ,
8384 } ) ;
84- } ) . toThrow ( "port is invalid" ) ;
85+ } ) . toThrow ( "arg2 is invalid" ) ;
86+ done ( ) ;
87+ } ) ;
88+
89+ it ( "should not override config.json" , ( done ) => {
90+ commander . loadDefinitions ( definitions ) ;
91+ commander . parse ( [ "node" , "./CLI.spec.js" , "--arg0" , "arg0Value" , "./spec/configs/CLIConfig.json" ] , {
92+ "PROGRAM_ARG_0" : "arg0ENVValue" ,
93+ "PROGRAM_ARG_1" : "arg1ENVValue" ,
94+ } ) ;
95+ let options = commander . getOptions ( ) ;
96+ expect ( options . arg2 ) . toBe ( 8888 ) ;
97+ expect ( options . arg3 ) . toBe ( "hello" ) ; //config value
98+ expect ( options . arg4 ) . toBe ( '/1' ) ;
99+ done ( ) ;
100+ } ) ;
101+
102+ it ( "should fail with invalid values in JSON" , ( done ) => {
103+ commander . loadDefinitions ( definitions ) ;
104+ expect ( ( ) => {
105+ commander . parse ( [ "node" , "./CLI.spec.js" , "--arg0" , "arg0Value" , "./spec/configs/CLIConfigFail.json" ] , {
106+ "PROGRAM_ARG_0" : "arg0ENVValue" ,
107+ "PROGRAM_ARG_1" : "arg1ENVValue" ,
108+ } ) ;
109+ } ) . toThrow ( "arg2 is invalid" )
110+ done ( ) ;
111+ } ) ;
112+
113+ it ( "should fail when too many apps are set" , ( done ) => {
114+ commander . loadDefinitions ( definitions ) ;
115+ expect ( ( ) => {
116+ commander . parse ( [ "node" , "./CLI.spec.js" , "./spec/configs/CLIConfigFailTooManyApps.json" ] ) ;
117+ } ) . toThrow ( "Multiple apps are not supported" )
118+ done ( ) ;
119+ } ) ;
120+
121+ it ( "should load config from apps" , ( done ) => {
122+ commander . loadDefinitions ( definitions ) ;
123+ commander . parse ( [ "node" , "./CLI.spec.js" , "./spec/configs/CLIConfigApps.json" ] ) ;
124+ let options = commander . getOptions ( ) ;
125+ expect ( options . arg1 ) . toBe ( "my_app" ) ;
126+ expect ( options . arg2 ) . toBe ( 8888 ) ;
127+ expect ( options . arg3 ) . toBe ( "hello" ) ; //config value
128+ expect ( options . arg4 ) . toBe ( '/1' ) ;
129+ done ( ) ;
130+ } ) ;
131+
132+ it ( "should fail when passing an invalid arguement" , ( done ) => {
133+ commander . loadDefinitions ( definitions ) ;
134+ expect ( ( ) => {
135+ commander . parse ( [ "node" , "./CLI.spec.js" , "./spec/configs/CLIConfigUnknownArg.json" ] ) ;
136+ } ) . toThrow ( 'error: unknown option myArg' )
85137 done ( ) ;
86138 } ) ;
87- } ) ;
139+ } ) ;
0 commit comments