@@ -32,6 +32,213 @@ describe('BaseClass', () => {
3232 config : config . variablePreparationTypeOptions ,
3333 } as any ) ;
3434 } ) ;
35+
36+ it ( 'should handle string variableType by converting to array - Import variables from a stack' , async ( ) => {
37+ baseClass = new BaseClass ( {
38+ log : logMock ,
39+ exit : exitMock ,
40+ config : {
41+ variableType : 'Import variables from a stack' ,
42+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
43+ } ,
44+ } as any ) ;
45+
46+ const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( ) ;
47+
48+ await baseClass . handleEnvImportFlow ( ) ;
49+
50+ expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
51+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
52+ } ) ;
53+
54+ it ( 'should handle string variableType by converting to array - Manually add custom variables to the list' , async ( ) => {
55+ baseClass = new BaseClass ( {
56+ log : logMock ,
57+ exit : exitMock ,
58+ config : {
59+ variableType : 'Manually add custom variables to the list' ,
60+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
61+ } ,
62+ } as any ) ;
63+
64+ const promptForEnvValuesMock = jest . spyOn ( baseClass , 'promptForEnvValues' ) . mockResolvedValueOnce ( ) ;
65+
66+ await baseClass . handleEnvImportFlow ( ) ;
67+
68+ expect ( promptForEnvValuesMock ) . toHaveBeenCalled ( ) ;
69+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
70+ } ) ;
71+
72+ it ( 'should handle string variableType by converting to array - Import variables from the .env.local file' , async ( ) => {
73+ baseClass = new BaseClass ( {
74+ log : logMock ,
75+ exit : exitMock ,
76+ config : {
77+ variableType : 'Import variables from the .env.local file' ,
78+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
79+ } ,
80+ } as any ) ;
81+
82+ const importVariableFromLocalConfigMock = jest
83+ . spyOn ( baseClass , 'importVariableFromLocalConfig' )
84+ . mockResolvedValueOnce ( ) ;
85+
86+ await baseClass . handleEnvImportFlow ( ) ;
87+
88+ expect ( importVariableFromLocalConfigMock ) . toHaveBeenCalled ( ) ;
89+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
90+ } ) ;
91+
92+ it ( 'should handle string variableType by converting to array - Skip adding environment variables' , async ( ) => {
93+ baseClass = new BaseClass ( {
94+ log : logMock ,
95+ exit : exitMock ,
96+ config : {
97+ variableType : 'Skip adding environment variables' ,
98+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
99+ } ,
100+ } as any ) ;
101+
102+ await baseClass . handleEnvImportFlow ( ) ;
103+
104+ expect ( baseClass . envVariables ) . toEqual ( [ ] ) ;
105+ expect ( logMock ) . toHaveBeenCalledWith ( 'Skipped adding environment variables.' , 'info' ) ;
106+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
107+ } ) ;
108+
109+ it ( 'should fail if string to array conversion is removed' , async ( ) => {
110+ baseClass = new BaseClass ( {
111+ log : logMock ,
112+ exit : exitMock ,
113+ config : {
114+ variableType : 'Skip adding environment variables' ,
115+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
116+ } ,
117+ } as any ) ;
118+
119+ await baseClass . handleEnvImportFlow ( ) ;
120+
121+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
122+ expect ( logMock ) . not . toHaveBeenCalledWith (
123+ "The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options." ,
124+ 'error' ,
125+ ) ;
126+ } ) ;
127+
128+ it ( 'should handle two options: Import from stack and Manually add variables' , async ( ) => {
129+ baseClass = new BaseClass ( {
130+ log : logMock ,
131+ exit : exitMock ,
132+ config : {
133+ variableType : [ 'Import variables from a stack' , 'Manually add custom variables to the list' ] ,
134+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
135+ } ,
136+ } as any ) ;
137+
138+ const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( ) ;
139+ const promptForEnvValuesMock = jest . spyOn ( baseClass , 'promptForEnvValues' ) . mockResolvedValueOnce ( ) ;
140+
141+ await baseClass . handleEnvImportFlow ( ) ;
142+
143+ expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
144+ expect ( promptForEnvValuesMock ) . toHaveBeenCalled ( ) ;
145+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
146+ } ) ;
147+
148+ it ( 'should handle two options: Import from stack and Import from .env.local' , async ( ) => {
149+ baseClass = new BaseClass ( {
150+ log : logMock ,
151+ exit : exitMock ,
152+ config : {
153+ variableType : [ 'Import variables from a stack' , 'Import variables from the .env.local file' ] ,
154+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
155+ } ,
156+ } as any ) ;
157+
158+ const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( ) ;
159+ const importVariableFromLocalConfigMock = jest
160+ . spyOn ( baseClass , 'importVariableFromLocalConfig' )
161+ . mockResolvedValueOnce ( ) ;
162+
163+ await baseClass . handleEnvImportFlow ( ) ;
164+
165+ expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
166+ expect ( importVariableFromLocalConfigMock ) . toHaveBeenCalled ( ) ;
167+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
168+ } ) ;
169+
170+ it ( 'should handle two options: Manually add and Import from .env.local' , async ( ) => {
171+ baseClass = new BaseClass ( {
172+ log : logMock ,
173+ exit : exitMock ,
174+ config : {
175+ variableType : [ 'Manually add custom variables to the list' , 'Import variables from the .env.local file' ] ,
176+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
177+ } ,
178+ } as any ) ;
179+
180+ const promptForEnvValuesMock = jest . spyOn ( baseClass , 'promptForEnvValues' ) . mockResolvedValueOnce ( ) ;
181+ const importVariableFromLocalConfigMock = jest
182+ . spyOn ( baseClass , 'importVariableFromLocalConfig' )
183+ . mockResolvedValueOnce ( ) ;
184+
185+ await baseClass . handleEnvImportFlow ( ) ;
186+
187+ expect ( promptForEnvValuesMock ) . toHaveBeenCalled ( ) ;
188+ expect ( importVariableFromLocalConfigMock ) . toHaveBeenCalled ( ) ;
189+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
190+ } ) ;
191+
192+ it ( 'should handle all three options: Import from stack, Manually add, and Import from .env.local' , async ( ) => {
193+ baseClass = new BaseClass ( {
194+ log : logMock ,
195+ exit : exitMock ,
196+ config : {
197+ variableType : [
198+ 'Import variables from a stack' ,
199+ 'Manually add custom variables to the list' ,
200+ 'Import variables from the .env.local file' ,
201+ ] ,
202+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
203+ } ,
204+ } as any ) ;
205+
206+ const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( ) ;
207+ const promptForEnvValuesMock = jest . spyOn ( baseClass , 'promptForEnvValues' ) . mockResolvedValueOnce ( ) ;
208+ const importVariableFromLocalConfigMock = jest
209+ . spyOn ( baseClass , 'importVariableFromLocalConfig' )
210+ . mockResolvedValueOnce ( ) ;
211+
212+ await baseClass . handleEnvImportFlow ( ) ;
213+
214+ expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
215+ expect ( promptForEnvValuesMock ) . toHaveBeenCalled ( ) ;
216+ expect ( importVariableFromLocalConfigMock ) . toHaveBeenCalled ( ) ;
217+ expect ( exitMock ) . not . toHaveBeenCalled ( ) ;
218+ } ) ;
219+
220+ it ( 'should fail when Skip is combined with other options' , async ( ) => {
221+ baseClass = new BaseClass ( {
222+ log : logMock ,
223+ exit : exitMock ,
224+ config : {
225+ variableType : [ 'Skip adding environment variables' , 'Import variables from a stack' ] ,
226+ variablePreparationTypeOptions : config . variablePreparationTypeOptions ,
227+ } ,
228+ } as any ) ;
229+
230+ const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( undefined ) ;
231+
232+ await baseClass . handleEnvImportFlow ( ) ;
233+
234+ expect ( logMock ) . toHaveBeenCalledWith (
235+ "The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options." ,
236+ 'error' ,
237+ ) ;
238+ expect ( exitMock ) . toHaveBeenCalledWith ( 1 ) ;
239+ expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
240+ } ) ;
241+
35242 it ( 'should exit if no options are selected' , async ( ) => {
36243 ( ux . inquire as jest . Mock ) . mockResolvedValueOnce ( [ ] ) ;
37244
@@ -45,21 +252,30 @@ describe('BaseClass', () => {
45252 } ) ;
46253
47254 it ( 'should exit if "Skip adding environment variables" is selected with other options' , async ( ) => {
255+ const exitMockWithThrow = jest . fn ( ) . mockImplementation ( ( ) => {
256+ throw new Error ( 'Exit called' ) ;
257+ } ) ;
258+ baseClass = new BaseClass ( {
259+ log : logMock ,
260+ exit : exitMockWithThrow ,
261+ config : config . variablePreparationTypeOptions ,
262+ } as any ) ;
263+
48264 const importEnvFromStackMock = jest . spyOn ( baseClass , 'importEnvFromStack' ) . mockResolvedValueOnce ( ) ;
49265 ( ux . inquire as jest . Mock ) . mockResolvedValueOnce ( [
50266 'Skip adding environment variables' ,
51267 'Import variables from a stack' ,
52268 ] ) ;
53269
54- await baseClass . handleEnvImportFlow ( ) ;
270+ await expect ( baseClass . handleEnvImportFlow ( ) ) . rejects . toThrow ( 'Exit called' ) ;
55271
56272 expect ( logMock ) . toHaveBeenCalledWith (
57273 "The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options." ,
58274 'error' ,
59275 ) ;
60276
61- expect ( exitMock ) . toHaveBeenCalledWith ( 1 ) ;
62- expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
277+ expect ( exitMockWithThrow ) . toHaveBeenCalledWith ( 1 ) ;
278+ expect ( importEnvFromStackMock ) . not . toHaveBeenCalled ( ) ;
63279 } ) ;
64280
65281 it ( 'should call importEnvFromStack if "Import variables from a stack" is selected' , async ( ) => {
@@ -162,7 +378,7 @@ describe('BaseClass', () => {
162378 'Import variables from the .env.local file' ,
163379 ] ) ;
164380
165- await baseClass . handleEnvImportFlow ( ) ;
381+ await baseClass . handleEnvImportFlow ( ) ;
166382
167383 expect ( importEnvFromStackMock ) . toHaveBeenCalled ( ) ;
168384 expect ( promptForEnvValuesMock ) . toHaveBeenCalled ( ) ;
@@ -298,4 +514,36 @@ describe('BaseClass', () => {
298514 expect ( exitMock ) . toHaveBeenCalledWith ( 1 ) ;
299515 } ) ;
300516 } ) ;
517+
518+ describe ( 'handleEnvVariables' , ( ) => {
519+ beforeEach ( ( ) => {
520+ baseClass = new BaseClass ( {
521+ log : logMock ,
522+ exit : exitMock ,
523+ config : { } ,
524+ } as any ) ;
525+ } ) ;
526+
527+ afterEach ( ( ) => {
528+ jest . clearAllMocks ( ) ;
529+ } ) ;
530+
531+ it ( 'should parse environment variables from config string with various value formats including URLs' , async ( ) => {
532+ baseClass = new BaseClass ( {
533+ log : logMock ,
534+ exit : exitMock ,
535+ config : {
536+ envVariables : 'APP_ENV:prod, API_URL:https://api.example.com/v1, DB_URL:postgresql://localhost:5432/dbname' ,
537+ } ,
538+ } as any ) ;
539+
540+ await baseClass . promptForEnvValues ( ) ;
541+
542+ expect ( baseClass . envVariables ) . toEqual ( [
543+ { key : 'APP_ENV' , value : 'prod' } ,
544+ { key : 'API_URL' , value : 'https://api.example.com/v1' } ,
545+ { key : 'DB_URL' , value : 'postgresql://localhost:5432/dbname' } ,
546+ ] ) ;
547+ } ) ;
548+ } ) ;
301549} ) ;
0 commit comments