diff --git a/src/assembly/manifest.ts b/src/assembly/manifest.ts index ba003da7..aab88a54 100644 --- a/src/assembly/manifest.ts +++ b/src/assembly/manifest.ts @@ -140,7 +140,11 @@ export class AssemblyManifestReader { let template: CloudFormationTemplate; const templateFile = path.join(this.directory, assetPath); try { - template = fs.readJSONSync(path.resolve(this.directory, templateFile)); + const resolvedPath = path.isAbsolute(this.directory) + ? path.resolve(this.directory, templateFile) + : templateFile; + + template = fs.readJSONSync(resolvedPath); } catch (e) { throw new Error(`Failed to read CloudFormation template at path: ${templateFile}: ${e}`); } diff --git a/tests/assembly/manifest.test.ts b/tests/assembly/manifest.test.ts index 2878b9c2..057201f8 100644 --- a/tests/assembly/manifest.test.ts +++ b/tests/assembly/manifest.test.ts @@ -296,7 +296,7 @@ describe('cloud assembly manifest reader', () => { expect(() => { AssemblyManifestReader.fromDirectory(path.dirname(manifestFile)); - }).toThrow(/no such file or directory * '\/tmp\/foo\/bar\/does\/not\/exist\/MyNestedStack.template.json'/); + }).toThrow(/no such file or directory* '\/tmp\/foo\/bar\/does\/not\/exist\/MyNestedStack.template.json'/); }); }); }); diff --git a/tests/cdk-resource.test.ts b/tests/cdk-resource.test.ts index f2e57f01..f76da3fd 100644 --- a/tests/cdk-resource.test.ts +++ b/tests/cdk-resource.test.ts @@ -1,4 +1,6 @@ import * as path from 'path'; +import { NestedStack } from 'aws-cdk-lib/core'; +import * as s3 from 'aws-cdk-lib/aws-s3'; import * as ecs from 'aws-cdk-lib/aws-ecs'; import * as route53 from 'aws-cdk-lib/aws-route53'; import * as events from 'aws-cdk-lib/aws-events'; @@ -10,19 +12,16 @@ import { setMocks, testApp } from './mocks'; import { MockResourceArgs } from '@pulumi/pulumi/runtime'; import { Construct } from 'constructs'; -beforeAll(() => { - process.env.AWS_REGION = 'us-east-2'; -}); -afterAll(() => { - process.env.AWS_REGION = undefined; -}); - describe('CDK Construct tests', () => { let resources: MockResourceArgs[] = []; beforeAll(() => { + process.env.AWS_REGION = 'us-east-2'; resources = []; setMocks(resources); }); + afterAll(() => { + process.env.AWS_REGION = undefined; + }); // DynamoDB table was previously mapped to the `aws` provider // otherwise this level of testing wouldn't be necessary. // We also don't need to do this type of testing for _every_ resource @@ -202,14 +201,36 @@ describe('CDK Construct tests', () => { ]), }); }); + + test('nested stack', async () => { + await testApp( + (scope: Construct) => { + const nestedStack = new NestedStack(scope, 'Nesty'); + const bucket = new s3.Bucket(nestedStack, 'bucket'); + }, + { + appOptions: { + props: { + outdir: 'cdk.out', + }, + }, + }, + ); + const nested = resources.find((res) => res.type === 'aws-native:s3:Bucket'); + expect(nested).toBeDefined(); + }); }); describe('logicalId tests', () => { let resources: MockResourceArgs[] = []; beforeEach(() => { + process.env.AWS_REGION = 'us-east-2'; resources = []; setMocks(resources); }); + afterAll(() => { + process.env.AWS_REGION = undefined; + }); test('logicalId is generated without hash for resources', async () => { await testApp((scope: Construct) => { new dynamodb.Table(scope, 'Table', { diff --git a/tests/mocks.ts b/tests/mocks.ts index b84af60b..49814005 100644 --- a/tests/mocks.ts +++ b/tests/mocks.ts @@ -8,7 +8,7 @@ import { FileAssetSource, ISynthesisSession, } from 'aws-cdk-lib/core'; -import { AppComponent, AppOptions } from '../src/types'; +import { AppComponent, AppOptions, AppResourceOptions } from '../src/types'; import { MockCallArgs, MockCallResult, MockResourceArgs } from '@pulumi/pulumi/runtime'; import { Construct } from 'constructs'; import { App, Stack } from '../src/stack'; @@ -37,11 +37,7 @@ export class MockAppComponent extends pulumi.ComponentResource implements AppCom } } -export async function testApp( - fn: (scope: Construct) => void, - options?: pulumi.ComponentResourceOptions, - withEnv?: boolean, -) { +export async function testApp(fn: (scope: Construct) => void, options?: AppResourceOptions, withEnv?: boolean) { const env = withEnv ? { account: '12345678912', region: 'us-east-1' } : undefined; class TestStack extends Stack { constructor(app: App, id: string) {