Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Fix issue regarding skipping env variables, suppressing non-dev error messages, parsing URL-formatted env values and handling empty server command inputs.#74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7a2d3da95075954da2e331f3dc69ce89e3fa6cec1fdf7a2711125814a9ce93741e39356c924f0b4747044d884375af1aac93a122261dfabfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| * @contentstack/security-admin | ||
| * @contentstack/launch-pr-reviewers | ||
| .github/workflows/sca-scan.yml @contentstack/security-admin | ||
| **/.snyk @contentstack/security-admin | ||
| .github/workflows/policy-scan.yml @contentstack/security-admin | ||
| .github/workflows/issues-jira.yml @contentstack/security-admin |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ## Testing instructions | ||
| - 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 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. | ||
| - Use only jest for writing test cases and refer existing unit test under the /src folder. | ||
| - Do not create code comments for any changes. | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -32,6 +32,213 @@ describe('BaseClass', () => { | ||
| config: config.variablePreparationTypeOptions, | ||
| } as any); | ||
| }); | ||
| it('should handle string variableType by converting to array - Import variables from a stack', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| variableType: 'Import variables from a stack', | ||
| variablePreparationTypeOptions: config.variablePreparationTypeOptions, | ||
| }, | ||
| } as any); | ||
| const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(importEnvFromStackMock).toHaveBeenCalled(); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should handle string variableType by converting to array - Manually add custom variables to the list', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| variableType: 'Manually add custom variables to the list', | ||
| variablePreparationTypeOptions: config.variablePreparationTypeOptions, | ||
| }, | ||
| } as any); | ||
| const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce(); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(promptForEnvValuesMock).toHaveBeenCalled(); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should handle string variableType by converting to array - Import variables from the .env.local file', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| variableType: 'Import variables from the .env.local file', | ||
| variablePreparationTypeOptions: config.variablePreparationTypeOptions, | ||
| }, | ||
| } as any); | ||
| const importVariableFromLocalConfigMock = jest | ||
| .spyOn(baseClass, 'importVariableFromLocalConfig') | ||
| .mockResolvedValueOnce(); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(importVariableFromLocalConfigMock).toHaveBeenCalled(); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should handle string variableType by converting to array - Skip adding environment variables', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| variableType: 'Skip adding environment variables', | ||
| variablePreparationTypeOptions: config.variablePreparationTypeOptions, | ||
| }, | ||
| } as any); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(baseClass.envVariables).toEqual([]); | ||
| expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info'); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should fail if string to array conversion is removed', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| variableType: 'Skip adding environment variables', | ||
| variablePreparationTypeOptions: config.variablePreparationTypeOptions, | ||
| }, | ||
| } as any); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| expect(logMock).not.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', | ||
| ); | ||
| }); | ||
| 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(); | ||
SakshiKoli-CS marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
| it('should exit if no options are selected', async () => { | ||
| (ux.inquire as jest.Mock).mockResolvedValueOnce([]); | ||
| @@ -45,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 () => { | ||
| @@ -162,7 +378,7 @@ describe('BaseClass', () => { | ||
| 'Import variables from the .env.local file', | ||
| ]); | ||
| await baseClass.handleEnvImportFlow(); | ||
| await baseClass.handleEnvImportFlow(); | ||
| expect(importEnvFromStackMock).toHaveBeenCalled(); | ||
| expect(promptForEnvValuesMock).toHaveBeenCalled(); | ||
| @@ -298,4 +514,36 @@ describe('BaseClass', () => { | ||
| expect(exitMock).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); | ||
| describe('handleEnvVariables', () => { | ||
| beforeEach(() => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: {}, | ||
| } as any); | ||
| }); | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
| it('should parse environment variables from config string with various value formats including URLs', async () => { | ||
| baseClass = new BaseClass({ | ||
| log: logMock, | ||
| exit: exitMock, | ||
| config: { | ||
| envVariables: 'APP_ENV:prod, API_URL:https://api.example.com/v1, DB_URL:postgresql://localhost:5432/dbname', | ||
| }, | ||
| } as any); | ||
| await baseClass.promptForEnvValues(); | ||
| expect(baseClass.envVariables).toEqual([ | ||
| { key: 'APP_ENV', value: 'prod' }, | ||
| { key: 'API_URL', value: 'https://api.example.com/v1' }, | ||
| { key: 'DB_URL', value: 'postgresql://localhost:5432/dbname' }, | ||
| ]); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.