Skip to content

Commit c164323

Browse files
committed
add unit test for ensureRepo
1 parent 9c070de commit c164323

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

packages/core/src/test/shared/sam/sync.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import assert, { fail } from 'assert'
1616
import {
1717
createEnvironmentPrompter,
1818
ensureBucket,
19+
ensureRepo,
1920
getSyncParamsFromConfig,
2021
getSyncWizard,
2122
prepareSyncParams,
@@ -65,6 +66,7 @@ import { assertTelemetry, assertTelemetryCurried } from '../../testUtil'
6566
import { PrompterTester } from '../wizards/prompterTester'
6667
import { createTestRegionProvider } from '../regions/testUtil'
6768
import { ToolkitPromptSettings } from '../../../shared/settings'
69+
import { DefaultEcrClient } from '../../../shared/clients/ecrClient'
6870

6971
describe('SyncWizard', async function () {
7072
const createTester = async (params?: Partial<SyncParams>) =>
@@ -1689,6 +1691,99 @@ describe('ensureBucket', () => {
16891691
})
16901692
})
16911693

1694+
describe('ensureRepo', () => {
1695+
let createRepositoryStub: sinon.SinonStub
1696+
let sandbox: sinon.SinonSandbox
1697+
1698+
beforeEach(() => {
1699+
sandbox = sinon.createSandbox()
1700+
createRepositoryStub = sandbox.stub()
1701+
sandbox.stub(DefaultEcrClient.prototype, 'createRepository').callsFake(createRepositoryStub)
1702+
})
1703+
1704+
afterEach(() => {
1705+
sandbox.restore()
1706+
})
1707+
1708+
const createInput = (ecrRepoUri: string | undefined) => ({
1709+
region: 'us-west-2',
1710+
ecrRepoUri,
1711+
})
1712+
1713+
const createNewRepoInput = (repoName: string) => createInput(`newrepo:${repoName}`)
1714+
1715+
describe('when not creating new repository', () => {
1716+
it('should return original ecrRepoUri when not matching newrepo pattern', async () => {
1717+
const input = createInput('existing-repo:latest')
1718+
const result = await ensureRepo(input)
1719+
1720+
assert(createRepositoryStub.notCalled)
1721+
assert.strictEqual(result, input.ecrRepoUri)
1722+
})
1723+
1724+
it('should return original ecrRepoUri when ecrRepoUri is undefined', async () => {
1725+
const input = createInput(undefined)
1726+
const result = await ensureRepo(input)
1727+
1728+
assert(!result)
1729+
assert(createRepositoryStub.notCalled)
1730+
})
1731+
})
1732+
1733+
describe('when creating new repository', () => {
1734+
const repoName = 'test-repo'
1735+
const input = createNewRepoInput(repoName)
1736+
1737+
it('should create new repository successfully', async () => {
1738+
const expectedUri = 'aws.ecr.test/test-repo'
1739+
createRepositoryStub.resolves({
1740+
repository: {
1741+
repositoryUri: expectedUri,
1742+
},
1743+
})
1744+
1745+
const result = await ensureRepo(input)
1746+
1747+
assert.strictEqual(result, expectedUri)
1748+
assert(createRepositoryStub.calledOnceWith(repoName))
1749+
})
1750+
1751+
it('should handle repository creation failure', async () => {
1752+
createRepositoryStub.rejects(new Error('Repository creation failed'))
1753+
1754+
try {
1755+
await ensureRepo(input)
1756+
assert.fail('Should have thrown an error')
1757+
} catch (err) {
1758+
assert(err instanceof ToolkitError)
1759+
assert.strictEqual(err.message, `Failed to create new ECR repository "${repoName}"`)
1760+
}
1761+
})
1762+
1763+
const testCases = [
1764+
{
1765+
name: 'undefined repositoryUri',
1766+
response: { repository: { repositoryUri: undefined } },
1767+
},
1768+
{
1769+
name: 'empty repository response',
1770+
response: {},
1771+
},
1772+
]
1773+
1774+
testCases.forEach(({ name, response }) => {
1775+
it(`should handle ${name}`, async () => {
1776+
createRepositoryStub.resolves(response)
1777+
1778+
const result = await ensureRepo(input)
1779+
1780+
assert(!result)
1781+
assert(createRepositoryStub.calledOnceWith(repoName))
1782+
})
1783+
})
1784+
})
1785+
})
1786+
16921787
const s3BucketListSummary: Array<
16931788
RequiredProps<S3.Bucket, 'Name'> & {
16941789
readonly region: string

0 commit comments

Comments
 (0)