|
| 1 | +/** |
| 2 | + * MIT No Attribution |
| 3 | + * |
| 4 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | + * the Software, and to permit persons to whom the Software is furnished to do so. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | + * SOFTWARE. |
| 18 | + */ |
| 19 | + |
| 20 | +// Disable Lambda asset bundling during unit-test synthesis. |
| 21 | +// |
| 22 | +// `Template.fromStack()` triggers a full CDK synth, which bundles every |
| 23 | +// NodejsFunction via esbuild — ~28s for the AgentStack (413 resources). Unit |
| 24 | +// tests assert on CloudFormation structure/properties, not bundled Lambda code, |
| 25 | +// so for those assertions bundling is overhead — plus a smoke-test of Lambda |
| 26 | +// `entry` resolution and handler compilation that unit tests intentionally cede |
| 27 | +// to the `agent/` build (real synth/deploy still bundles). Skipping it cuts a |
| 28 | +// single synth ~15× (~28.7s -> ~1.9s). See #366. |
| 29 | +// |
| 30 | +// `aws:cdk:bundling-stacks: []` tells the CLI/synth to bundle no stacks. CDK |
| 31 | +// reads CDK_CONTEXT_JSON when an `App` is constructed, so a bare `new App()` |
| 32 | +// (the overwhelming-majority pattern in our tests) picks this up with no |
| 33 | +// call-site changes. |
| 34 | +// |
| 35 | +// Precedence matters for the opt-out. CDK's `App.loadContext(props.context, |
| 36 | +// props.postCliContext)` applies `props.context` FIRST, then overwrites it with |
| 37 | +// CDK_CONTEXT_JSON, then applies `postCliContext` LAST. So for this key the env |
| 38 | +// var beats constructor `context` — `new App({ context: { 'aws:cdk:bundling- |
| 39 | +// stacks': ['**'] } })` does NOT re-enable bundling (the env var clobbers it). |
| 40 | +// |
| 41 | +// This does NOT stop tests from synthesizing — `Template.fromStack()` still |
| 42 | +// runs a full synth; it only skips the esbuild asset-bundling step within that |
| 43 | +// synth. The opt-out below exists solely for the rare test that needs the |
| 44 | +// *bundled-asset output itself* (e.g. asserting on a real asset hash / S3 key), |
| 45 | +// where an unbundled synth would silently yield a placeholder value. Such a |
| 46 | +// test must opt out via `postCliContext` (which wins over the env var), |
| 47 | +// constructing its `App` with |
| 48 | +// `new App({ postCliContext: { 'aws:cdk:bundling-stacks': ['**'] } })` |
| 49 | +// (or the specific stack id). |
| 50 | +const BUNDLING_DISABLED_CONTEXT = { 'aws:cdk:bundling-stacks': [] as string[] }; |
| 51 | + |
| 52 | +const existing = process.env.CDK_CONTEXT_JSON; |
| 53 | +if (existing) { |
| 54 | + // Preserve any ambient context, but let the bundling disable win |
| 55 | + // unconditionally: a pre-set CDK_CONTEXT_JSON that carries |
| 56 | + // `aws:cdk:bundling-stacks` must not silently re-enable bundling and defeat |
| 57 | + // the speedup. The documented per-test opt-out is `postCliContext` (applied |
| 58 | + // AFTER the env var regardless — see above), so spreading our key LAST keeps |
| 59 | + // that escape hatch working while making the global disable robust to |
| 60 | + // whatever the environment already set. |
| 61 | + let ambient: Record<string, unknown>; |
| 62 | + try { |
| 63 | + ambient = JSON.parse(existing); |
| 64 | + } catch { |
| 65 | + // Without this guard a malformed pre-set var throws here, inside |
| 66 | + // `setupFiles`, and fails EVERY test in EVERY file with an opaque parse |
| 67 | + // error. Re-throw with the offending value so the blame is localized. |
| 68 | + throw new Error(`malformed CDK_CONTEXT_JSON in environment (must be valid JSON): ${existing}`); |
| 69 | + } |
| 70 | + const merged = { ...ambient, ...BUNDLING_DISABLED_CONTEXT }; |
| 71 | + process.env.CDK_CONTEXT_JSON = JSON.stringify(merged); |
| 72 | +} else { |
| 73 | + process.env.CDK_CONTEXT_JSON = JSON.stringify(BUNDLING_DISABLED_CONTEXT); |
| 74 | +} |
0 commit comments