Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Follow the Arrange, Act, Assert structure when writing unit tests.
- When writing unit tests, create individual unit tests that cover each logical branching in the code.
- For the happy path, can we have a single unit test where in the all the top level if conditions are executed? Maybe this might help with reducing the number of total unit tests created and still give same test coverage.
- For the happy path, can we have a single unit test where all the top level if conditions are executed? This might help with reducing the number of total unit tests created and still give same test coverage.
- For the tests for edge cases do not create separate describe blocks, keep the hierarchy flat.
- For the tests for edge cases, do not skip assertions, its still worth adding all assertions similar to the happy paths tests.

129 changes: 126 additions & 3 deletions src/adapters/base-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,120 @@ describe('BaseClass', () => {
);
});

it('should handle two options: Import from stack and Manually add variables', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Import variables from a stack', 'Manually add custom variables to the list'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle two options: Import from stack and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Import variables from a stack', 'Import variables from the .env.local file'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle two options: Manually add and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Manually add custom variables to the list', 'Import variables from the .env.local file'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle all three options: Import from stack, Manually add, and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: [
'Import variables from a stack',
'Manually add custom variables to the list',
'Import variables from the .env.local file',
],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should fail when Skip is combined with other options', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Skip adding environment variables', 'Import variables from a stack'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(undefined);

await baseClass.handleEnvImportFlow();

expect(logMock).toHaveBeenCalledWith(
"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.",
'error',
);
expect(exitMock).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).toHaveBeenCalled();
});

it('should exit if no options are selected', async () => {
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);

Expand All @@ -138,21 +252,30 @@ describe('BaseClass', () => {
});

it('should exit if "Skip adding environment variables" is selected with other options', async () => {
const exitMockWithThrow = jest.fn().mockImplementation(() => {
throw new Error('Exit called');
});
baseClass = new BaseClass({
log: logMock,
exit: exitMockWithThrow,
config: config.variablePreparationTypeOptions,
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
(ux.inquire as jest.Mock).mockResolvedValueOnce([
'Skip adding environment variables',
'Import variables from a stack',
]);

await baseClass.handleEnvImportFlow();
await expect(baseClass.handleEnvImportFlow()).rejects.toThrow('Exit called');

expect(logMock).toHaveBeenCalledWith(
"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.",
'error',
);

expect(exitMock).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).toHaveBeenCalled();
expect(exitMockWithThrow).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).not.toHaveBeenCalled();
});

it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/commands/launch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class Launch extends BaseCommand<typeof Launch> {
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Import variables from a stack" --alias=<value>',
// eslint-disable-next-line max-len
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Manually add custom variables to the list" --env-variables="APP_ENV:prod, TEST_ENV:testVal"',
// eslint-disable-next-line max-len
'<%= config.bin %> <%= command.id %> --config <path/to/launch/config/file> --type <options: GitHub|FileUpload> --name=<value> --environment=<value> --branch=<value> --build-command=<value> --framework=<option> --org=<value> --out-dir=<value> --variable-type="Import variables from a stack" --variable-type="Manually add custom variables to the list" --alias=<value>',
];

static flags: FlagInput = {
Expand Down Expand Up @@ -64,10 +66,11 @@ export default class Launch extends BaseCommand<typeof Launch> {
description: '[optional] Server Command.',
}),
'variable-type': Flags.string({
multiple: true,
options: [...config.variablePreparationTypeOptions],
description:
// eslint-disable-next-line max-len
'[optional] Provide a variable type. <options: Import variables from a stack|Manually add custom variables to the list|Import variables from the .env.local file|Skip adding environment variables>',
'[optional] Provide a variable type (can specify multiple times). <options: Import variables from a stack|Manually add custom variables to the list|Import variables from the .env.local file|Skip adding environment variables>',
}),
'show-variables': Flags.boolean({
hidden: true,
Expand Down
Loading