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

Commit

Permalink
Feature/improve mira test coverage (#55)
Browse files Browse the repository at this point in the history
* Improve mira test coverage

* Transpiler test, add extra conditions depending on platform

* Validators test

* Removing unnecessary logs
  • Loading branch information
thiagokroger authored Jul 27, 2020
1 parent 3b0855b commit 5ca7c35
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 5 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()
})
})
6 changes: 6 additions & 0 deletions src/cdk/constructs/config/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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
1 change: 0 additions & 1 deletion src/cdk/constructs/config/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export async function isValidGitBranchName (input: string): Promise<unknown> {

execFile('git', ['check-ref-format', '--branch', input], (error) => {
if (error?.code) {
console.log(`\nNot a valid branch name: ${input}`)
return resolve(false)
}
resolve(true)
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 @@ -24,7 +24,6 @@ interface Properties {
export const handler = async (
event: CustomResourceProviderRequest<Properties>
): Promise<void> => {
console.log(JSON.stringify(event, null, 2))
const { RequestType, ResourceProperties: { BucketName } = {} } = event

let status = 'SUCCESS'
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 === -13 || err.errno === -1).toBe(true)
expect(err.code === 'EACCES' || err.code === 'EPERM').toBe(true)
}
})

0 comments on commit 5ca7c35

Please sign in to comment.