Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Stacks render CloudFormation elements in nested Stages #10156

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export class Stack extends Construct implements ITaggable {
return c;
}

if (!c.node.scope) {
throw new Error(`No stack could be identified for the construct at path ${construct.node.path}`);
if (Stage.isStage(c) || !c.node.scope) {
throw new Error(`${construct.constructor?.name ?? 'Construct'} at '${construct.node.path}' should be created in the scope of a Stack, but no Stack found`);
}

return _lookup(c.node.scope);
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/core/test/test.cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ export = nodeunit.testCase({
// No DependsOn!
});

test.done();
},

'CfnResource cannot be created outside Stack'(test: nodeunit.Test) {
const app = new core.App();
test.throws(() => {
new core.CfnResource(app, 'Resource', {
type: 'Some::Resource',
});
}, /should be created in the scope of a Stack, but no Stack found/);


test.done();
},

/**
* Stages start a new scope, which does not count as a Stack anymore
*/
'CfnResource cannot be in Stage in Stack'(test: nodeunit.Test) {
const app = new core.App();
const stack = new core.Stack(app, 'Stack');
const stage = new core.Stage(stack, 'Stage');
test.throws(() => {
new core.CfnResource(stage, 'Resource', {
type: 'Some::Resource',
});
}, /should be created in the scope of a Stack, but no Stack found/);


test.done();
},
});
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/test/test.stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ export = {
'Stack.of() throws when there is no parent Stack'(test: Test) {
const root = new Construct(undefined as any, 'Root');
const construct = new Construct(root, 'Construct');
test.throws(() => Stack.of(construct), /No stack could be identified for the construct at path/);
test.throws(() => Stack.of(construct), /should be created in the scope of a Stack, but no Stack found/);
test.done();
},

Expand Down