Skip to content

Commit

Permalink
chore: fixed bug in a condition, added missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radekl committed Jul 12, 2023
1 parent 94e6095 commit c74a28e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
93 changes: 92 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ describe('Deploy CloudFormation Stack', () => {
expect(mockExecuteChangeSet).toHaveBeenCalledTimes(0)
})

test('deploys the stack with template', async () => {
test('deploys the stack with prefixed envs', async () => {
const inputs: Inputs = {
name: 'MockStack',
template: 'template.yaml',
Expand All @@ -1225,6 +1225,8 @@ describe('Deploy CloudFormation Stack', () => {

await run()

delete process.env.CFD_AdminNickname

expect(core.setFailed).toHaveBeenCalledTimes(0)
expect(mockDescribeStacks).toHaveBeenCalledTimes(2)
expect(mockDescribeStacks).toHaveBeenNthCalledWith(1, {
Expand All @@ -1248,6 +1250,95 @@ describe('Deploy CloudFormation Stack', () => {
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'stack-id', mockStackId)
})

test('deploys the stack with prefixed envs but no envs are passed', async () => {
const inputs: Inputs = {
name: 'MockStack',
template: 'template.yaml',
capabilities: 'CAPABILITY_IAM',
'parameter-overrides': 'AdminEmail=no-reply@amazon.com',
'envs-prefix-for-parameter-overrides': 'CFD_',
'no-fail-on-empty-changeset': '0',
'disable-rollback': '0',
'timeout-in-minutes': '',
'notification-arns': '',
'role-arn': '',
tags: '',
'termination-protection': ''
}

jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
return inputs[name]
})

await run()

expect(core.setFailed).toHaveBeenCalledTimes(0)
expect(mockDescribeStacks).toHaveBeenCalledTimes(2)
expect(mockDescribeStacks).toHaveBeenNthCalledWith(1, {
StackName: 'MockStack'
})
expect(mockDescribeStacks).toHaveBeenNthCalledWith(2, {
StackName: mockStackId
})
expect(mockCreateStack).toHaveBeenNthCalledWith(1, {
StackName: 'MockStack',
TemplateBody: mockTemplate,
Capabilities: ['CAPABILITY_IAM'],
Parameters: [
{ ParameterKey: 'AdminEmail', ParameterValue: 'no-reply@amazon.com' }
],
DisableRollback: false,
EnableTerminationProtection: false
})
expect(core.setOutput).toHaveBeenCalledTimes(1)
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'stack-id', mockStackId)
})

test('deploys the stack with prefixed envs but no other parameter overrides are passed', async () => {
const inputs: Inputs = {
name: 'MockStack',
template: 'template.yaml',
capabilities: 'CAPABILITY_IAM',
'envs-prefix-for-parameter-overrides': 'CFD_',
'no-fail-on-empty-changeset': '0',
'disable-rollback': '0',
'timeout-in-minutes': '',
'notification-arns': '',
'role-arn': '',
tags: '',
'termination-protection': ''
}

jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
return inputs[name]
})

process.env = Object.assign(process.env, { CFD_AdminNickname: 'root' })

await run()

delete process.env.CFD_AdminNickname

expect(core.setFailed).toHaveBeenCalledTimes(0)
expect(mockDescribeStacks).toHaveBeenCalledTimes(2)
expect(mockDescribeStacks).toHaveBeenNthCalledWith(1, {
StackName: 'MockStack'
})
expect(mockDescribeStacks).toHaveBeenNthCalledWith(2, {
StackName: mockStackId
})
expect(mockCreateStack).toHaveBeenNthCalledWith(1, {
StackName: 'MockStack',
TemplateBody: mockTemplate,
Capabilities: ['CAPABILITY_IAM'],
Parameters: [{ ParameterKey: 'AdminNickname', ParameterValue: 'root' }],
DisableRollback: false,
EnableTerminationProtection: false
})
expect(core.setOutput).toHaveBeenCalledTimes(1)
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'stack-id', mockStackId)
})

test('error is caught by core.setFailed', async () => {
mockDescribeStacks.mockReset()
mockDescribeStacks.mockImplementation(() => {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function run() {
if (parameterOverrides) {
params.Parameters = (0, utils_1.parseParameters)(parameterOverrides.trim());
}
if (envsPrefixForParameterOverrides.length > 0) {
if (envsPrefixForParameterOverrides) {
const envParameters = (0, utils_1.parseParametersFromEnvs)(envsPrefixForParameterOverrides, process.env);
params.Parameters = params.Parameters
? [...params.Parameters, ...envParameters]
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export async function run(): Promise<void> {
params.Parameters = parseParameters(parameterOverrides.trim())
}

if (envsPrefixForParameterOverrides.length > 0) {
if (envsPrefixForParameterOverrides) {
const envParameters = parseParametersFromEnvs(
envsPrefixForParameterOverrides,
process.env
Expand Down

0 comments on commit c74a28e

Please sign in to comment.