Skip to content

Commit

Permalink
chore: migrate all tests off compat layer and use "constructs" module (
Browse files Browse the repository at this point in the history
…#10698)

Switching all tests and test utilities to use the "constructs" module
as preparation for v2 branch. As part of CDKv2, the construct compat
layer in the "@aws-cdk/core" module will be removed.

This change pre-migrates tests to the new module so as to reduce code
divergence between the 'v1' and 'v2' code branches. This will in turn
reduce the number of merge conflicts when changes are forward merged
from the 'v1' branch onto the 'v2' branch.

This change also adds a new private tools module - eslint-plugin-cdk -
where CDK specific eslint rules can be added. The first rule now is to
ensure that tests and test related utilities do not use the construct
compat layer.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored Oct 5, 2020
1 parent c60764e commit 3cbb863
Show file tree
Hide file tree
Showing 133 changed files with 888 additions and 134 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/app-delivery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"pkglint": "pkglint -f",
"test": "cdk-test",
"watch": "cdk-watch",
"lint": "cdk-lint",
"integ": "cdk-integ",
"awslint": "cdk-awslint",
"build+test+package": "npm run build+test && npm run package",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as fc from 'fast-check';
import * as nodeunit from 'nodeunit';
import { PipelineDeployStackAction } from '../lib/pipeline-deploy-stack-action';
Expand Down Expand Up @@ -459,7 +460,7 @@ class FakeAction implements codepipeline.IAction {
this.outputArtifact = new codepipeline.Artifact('OutputArtifact');
}

public bind(_scope: cdk.Construct, _stage: codepipeline.IStage, _options: codepipeline.ActionBindOptions):
public bind(_scope: constructs.Construct, _stage: codepipeline.IStage, _options: codepipeline.ActionBindOptions):
codepipeline.ActionConfig {
return {};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/assert/test/assertions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as cdk from '@aws-cdk/core';
import * as cx from '@aws-cdk/cx-api';
import * as constructs from 'constructs';

import { countResources, countResourcesLike, exist, expect as cdkExpect, haveType, MatchStyle, matchTemplate } from '../lib/index';

Expand Down Expand Up @@ -332,7 +333,7 @@ interface TestResourceProps extends cdk.CfnResourceProps {
}

class TestResource extends cdk.CfnResource {
constructor(scope: cdk.Construct, id: string, props: TestResourceProps) {
constructor(scope: constructs.Construct, id: string, props: TestResourceProps) {
super(scope, id, props);
}
}
Expand All @@ -342,7 +343,7 @@ interface TestParameterProps extends cdk.CfnParameterProps {
}

class TestParameter extends cdk.CfnParameter {
constructor(scope: cdk.Construct, id: string, props: TestParameterProps) {
constructor(scope: constructs.Construct, id: string, props: TestParameterProps) {
super(scope, id, props);
}
}
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-amplify/test/integ.app-codecommit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as codecommit from '@aws-cdk/aws-codecommit';
import { App, Construct, Stack, StackProps } from '@aws-cdk/core';
import { App, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as amplify from '../lib';

class TestStack extends Stack {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-amplify/test/integ.app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App, Construct, Stack, StackProps } from '@aws-cdk/core';
import { App, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as amplify from '../lib';

class TestStack extends Stack {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/integ.cors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// !cdk-integ pragma:ignore-assets
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import { App, Construct, Stack, StackProps } from '@aws-cdk/core';
import { App, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as apigw from '../lib';

class TestStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Deployment, LambdaRestApi, Stage } from '../lib';

class LateBoundDeploymentStageStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App, CfnOutput, Construct, NestedStack, NestedStackProps, Stack } from '@aws-cdk/core';
import { App, CfnOutput, NestedStack, NestedStackProps, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Deployment, Method, MockIntegration, PassthroughBehavior, RestApi, Stage } from '../lib';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as apig from '../lib';

class FirstStack extends cdk.Stack {
public readonly firstLambda: lambda.Function;

constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

this.firstLambda = new lambda.Function(this, 'firstLambda', {
Expand All @@ -29,7 +30,7 @@ interface SecondStackProps extends cdk.StackProps {
}

class SecondStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
constructor(scope: constructs.Construct, id: string, props: SecondStackProps) {
super(scope, id, props);

const api = new apig.RestApi(this, 'BooksApi', {
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-applicationautoscaling/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as scalingcommon from '@aws-cdk/aws-autoscaling-common';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as fc from 'fast-check';
import * as appscaling from '../lib';

export function createScalableTarget(scope: cdk.Construct) {
export function createScalableTarget(scope: constructs.Construct) {
return new appscaling.ScalableTarget(scope, 'Target', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
scalableDimension: 'test:TestCount',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@aws-cdk/assert/jest';
import * as cdk from '@aws-cdk/core';
import * as cognito from '@aws-cdk/aws-cognito';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';
import * as t from './scalar-type-defintions';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

Expand Down Expand Up @@ -71,7 +72,7 @@ nodeunitShim({
});

class FakeNotificationTarget implements autoscaling.ILifecycleHookTarget {
public bind(_scope: cdk.Construct, lifecycleHook: autoscaling.ILifecycleHook): autoscaling.LifecycleHookTargetConfig {
public bind(_scope: constructs.Construct, lifecycleHook: autoscaling.ILifecycleHook): autoscaling.LifecycleHookTargetConfig {
lifecycleHook.role.addToPolicy(new iam.PolicyStatement({
actions: ['action:Work'],
resources: ['*'],
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/test/scaling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

Expand Down Expand Up @@ -228,7 +229,7 @@ class ASGFixture extends cdk.Construct {
public readonly vpc: ec2.Vpc;
public readonly asg: autoscaling.AutoScalingGroup;

constructor(scope: cdk.Construct, id: string) {
constructor(scope: constructs.Construct, id: string) {
super(scope, id);

this.vpc = new ec2.Vpc(this, 'VPC');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, haveResource, MatchStyle } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

Expand Down Expand Up @@ -107,7 +108,7 @@ nodeunitShim({
},
});

function makeAutoScalingGroup(scope: cdk.Construct) {
function makeAutoScalingGroup(scope: constructs.Construct) {
const vpc = new ec2.Vpc(scope, 'VPC');
return new autoscaling.AutoScalingGroup(scope, 'ASG', {
vpc,
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-backup/test/integ.backup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as efs from '@aws-cdk/aws-efs';
import { App, Construct, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core';
import { App, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as backup from '../lib';

class TestStack extends Stack {
Expand Down
11 changes: 8 additions & 3 deletions packages/@aws-cdk/aws-backup/test/selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import '@aws-cdk/assert/jest';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as efs from '@aws-cdk/aws-efs';
import { Construct, Stack } from '@aws-cdk/core';
import { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { BackupPlan, BackupResource, BackupSelection } from '../lib';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '@aws-cdk/core';

let stack: Stack;
let plan: BackupPlan;
beforeEach(() => {
Expand Down Expand Up @@ -120,13 +125,13 @@ test('allow restores', () => {

test('fromConstruct', () => {
// GIVEN
class EfsConstruct extends Construct {
class EfsConstruct extends CoreConstruct {
constructor(scope: Construct, id: string) {
super(scope, id);
new efs.CfnFileSystem(this, 'FileSystem');
}
}
class MyConstruct extends Construct {
class MyConstruct extends CoreConstruct {
constructor(scope: Construct, id: string) {
super(scope, id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as route53 from '@aws-cdk/aws-route53';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as certmgr from '../lib';

class CertStack extends Stack {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloud9/test/integ.cloud9.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as cloud9 from '../lib';

export class Cloud9Env extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const vpc = new ec2.Vpc(this, 'VPC', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
import { App, CfnOutput, Construct, CustomResource, CustomResourceProvider, CustomResourceProviderRuntime, Stack, Token } from '@aws-cdk/core';

/* eslint-disable cdk/no-core-construct */

class TestStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as sqs from '@aws-cdk/aws-sqs';
import { App, CfnParameter, Construct, Stack } from '@aws-cdk/core';
import * as cfn from '../lib';

/* eslint-disable cdk/no-core-construct */

interface MyNestedStackProps {
readonly subscriber?: sqs.Queue;
readonly siblingTopic?: sns.Topic; // a topic defined in a sibling nested stack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as lambda from '@aws-cdk/aws-lambda';
import { App, Construct, Stack } from '@aws-cdk/core';
import * as cfn from '../lib';

/* eslint-disable cdk/no-core-construct */

class NestedStack extends cfn.NestedStack {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as sns from '@aws-cdk/aws-sns';
import { App, Construct, Stack } from '@aws-cdk/core';
import * as cfn from '../lib';

/* eslint-disable cdk/no-core-construct */

class YourNestedStack extends cfn.NestedStack {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// nested stack references a resource from a non-nested non-parent stack

/* eslint-disable cdk/no-core-construct */

import * as sns from '@aws-cdk/aws-sns';
import { App, Construct, Stack } from '@aws-cdk/core';
import * as cfn from '../lib';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as cfn from '../lib';

// non-nested non-parent stack consumes a resource from a nested stack

/* eslint-disable cdk/no-core-construct */

class ProducerNestedStack extends cfn.NestedStack {
public readonly topic: sns.Topic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as cfn from '../lib';

// references between siblings

/* eslint-disable cdk/no-core-construct */

class ProducerNestedStack extends cfn.NestedStack {
public readonly topic: sns.Topic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { CustomResource, CustomResourceProvider } from '../lib';

/* eslint-disable cdk/no-core-construct */

interface DemoResourceProps {
/**
* Message to echo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { App, CfnParameter, CfnResource, Construct, ContextProvider, Stack } fro
import { Test } from 'nodeunit';
import { NestedStack } from '../lib/nested-stack';

/* eslint-disable cdk/no-core-construct */
/* eslint-disable max-len */

export = {
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudformation/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cdk from '@aws-cdk/core';
import { Test, testCase } from 'nodeunit';
import { CustomResource, CustomResourceProvider } from '../lib';

/* eslint-disable cdk/no-core-construct */
/* eslint-disable quote-props */

export = testCase({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as certificatemanager from '@aws-cdk/aws-certificatemanager';
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cloudfront from '../lib';

class AcmCertificateAliasStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cloudfront from '../lib';

class AcmCertificateAliasStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cloudfront from '../lib';

class AcmCertificateAliasStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack } from '@aws-cdk/core';
import { App, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cloudfront from '../lib';

class TestStack extends Stack {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/test/test.alarm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ABSENT, expect, haveResource } from '@aws-cdk/assert';
import { Construct, Duration, Stack } from '@aws-cdk/core';
import { Duration, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Test } from 'nodeunit';
import { Alarm, IAlarm, IAlarmAction, Metric } from '../lib';

Expand Down
Loading

0 comments on commit 3cbb863

Please sign in to comment.