Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corymhall committed Dec 30, 2024
1 parent 8160c02 commit 5a9e7c9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/assembly/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/assembly/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'/);
});
});
});
35 changes: 28 additions & 7 deletions tests/cdk-resource.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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', {
Expand Down
8 changes: 2 additions & 6 deletions tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5a9e7c9

Please sign in to comment.