diff --git a/packages/@aws-cdk/core/test/test.app.ts b/packages/@aws-cdk/core/test/test.app.ts index af7a301f5b95b..eef7f2db88d33 100644 --- a/packages/@aws-cdk/core/test/test.app.ts +++ b/packages/@aws-cdk/core/test/test.app.ts @@ -325,6 +325,32 @@ export = { test.done(); }, + + 'stacks are written to the assembly file in a topological order'(test: Test) { + // WHEN + const assembly = withApp({}, (app) => { + const stackC = new Stack(app, 'StackC'); + const stackD = new Stack(app, 'StackD'); + const stackA = new Stack(app, 'StackA'); + const stackB = new Stack(app, 'StackB'); + + // Create the following dependency order: + // A -> + // C -> D + // B -> + stackC.addDependency(stackA); + stackC.addDependency(stackB); + stackD.addDependency(stackC); + }); + + // THEN + const artifactsIds = assembly.artifacts.map(a => a.id); + test.ok(artifactsIds.indexOf('StackA') < artifactsIds.indexOf('StackC')); + test.ok(artifactsIds.indexOf('StackB') < artifactsIds.indexOf('StackC')); + test.ok(artifactsIds.indexOf('StackC') < artifactsIds.indexOf('StackD')); + + test.done(); + }, }; class MyConstruct extends Construct { diff --git a/packages/@aws-cdk/cx-api/test/cloud-assembly-builder.test.ts b/packages/@aws-cdk/cx-api/test/cloud-assembly-builder.test.ts index 1512c86ff5044..c3ae0aa6609ea 100644 --- a/packages/@aws-cdk/cx-api/test/cloud-assembly-builder.test.ts +++ b/packages/@aws-cdk/cx-api/test/cloud-assembly-builder.test.ts @@ -166,4 +166,62 @@ test('write and read nested cloud assembly artifact', () => { const nested = art?.nestedAssembly; expect(nested?.artifacts.length).toEqual(0); +}); + +test('artifcats are written in topological order', () => { + // GIVEN + const outdir = fs.mkdtempSync(path.join(os.tmpdir(), 'cloud-assembly-builder-tests')); + const session = new cxapi.CloudAssemblyBuilder(outdir); + const templateFile = 'foo.template.json'; + + const innerAsmDir = path.join(outdir, 'hello'); + new cxapi.CloudAssemblyBuilder(innerAsmDir).buildAssembly(); + + // WHEN + + // Create the following dependency order: + // A -> + // C -> D + // B -> + session.addArtifact('artifact-D', { + type: cxschema.ArtifactType.AWS_CLOUDFORMATION_STACK, + environment: 'aws://1222344/us-east-1', + dependencies: ['artifact-C'], + properties: { + templateFile, + }, + }); + + session.addArtifact('artifact-C', { + type: cxschema.ArtifactType.AWS_CLOUDFORMATION_STACK, + environment: 'aws://1222344/us-east-1', + dependencies: ['artifact-B', 'artifact-A'], + properties: { + templateFile, + }, + }); + + session.addArtifact('artifact-B', { + type: cxschema.ArtifactType.AWS_CLOUDFORMATION_STACK, + environment: 'aws://1222344/us-east-1', + properties: { + templateFile, + }, + }); + + session.addArtifact('artifact-A', { + type: cxschema.ArtifactType.AWS_CLOUDFORMATION_STACK, + environment: 'aws://1222344/us-east-1', + properties: { + templateFile, + }, + }); + + const asm = session.buildAssembly(); + const artifactsIds = asm.artifacts.map(a => a.id); + + // THEN + expect(artifactsIds.indexOf('artifact-A')).toBeLessThan(artifactsIds.indexOf('artifact-C')); + expect(artifactsIds.indexOf('artifact-B')).toBeLessThan(artifactsIds.indexOf('artifact-C')); + expect(artifactsIds.indexOf('artifact-C')).toBeLessThan(artifactsIds.indexOf('artifact-D')); }); \ No newline at end of file diff --git a/packages/@aws-cdk/cx-api/test/cloud-assembly.test.ts b/packages/@aws-cdk/cx-api/test/cloud-assembly.test.ts index 42d7dffd94233..f3ee5f817caa6 100644 --- a/packages/@aws-cdk/cx-api/test/cloud-assembly.test.ts +++ b/packages/@aws-cdk/cx-api/test/cloud-assembly.test.ts @@ -85,7 +85,7 @@ test('assets', () => { test('can-read-0.36.0', () => { // WHEN new CloudAssembly(path.join(FIXTURES, 'single-stack-0.36')); - // THEN: no eexception + // THEN: no exception expect(true).toBeTruthy(); });