Skip to content

Commit

Permalink
Merge branch 'main' into mrgrain/chore/toolkit/do-not-depend-on-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain authored Jan 17, 2025
2 parents d5c700c + 88fe797 commit c0b404c
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 64 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/check-suite-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

name: Check Suite Logger

on:
check_suite:
types: [completed]

jobs:
log-check-suite:
runs-on: ubuntu-latest

steps:
- name: Log check suite event details
run: |
echo "Check Suite ID: ${{ github.event.check_suite.id }}"
echo "Status: ${{ github.event.check_suite.status }}"
echo "Conclusion: ${{ github.event.check_suite.conclusion }}"
echo "URL: ${{ github.event.check_suite.url }}"
echo "Head Branch: ${{ github.event.check_suite.head_branch }}"
echo "Head SHA: ${{ github.event.check_suite.head_sha }}"
echo "Repository: ${{ github.event.repository.full_name }}"
echo "Sender: ${{ github.event.sender.login }}"
echo "Created At: ${{ github.event.check_suite.created_at }}"
echo "Updated At: ${{ github.event.check_suite.updated_at }}"
echo "Pull Requests: ${{ toJson(github.event.check_suite.pull_requests) }}"
1 change: 1 addition & 0 deletions packages/@aws-cdk/toolkit/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dist
coverage
.nyc_output
*.tgz
docs

# Ignore config files
.eslintrc.js
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/toolkit/lib/actions/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DiffMethod {
*
* This will create, analyze, and subsequently delete a changeset against the CloudFormation stack.
*/
public static ChangeSet(options: ChangeSetDiffOptions = {}) {
public static ChangeSet(options: ChangeSetDiffOptions = {}): DiffMethod {
return new class extends DiffMethod {
public override readonly options: ChangeSetDiffOptions;
public constructor(opts: ChangeSetDiffOptions) {
Expand All @@ -44,7 +44,7 @@ export class DiffMethod {
}(options);
}

public static TemplateOnly(options: CloudFormationDiffOptions = {}) {
public static TemplateOnly(options: CloudFormationDiffOptions = {}): DiffMethod {
return new class extends DiffMethod {
public override readonly options: CloudFormationDiffOptions;
public constructor(opts: CloudFormationDiffOptions) {
Expand All @@ -54,7 +54,7 @@ export class DiffMethod {
}(options);
}

public static LocalFile(path: string) {
public static LocalFile(path: string): DiffMethod {
return new class extends DiffMethod {
public override readonly options: { path: string };
public constructor(opts: { path: string }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export abstract class CloudAssemblySourceBuilder {

/**
* Create a Cloud Assembly from a Cloud Assembly builder function.
* @param builder the builder function
* @param props additional configuration properties
* @returns the CloudAssembly source
*/
public async fromAssemblyBuilder(
builder: AssemblyBuilder,
Expand Down Expand Up @@ -52,9 +55,8 @@ export abstract class CloudAssemblySourceBuilder {

/**
* Creates a Cloud Assembly from an existing assembly directory.
* @param directory the directory of the AWS CDK app. Defaults to the current working directory.
* @param props additional configuration properties
* @returns an instance of `AwsCdkCli`
* @param directory the directory of a already produced Cloud Assembly.
* @returns the CloudAssembly source
*/
public async fromAssemblyDirectory(directory: string): Promise<ICloudAssemblySource> {
const services: ToolkitServices = await this.toolkitServices();
Expand All @@ -78,9 +80,8 @@ export abstract class CloudAssemblySourceBuilder {
}
/**
* Use a directory containing an AWS CDK app as source.
* @param directory the directory of the AWS CDK app. Defaults to the current working directory.
* @param props additional configuration properties
* @returns an instance of `AwsCdkCli`
* @returns the CloudAssembly source
*/
public async fromCdkApp(app: string, props: CdkAppSourceProps = {}): Promise<ICloudAssemblySource> {
const services: ToolkitServices = await this.toolkitServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export class StackAssembly extends CloudAssembly implements ICloudAssemblySource
/**
* Select all stacks that have the validateOnSynth flag et.
*
* @param assembly
* @returns a `StackCollection` of all stacks that needs to be validated
*/
public selectStacksForValidation() {
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build+extract": "yarn build",
"build+test+extract": "yarn build+test",
"bundle": "node build-tools/bundle.mjs",
"docs": "typedoc lib/index.ts --excludeExternals --excludePrivate --excludeProtected --excludeInternal",
"lint": "cdk-lint",
"package": "cdk-package",
"pkglint": "pkglint -f",
Expand Down Expand Up @@ -50,6 +51,7 @@
"aws-cdk-lib": "0.0.0",
"esbuild": "^0.24.0",
"jest": "^29.7.0",
"typedoc": "^0.27.6",
"typescript": "~5.6.3"
},
"dependencies": {
Expand Down
36 changes: 24 additions & 12 deletions packages/aws-cdk-lib/testhelpers/jest-bufferedconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TestEnvironment as NodeEnvironment } from 'jest-environment-node';

interface ConsoleMessage {
type: 'log' | 'error' | 'warn' | 'info' | 'debug';
message: string;
args: any[];
}

export default class TestEnvironment extends NodeEnvironment implements JestEnvironment<unknown> {
Expand All @@ -26,11 +26,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
// doesn't work properly.
(this as JestEnvironment<unknown>).handleTestEvent = (async (event, _state) => {
if (event.name === 'test_done' && event.test.errors.length > 0 && this.log.length > 0) {
this.stopCapture();

this.originalConsole.log(`[Console output] ${fullTestName(event.test)}\n`);
for (const item of this.log) {
this.originalConsole[item.type](' ' + item.message);
this.originalConsole[item.type].apply(this.originalConsole, [' ', ...item.args]);
}
this.originalConsole.log('\n');

this.startCapture();
}

if (event.name === 'test_done') {
Expand All @@ -43,43 +47,51 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
await super.setup();

this.log = [];
this.startCapture();
}

async teardown() {
this.stopCapture();
await super.teardown();
}

private startCapture() {
this.originalConsole = console;
this.originalStdoutWrite = process.stdout.write;
this.originalStderrWrite = process.stderr.write;

this.global.console = {
...console,
log: (message) => this.log.push({ type: 'log', message }),
error: (message) => this.log.push({ type: 'error', message }),
warn: (message) => this.log.push({ type: 'warn', message }),
info: (message) => this.log.push({ type: 'info', message }),
debug: (message) => this.log.push({ type: 'debug', message }),
log: (...args) => this.log.push({ type: 'log', args }),
error: (...args) => this.log.push({ type: 'error', args }),
warn: (...args) => this.log.push({ type: 'warn', args }),
info: (...args) => this.log.push({ type: 'info', args }),
debug: (...args) => this.log.push({ type: 'debug', args }),
};

const self = this;
process.stdout.write = function (chunk: Buffer | string, enccb?: BufferEncoding | ((error?: Error | null) => void)): void {
const encoding = typeof enccb === 'string' ? enccb : 'utf-8';
const message = Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk;
self.log.push({ type: 'log', message: message.replace(/\n$/, '') });
self.log.push({ type: 'log', args: [message.replace(/\n$/, '')] });
if (typeof enccb === 'function') {
enccb();
}
} as any;
process.stderr.write = function (chunk: Buffer | string, enccb?: BufferEncoding | ((error?: Error | null) => void)): void {
const encoding = typeof enccb === 'string' ? enccb : 'utf-8';
const message = Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk;
self.log.push({ type: 'error', message: message.replace(/\n$/, '') });
self.log.push({ type: 'error', args: [message.replace(/\n$/, '')] });
if (typeof enccb === 'function') {
enccb();
}
} as any;
}

async teardown() {
private stopCapture() {
this.global.console = this.originalConsole;
process.stdout.write = this.originalStdoutWrite;
process.stderr.write = this.originalStderrWrite;
await super.teardown();
}
}

Expand All @@ -92,7 +104,7 @@ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
function fullTestName(test: TestDescription) {
let ret = test.name;
while (test.parent != null && test.parent.name !== 'ROOT_DESCRIBE_BLOCK') {
ret = test.parent.name + ' › ' + fullTestName;
ret = test.parent.name + ' › ' + ret;
test = test.parent;
}
return ret;
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
Expand Down
36 changes: 24 additions & 12 deletions packages/aws-cdk/test/jest-bufferedconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TestEnvironment as NodeEnvironment } from 'jest-environment-node';

interface ConsoleMessage {
type: 'log' | 'error' | 'warn' | 'info' | 'debug';
message: string;
args: any[];
}

export default class TestEnvironment extends NodeEnvironment implements JestEnvironment<unknown> {
Expand All @@ -26,11 +26,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
// doesn't work properly.
(this as JestEnvironment<unknown>).handleTestEvent = (async (event, _state) => {
if (event.name === 'test_done' && event.test.errors.length > 0 && this.log.length > 0) {
this.stopCapture();

this.originalConsole.log(`[Console output] ${fullTestName(event.test)}\n`);
for (const item of this.log) {
this.originalConsole[item.type](' ' + item.message);
this.originalConsole[item.type].apply(this.originalConsole, [' ', ...item.args]);
}
this.originalConsole.log('\n');

this.startCapture();
}

if (event.name === 'test_done') {
Expand All @@ -43,43 +47,51 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
await super.setup();

this.log = [];
this.startCapture();
}

async teardown() {
this.stopCapture();
await super.teardown();
}

private startCapture() {
this.originalConsole = console;
this.originalStdoutWrite = process.stdout.write;
this.originalStderrWrite = process.stderr.write;

this.global.console = {
...console,
log: (message) => this.log.push({ type: 'log', message }),
error: (message) => this.log.push({ type: 'error', message }),
warn: (message) => this.log.push({ type: 'warn', message }),
info: (message) => this.log.push({ type: 'info', message }),
debug: (message) => this.log.push({ type: 'debug', message }),
log: (...args) => this.log.push({ type: 'log', args }),
error: (...args) => this.log.push({ type: 'error', args }),
warn: (...args) => this.log.push({ type: 'warn', args }),
info: (...args) => this.log.push({ type: 'info', args }),
debug: (...args) => this.log.push({ type: 'debug', args }),
};

const self = this;
process.stdout.write = function (chunk: Buffer | string, enccb?: BufferEncoding | ((error?: Error | null) => void)): void {
const encoding = typeof enccb === 'string' ? enccb : 'utf-8';
const message = Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk;
self.log.push({ type: 'log', message: message.replace(/\n$/, '') });
self.log.push({ type: 'log', args: [message.replace(/\n$/, '')] });
if (typeof enccb === 'function') {
enccb();
}
} as any;
process.stderr.write = function (chunk: Buffer | string, enccb?: BufferEncoding | ((error?: Error | null) => void)): void {
const encoding = typeof enccb === 'string' ? enccb : 'utf-8';
const message = Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk;
self.log.push({ type: 'error', message: message.replace(/\n$/, '') });
self.log.push({ type: 'error', args: [message.replace(/\n$/, '')] });
if (typeof enccb === 'function') {
enccb();
}
} as any;
}

async teardown() {
private stopCapture() {
this.global.console = this.originalConsole;
process.stdout.write = this.originalStdoutWrite;
process.stderr.write = this.originalStderrWrite;
await super.teardown();
}
}

Expand All @@ -92,7 +104,7 @@ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
function fullTestName(test: TestDescription) {
let ret = test.name;
while (test.parent != null && test.parent.name !== 'ROOT_DESCRIBE_BLOCK') {
ret = test.parent.name + ' › ' + fullTestName;
ret = test.parent.name + ' › ' + ret;
test = test.parent;
}
return ret;
Expand Down
Loading

0 comments on commit c0b404c

Please sign in to comment.