Skip to content

Commit 5a10cea

Browse files
committed
CL-2062 | +Harshi | Fix skip environment variables option with --variable-type flag
1 parent 1f3dc69 commit 5a10cea

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

src/adapters/base-class.test.ts

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,99 @@ 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+
35128
it('should exit if no options are selected', async () => {
36129
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);
37130

@@ -162,7 +255,7 @@ describe('BaseClass', () => {
162255
'Import variables from the .env.local file',
163256
]);
164257

165-
await baseClass.handleEnvImportFlow();
258+
await baseClass.handleEnvImportFlow();
166259

167260
expect(importEnvFromStackMock).toHaveBeenCalled();
168261
expect(promptForEnvValuesMock).toHaveBeenCalled();

src/adapters/base-class.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ export default class BaseClass {
504504
* @memberof BaseClass
505505
*/
506506
async handleEnvImportFlow(): Promise<void> {
507-
const variablePreparationType =
507+
let variablePreparationType: string | string[] =
508508
this.config.variableType ||
509509
(await ux.inquire({
510510
type: 'checkbox',
@@ -514,6 +514,10 @@ export default class BaseClass {
514514
message: 'Import variables from a stack and/or manually add custom variables to the list',
515515
}));
516516

517+
if (typeof variablePreparationType === 'string') {
518+
variablePreparationType = [variablePreparationType];
519+
}
520+
517521
if (variablePreparationType.length === 0) {
518522
this.log('Please select at least one option by pressing <space>, then press <enter> to proceed.', 'error');
519523
this.exit(1);

0 commit comments

Comments
 (0)