Skip to content

Commit e1a9030

Browse files
authored
chore(cli): lint test files (#441)
These were previously ignored, presumingly because they did not follow most of our linting rules. However now a lot less tests are in the CLI package (most moved to toolkit-lib) so we can safely re-enable linting of test files. Added `'@typescript-eslint/unbound-method': 'off'` for test files, since there were many violations of this and it doesn't seem necessary to enforce this rule in test code. Most test code changes are automated. I've annotated the few manual fixes I made. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 302f9bc commit e1a9030

22 files changed

+101
-87
lines changed

.projenrc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,8 @@ const cli = configureProject(
10021002
'@types/sinon',
10031003
'@types/yargs@^15',
10041004
'aws-cdk-lib',
1005+
'aws-sdk-client-mock',
1006+
'aws-sdk-client-mock-jest',
10051007
'axios',
10061008
'constructs',
10071009
'fast-check',
@@ -1097,7 +1099,7 @@ const cli = configureProject(
10971099
},
10981100
eslintOptions: {
10991101
dirs: ['lib'],
1100-
ignorePatterns: ['*.template.ts', '*.d.ts', 'test/**/*.ts'],
1102+
ignorePatterns: ['*.template.ts', '*.d.ts'],
11011103
},
11021104
jestOptions: jestOptionsForProject({
11031105
jestConfig: {
@@ -1144,6 +1146,7 @@ cli.eslint?.addOverride({
11441146
files: ['./test/**'],
11451147
rules: {
11461148
'@cdklabs/no-throw-default-error': 'off',
1149+
'@typescript-eslint/unbound-method': 'off',
11471150
},
11481151
});
11491152

packages/aws-cdk/.eslintrc.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/.projen/deps.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/package.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/test/_helpers/assembly.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as path from 'path';
33
import { ArtifactMetadataEntryType, ArtifactType, type AssetManifest, type AssetMetadataEntry, type AwsCloudFormationStackProperties, type MetadataEntry, type MissingContext } from '@aws-cdk/cloud-assembly-schema';
44
import { type CloudAssembly, CloudAssemblyBuilder, type CloudFormationStackArtifact, type StackMetadata } from '@aws-cdk/cx-api';
55
import { cxapiAssemblyWithForcedVersion } from './assembly-versions';
6-
import { MockSdkProvider } from '../_helpers/mock-sdk';
7-
import { CloudExecutable } from '../../lib/cxapp/cloud-executable';
8-
import { Configuration } from '../../lib/cli/user-configuration';
96
import { TestIoHost } from './io-host';
10-
import { IIoHost } from '../../lib/cli/io-host';
117
import { asIoHelper } from '../../../@aws-cdk/toolkit-lib/lib/api/io/private';
8+
import type { IIoHost } from '../../lib/cli/io-host';
9+
import { Configuration } from '../../lib/cli/user-configuration';
10+
import { CloudExecutable } from '../../lib/cxapp/cloud-executable';
11+
import { MockSdkProvider } from '../_helpers/mock-sdk';
1212

1313
export const DEFAULT_FAKE_TEMPLATE = { No: 'Resources' };
1414

@@ -47,7 +47,7 @@ export class MockCloudExecutable extends CloudExecutable {
4747
const configuration = new Configuration();
4848
const sdkProvider = sdkProviderArg ?? new MockSdkProvider();
4949
const mockIoHost = ioHost ?? new TestIoHost();
50-
50+
5151
super({
5252
configuration,
5353
sdkProvider,

packages/aws-cdk/test/_helpers/fake-io-host.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IIoHost, IoMessage, IoMessageLevel, IoRequest } from "../../../@aws-cdk/toolkit-lib/lib/api/io";
1+
import type { IIoHost, IoMessage, IoMessageLevel, IoRequest } from '../../../@aws-cdk/toolkit-lib/lib/api/io';
22

33
/**
44
* An implementation of `IIoHost` that records messages and lets you assert on what was logged

packages/aws-cdk/test/_helpers/jest-bufferedconsole.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* A Jest environment that buffers outputs to `console.log()` and only shows it for failing tests.
44
*/
55
import type { EnvironmentContext, JestEnvironment, JestEnvironmentConfig } from '@jest/environment';
6-
import { Circus } from '@jest/types';
6+
import type { Circus } from '@jest/types';
77
import { TestEnvironment as NodeEnvironment } from 'jest-environment-node';
88

99
interface ConsoleMessage {

packages/aws-cdk/test/_helpers/jest-setup-after-env.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ afterAll(async () => {
6363
await new Promise<void>((resolve, reject) => {
6464
const response = aft(resolve as any);
6565
if (isPromise(response)) {
66-
response.then(() => { return resolve(); }, reject);
66+
response.then(() => {
67+
return resolve();
68+
}, reject);
6769
} else {
6870
resolve();
6971
}

packages/aws-cdk/test/_helpers/mock-sdk.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'aws-sdk-client-mock-jest';
2-
import { Environment } from '@aws-cdk/cx-api';
2+
import type { Environment } from '@aws-cdk/cx-api';
33
import { AppSyncClient } from '@aws-sdk/client-appsync';
44
import { CloudControlClient } from '@aws-sdk/client-cloudcontrol';
5-
import { CloudFormationClient, Stack, StackStatus } from '@aws-sdk/client-cloudformation';
5+
import type { Stack } from '@aws-sdk/client-cloudformation';
6+
import { CloudFormationClient, StackStatus } from '@aws-sdk/client-cloudformation';
67
import { CloudWatchLogsClient } from '@aws-sdk/client-cloudwatch-logs';
78
import { CodeBuildClient } from '@aws-sdk/client-codebuild';
89
import { EC2Client } from '@aws-sdk/client-ec2';
@@ -19,12 +20,12 @@ import { SFNClient } from '@aws-sdk/client-sfn';
1920
import { SSMClient } from '@aws-sdk/client-ssm';
2021
import { AssumeRoleCommand, GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
2122
import { createCredentialChain } from '@aws-sdk/credential-providers';
22-
import { AwsCredentialIdentity } from '@smithy/types';
23+
import type { AwsCredentialIdentity } from '@smithy/types';
2324
import { mockClient } from 'aws-sdk-client-mock';
2425
import { type Account } from 'cdk-assets';
26+
import { TestIoHost } from './io-host';
2527
import { SDK, SdkProvider } from '../../lib/api/aws-auth';
2628
import { CloudFormationStack } from '../../lib/api/cloudformation';
27-
import { TestIoHost } from './io-host';
2829

2930
export const FAKE_CREDENTIALS: AwsCredentialIdentity = {
3031
accessKeyId: 'ACCESS',
@@ -98,7 +99,7 @@ export const restoreSdkMocksToDefault = () => {
9899
*/
99100
export function undoAllSdkMocks() {
100101
applyToAllMocks('restore');
101-
};
102+
}
102103

103104
function applyToAllMocks(meth: 'reset' | 'restore') {
104105
mockAppSyncClient[meth]();

packages/aws-cdk/test/_helpers/mock-toolkitinfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/order */
22
import { ToolkitInfo, DEFAULT_BOOTSTRAP_VARIANT } from '../../lib/api';
3-
import { CloudFormationStack } from '../../lib/api/cloudformation';
3+
import type { CloudFormationStack } from '../../lib/api/cloudformation';
44

55
export interface MockToolkitInfoProps {
66
readonly bucketName?: string;

packages/aws-cdk/test/cli/cdk-toolkit.test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const fakeChokidarWatcherOn = {
1818
},
1919

2020
get fileEventCallback(): (
21-
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
22-
path: string,
21+
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
22+
path: string,
2323
) => Promise<void> {
2424
expect(mockChokidarWatcherOn.mock.calls.length).toBeGreaterThanOrEqual(2);
2525
const secondCall = mockChokidarWatcherOn.mock.calls[1];
@@ -60,46 +60,49 @@ jest.setTimeout(30_000);
6060
import 'aws-sdk-client-mock';
6161
import * as os from 'os';
6262
import * as path from 'path';
63-
import * as cdkAssets from 'cdk-assets';
6463
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
6564
import { Manifest } from '@aws-cdk/cloud-assembly-schema';
6665
import * as cxapi from '@aws-cdk/cx-api';
66+
import type { DestroyStackResult } from '@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack';
6767
import { DescribeStacksCommand, GetTemplateCommand, StackStatus } from '@aws-sdk/client-cloudformation';
6868
import { GetParameterCommand } from '@aws-sdk/client-ssm';
69+
import * as cdkAssets from 'cdk-assets';
6970
import * as fs from 'fs-extra';
7071
import * as promptly from 'promptly';
71-
import { SdkProvider } from '../../lib/api';
72+
import type { Template } from '../../../@aws-cdk/toolkit-lib/lib/api';
73+
import { asIoHelper } from '../../../@aws-cdk/toolkit-lib/lib/api/io/private';
74+
import type { SdkProvider } from '../../lib/api';
7275
import { Bootstrapper, type BootstrapSource } from '../../lib/api/bootstrap';
73-
import {
76+
import type {
7477
DeployStackResult,
7578
SuccessfulDeployStackResult,
76-
Deployments,
7779
DeployStackOptions,
7880
DestroyStackOptions,
7981
RollbackStackOptions,
8082
RollbackStackResult,
8183
} from '../../lib/api/deployments';
84+
import {
85+
Deployments,
86+
} from '../../lib/api/deployments';
8287
import { HotswapMode } from '../../lib/api/hotswap';
8388
import { Mode } from '../../lib/api/plugin';
84-
import { Tag } from '../../lib/api/tags';
89+
import type { Tag } from '../../lib/api/tags';
8590
import { CdkToolkit, markTesting } from '../../lib/cli/cdk-toolkit';
91+
import { CliIoHost } from '../../lib/cli/io-host';
8692
import { Configuration } from '../../lib/cli/user-configuration';
93+
import { StackActivityProgress } from '../../lib/commands/deploy';
8794
import { RequireApproval } from '../../lib/commands/diff';
88-
import { CliIoHost } from '../../lib/cli/io-host';
8995
import { flatten } from '../../lib/util';
90-
import { MockCloudExecutable, TestStackArtifact } from '../_helpers/assembly';
9196
import { instanceMockFrom } from '../_helpers/as-mock';
97+
import type { TestStackArtifact } from '../_helpers/assembly';
98+
import { MockCloudExecutable } from '../_helpers/assembly';
9299
import {
93100
mockCloudFormationClient,
94101
MockSdk,
95102
MockSdkProvider,
96103
mockSSMClient,
97104
restoreSdkMocksToDefault,
98105
} from '../_helpers/mock-sdk';
99-
import { asIoHelper } from '../../../@aws-cdk/toolkit-lib/lib/api/io/private';
100-
import { StackActivityProgress } from '../../lib/commands/deploy';
101-
import { Template } from '../../../@aws-cdk/toolkit-lib/lib/api';
102-
import { DestroyStackResult } from '@aws-cdk/toolkit-lib/lib/api/deployments/deploy-stack';
103106

104107
markTesting();
105108

@@ -154,7 +157,6 @@ function defaultToolkitSetup() {
154157

155158
const mockSdk = new MockSdk();
156159

157-
158160
describe('bootstrap', () => {
159161
test('accepts qualifier from context', async () => {
160162
// GIVEN
@@ -276,6 +278,7 @@ describe('deploy', () => {
276278
deployments: new FakeCloudFormation({}),
277279
});
278280
stderrMock.mockImplementation((...x) => {
281+
// eslint-disable-next-line no-console
279282
console.error(...x);
280283
});
281284

@@ -1034,8 +1037,8 @@ describe('deploy', () => {
10341037

10351038
await toolkit.deploy({
10361039
progress: StackActivityProgress.EVENTS,
1037-
selector: { patterns: ["**"] },
1038-
hotswap: HotswapMode.FALL_BACK
1040+
selector: { patterns: ['**'] },
1041+
hotswap: HotswapMode.FALL_BACK,
10391042
});
10401043

10411044
// now expect it to be updated
@@ -1556,7 +1559,6 @@ describe('migrate', () => {
15561559
});
15571560

15581561
describe('rollback', () => {
1559-
15601562
test('rollback uses deployment role', async () => {
15611563
cloudExecutable = new MockCloudExecutable({
15621564
stacks: [MockStack.MOCK_STACK_C],

packages/aws-cdk/test/cli/cli-commands.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('doctor', () => {
2929
await exec(['doctor']);
3030
expect(logging.info).toHaveBeenCalledWith(expect.stringContaining('CDK Version:'));
3131
});
32-
})
32+
});
3333

3434
describe('docs', () => {
3535
test('prints docs url version', async () => {

packages/aws-cdk/test/cli/io-host/cli-io-host-corked.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { debug, error, info, success, warning } from '../../../lib/logging';
21
import { CliIoHost } from '../../../lib/cli/io-host';
2+
import { debug, error, info, success, warning } from '../../../lib/logging';
33

44
const ioHost = CliIoHost.instance({}, true);
55
let mockStderr: jest.Mock;

packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { PassThrough } from 'stream';
2-
import * as chalk from 'chalk';
3-
import { CliIoHost, IoMessage, IoMessageLevel, IoRequest } from '../../../lib/cli/io-host';
42
import { RequireApproval } from '@aws-cdk/cloud-assembly-schema';
3+
import * as chalk from 'chalk';
4+
import type { IoMessage, IoMessageLevel, IoRequest } from '../../../lib/cli/io-host';
5+
import { CliIoHost } from '../../../lib/cli/io-host';
56

67
let passThrough: PassThrough;
78

@@ -429,7 +430,7 @@ describe('CliIoHost', () => {
429430
});
430431

431432
test('require approval by default - respond no', async () => {
432-
expect(() => requestResponse('n', plainMessage({
433+
await expect(() => requestResponse('n', plainMessage({
433434
time: new Date(),
434435
level: 'info',
435436
action: 'synth',

0 commit comments

Comments
 (0)