-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): facility to warn when using deprecated apis
Introduce `Annotations.addDeprecation()` which will attach a warning to the construct indicating that a deprecated API is used. At the moment, we only use this to warn when `.node` is used instead of `.construct`, but we will gradually use this to report the usage of all deprecated APIs as a preparation for v2.0. If the environment variable `CDK_BLOCK_DEPRECATIONS` is set (and it is set in `cdk-test`), it will cause usage of deprecated APIs to throw an error instead. Related: aws/aws-cdk-rfcs#192
- Loading branch information
Elad Ben-Israel
committed
Aug 11, 2020
1 parent
1fe9684
commit 006954d
Showing
4 changed files
with
158 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { CloudAssembly } from '@aws-cdk/cx-api'; | ||
import { Test } from 'nodeunit'; | ||
import { Construct, App, Stack } from '../lib'; | ||
import { Annotations } from '../lib/annotations'; | ||
|
||
const restore = process.env.CDK_BLOCK_DEPRECATIONS; | ||
|
||
export = { | ||
'tearDown'(cb: any) { | ||
process.env.CDK_BLOCK_DEPRECATIONS = restore; // restore to the original value | ||
cb(); | ||
}, | ||
|
||
'addDeprecation() annotates the usage of a deprecated API'(test: Test) { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app, 'MyStack'); | ||
const c1 = new Construct(stack, 'Hello'); | ||
|
||
// WHEN | ||
delete process.env.CDK_BLOCK_DEPRECATIONS; | ||
Annotations.of(c1).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
|
||
// THEN | ||
test.deepEqual(getWarnings(app.synth()), [ | ||
{ | ||
path: '/MyStack/Hello', | ||
message: 'The API @aws-cdk/core.Construct.node is deprecated: use @aws-cdk.Construct.construct instead. This API should will be removed in the next major version of the AWS CDK', | ||
}, | ||
]); | ||
test.done(); | ||
}, | ||
|
||
'deduplicated per node based on "api"'(test: Test) { | ||
// GIVEN | ||
const app = new App(); | ||
const stack1 = new Stack(app, 'MyStack1'); | ||
const stack2 = new Stack(app, 'MyStack2'); | ||
const c1 = new Construct(stack1, 'Hello'); | ||
const c2 = new Construct(stack1, 'World'); | ||
const c3 = new Construct(stack2, 'FooBar'); | ||
|
||
// WHEN | ||
delete process.env.CDK_BLOCK_DEPRECATIONS; | ||
Annotations.of(c1).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
Annotations.of(c2).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
Annotations.of(c1).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
Annotations.of(c3).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
Annotations.of(c1).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
Annotations.of(c1).addDeprecation('@aws-cdk/core.Construct.node', 'use @aws-cdk.Construct.construct instead'); | ||
|
||
// THEN | ||
test.deepEqual(getWarnings(app.synth()), [ | ||
{ | ||
path: '/MyStack1/Hello', | ||
message: 'The API @aws-cdk/core.Construct.node is deprecated: use @aws-cdk.Construct.construct instead. This API should will be removed in the next major version of the AWS CDK', | ||
}, | ||
{ | ||
path: '/MyStack1/World', | ||
message: 'The API @aws-cdk/core.Construct.node is deprecated: use @aws-cdk.Construct.construct instead. This API should will be removed in the next major version of the AWS CDK', | ||
}, | ||
{ | ||
path: '/MyStack2/FooBar', | ||
message: 'The API @aws-cdk/core.Construct.node is deprecated: use @aws-cdk.Construct.construct instead. This API should will be removed in the next major version of the AWS CDK', | ||
}, | ||
]); | ||
test.done(); | ||
}, | ||
|
||
'CDK_BLOCK_DEPRECATIONS will throw if a deprecated API is used'(test: Test) { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app, 'MyStack'); | ||
const c1 = new Construct(stack, 'Hello'); | ||
|
||
// THEN | ||
process.env.CDK_BLOCK_DEPRECATIONS = '1'; | ||
test.throws(() => Annotations.of(c1).addDeprecation('foo', 'bar'), /MyStack\/Hello: The API foo is deprecated: bar\. This API should will be removed in the next major version of the AWS CDK/); | ||
test.done(); | ||
}, | ||
}; | ||
|
||
function getWarnings(casm: CloudAssembly) { | ||
const result = new Array<{ path: string, message: string }>(); | ||
for (const stack of Object.values(casm.manifest.artifacts ?? {})) { | ||
for (const [path, md] of Object.entries(stack.metadata ?? {})) { | ||
for (const x of md) { | ||
if (x.type === 'aws:cdk:warning') { | ||
result.push({ path, message: x.data as string }); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters