Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Improve mira test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokroger committed Jul 21, 2020
1 parent 25f4423 commit ebeb4fe
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/cdk/constructs/config/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ describe('autocomplete', () => {
expect(res.length > 0).toBeTruthy()
})
})

describe('autocomplete with empty input', () => {
it('buildSearchRegions should return a promise', async () => {
const fn = buildSearchRegions()
const res = await fn({}, '')
expect(Array.isArray(res)).toBeTruthy()
expect(res.length > 0).toBeTruthy()
})
})
14 changes: 14 additions & 0 deletions src/cdk/constructs/config/validators.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as validators from './validators'
import { execFile } from 'child_process'

describe('Validators', () => {
it('isValidAwsAccountId', async () => {
Expand All @@ -21,6 +22,13 @@ describe('Validators', () => {
expect(validators.isValidAwsAccountIdList('1234567890123,000000000000')).toBeFalsy()
})

it('isValidAwsCliProfile', async () => {
expect(await validators.isValidAwsCliProfile('')).toBeFalsy()
expect(await validators.isValidAwsCliProfile('value')).toBeFalsy()
await execFile('aws', ['configure', 'set', 'region', 'eu-west-1', '--profile', 'user1'])
expect(await validators.isValidAwsCliProfile('user1')).toBeTruthy()
})

it('isValidAwsHostedZoneId', async () => {
expect(validators.isValidAwsHostedZoneId('ZABCDEF1234567')).toBeTruthy()

Expand Down Expand Up @@ -51,6 +59,12 @@ describe('Validators', () => {
expect(validators.isValidDomain('a.c')).toBeFalsy()
})

it('isValidGitBranchName', async () => {
expect(await validators.isValidGitBranchName('')).toBeFalsy()
expect(await validators.isValidGitBranchName(' ')).toBeFalsy()
expect(await validators.isValidGitBranchName('value')).toBeTruthy()
})

it('isValidEnvironmentNameList', async () => {
expect(validators.isValidEnvironmentNameList('ab')).toBeTruthy()
expect(validators.isValidEnvironmentNameList('a1')).toBeTruthy()
Expand Down
20 changes: 20 additions & 0 deletions src/cdk/custom-resources/auto-delete-bucket/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ describe('auto-delete bucket lambda', () => {
mockSendResponse.mockClear()
})

it('fails if ResourceProperties is not provided no matter the request type', async () => {
await handler({
...eventBase,
RequestType: 'Create',
ResourceProperties: undefined
})

expect(mockDeleteBucket).not.toHaveBeenCalled()
expect(mockSendResponse).toHaveBeenCalledTimes(1)
expect(mockSendResponse).toHaveBeenCalledWith('ResponseURL', {
status: 'FAILED',
reason: 'BucketName is required',
physicalResourceId: 'LogicalResourceId',
stackId: 'StackId',
requestId: 'RequestId',
logicalResourceId: 'LogicalResourceId',
data: undefined
})
})

it('fails if bucket name is not provided no matter the request type', async () => {
await handler({
...eventBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,39 @@ describe('delete bucket', () => {
}
})
})

it('deletes all objects from the bucket without Versions and DeleteMarkers value without Key', async () => {
mockListObjectVersionsPromise.mockResolvedValue({
IsTruncated: false,
DeleteMarkers: [{ VersionId: 'v3', RandomKey: 'test3' }]
})

await deleteBucket('testBucket')

expect(mockListObjectVersions).toBeCalledTimes(1)
expect(mockListObjectVersions).toBeCalledWith({ Bucket: 'testBucket' })

expect(mockDeleteObjects).toBeCalledTimes(1)
expect(mockDeleteObjects).toBeCalledWith({
Bucket: 'testBucket',
Delete: {
Objects: [
{ Key: '', VersionId: 'v3' }
]
}
})
})

it('deletes all objects from the bucket without Versions and DeleteMarkers', async () => {
mockListObjectVersionsPromise.mockResolvedValue({
IsTruncated: false
})

await deleteBucket('testBucket')

expect(mockListObjectVersions).toBeCalledTimes(1)
expect(mockListObjectVersions).toBeCalledWith({ Bucket: 'testBucket' })

expect(mockDeleteObjects).toBeCalledTimes(0)
})
})
2 changes: 2 additions & 0 deletions src/config/job-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ describe('MiraJobConfig', () => {
mockConfigHandler(healthyConfig)
expect(() => new MiraJobConfig('deploy')).toThrowError('Cannot deploy environment undefined')

expect(() => new MiraJobConfig('')).toThrowError('Cannot resolve deployment type')

let brokenConfig

brokenConfig = _.cloneDeep(healthyConfig)
Expand Down
3 changes: 0 additions & 3 deletions src/config/job-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ export default class MiraConfig {
const envData: EnvData = loadEnvironment(envKey)
return massageEnvData(envData, false)
})
if (!config.get('cicd.buildspec_file')) {
throw new Error('Missing config.cicd.buildspec_file')
}
const details: CICDDetails = {
stackName: deploymentStackName,
role: loadAWSProfile(config.get('cicd.profile')),
Expand Down
25 changes: 25 additions & 0 deletions src/transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,28 @@ test('should change extension to file', () => {
const res = t.changeExtension('foo')
expect(res).toBe('sampleFile.foo')
})

test('should return null when passing the wrong path', async () => {
const t = new Transpiler('sampleFile.ts')
const root = __dirname
const res = await t.findTSConfigFile(root)
expect(res).toBe(null)
})

test('should return null when passing the wrong path', async () => {
const t = new Transpiler('sampleFile.ts')
const root = __dirname
const res = await t.findTSConfigFile(root)
expect(res).toBe(null)
})

test('should throw access error when passing empty path', async () => {
console.error = jest.fn()
const t = new Transpiler('sampleFile.ts')
try {
await t.findTSConfigFile(' ')
} catch (err) {
expect(err.errno).toBe(-13)
expect(err.code).toBe('EACCES')
}
})

0 comments on commit ebeb4fe

Please sign in to comment.