Skip to content

Commit 783fc98

Browse files
committed
refactor(cli): inject CLI version as a parameter to Notices
- Remove direct dependency on versionNumber import in Notices class - Add cliVersion as a required parameter in NoticesProps interface - Store cliVersion as a class property and use it in display method - Improve testability by removing the need to mock version functions
1 parent 634f512 commit 783fc98

File tree

6 files changed

+48
-56
lines changed

6 files changed

+48
-56
lines changed

packages/@aws-cdk/tmp-toolkit-helpers/src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './format-error';
99
export * from './json';
1010
export * from './objects';
1111
export * from './parallel';
12+
export * from './package-info';
1213
export * from './serialize';
1314
export * from './string-manipulation';
1415
export * from './type-brands';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as path from 'path';
2+
import { bundledPackageRootDir } from './directories';
3+
4+
export function displayVersion() {
5+
return `${versionNumber()} (build ${commit()})`;
6+
}
7+
8+
export function isDeveloperBuild(): boolean {
9+
return versionNumber() === '0.0.0';
10+
}
11+
12+
export function versionNumber(): string {
13+
// eslint-disable-next-line @typescript-eslint/no-require-imports
14+
return require(path.join(bundledPackageRootDir(__dirname), 'package.json')).version.replace(/\+[0-9a-f]+$/, '');
15+
}
16+
17+
function commit(): string {
18+
// eslint-disable-next-line @typescript-eslint/no-require-imports
19+
return require(path.join(bundledPackageRootDir(__dirname), 'build-info.json')).commit;
20+
}

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
117117
proxyAddress: configuration.settings.get(['proxy']),
118118
caBundlePath: configuration.settings.get(['caBundlePath']),
119119
},
120+
cliVersion: version.versionNumber(),
120121
});
121122
await notices.refresh();
122123

packages/aws-cdk/lib/context-providers/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { TRANSIENT_CONTEXT_KEY } from '../api/context';
1919
import { replaceEnvPlaceholders } from '../api/environment';
2020
import { PluginHost } from '../api/plugin';
2121
import type { ContextProviderPlugin } from '../api/plugin/context-provider-plugin';
22-
import { CliIoHost } from '../cli/io-host';
2322
import { formatErrorMessage } from '../util';
2423

2524
type ContextProviderFactory = ((sdk: SdkProvider, io: IContextProviderMessages) => ContextProviderPlugin);
@@ -98,7 +97,6 @@ export async function provideContextValues(
9897
}
9998
}
10099

101-
ioHelper = ioHelper ?? CliIoHost.instance().asIoHelper();
102100
const provider = factory(sdk, new ContextProviderMessages(ioHelper, providerName));
103101

104102
let value;

packages/aws-cdk/lib/notices.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { ProxyAgentProvider } from './api/aws-auth';
1010
import type { Context } from './api/context';
1111
import type { ConstructTreeNode } from './api/tree';
1212
import { loadTreeFromDir } from './api/tree';
13-
import { versionNumber } from './cli/version';
1413
import { cdkCacheDir, formatErrorMessage } from './util';
1514
import type { IIoHost } from '../../@aws-cdk/tmp-toolkit-helpers/src/api';
1615
import { ToolkitError } from '../../@aws-cdk/tmp-toolkit-helpers/src/api';
@@ -48,6 +47,11 @@ export interface NoticesProps {
4847
* Where messages are going to be sent
4948
*/
5049
readonly ioHost: IIoHost;
50+
51+
/**
52+
* The version of the CLI
53+
*/
54+
readonly cliVersion: string;
5155
}
5256

5357
export interface NoticesPrintOptions {
@@ -307,6 +311,7 @@ export class Notices {
307311
private readonly httpOptions: SdkHttpOptions;
308312
private readonly ioHelper: IoHelper;
309313
private readonly ioMessages: IoDefaultMessages;
314+
private readonly cliVersion: string;
310315

311316
private data: Set<Notice> = new Set();
312317

@@ -321,6 +326,7 @@ export class Notices {
321326
this.httpOptions = props.httpOptions ?? {};
322327
this.ioHelper = asIoHelper(props.ioHost, 'notices' as any /* forcing a CliAction to a ToolkitAction */);
323328
this.ioMessages = new IoDefaultMessages(this.ioHelper);
329+
this.cliVersion = props.cliVersion;
324330
}
325331

326332
/**
@@ -361,7 +367,7 @@ export class Notices {
361367
public display(options: NoticesPrintOptions = {}) {
362368
const filteredNotices = new NoticesFilter(this.ioMessages).filter({
363369
data: Array.from(this.data),
364-
cliVersion: versionNumber(),
370+
cliVersion: this.cliVersion,
365371
outDir: this.output,
366372
bootstrappedEnvironments: Array.from(this.bootstrappedEnvironments.values()),
367373
});

packages/aws-cdk/test/notices.test.ts

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ describe(CachedDataSource, () => {
630630
});
631631

632632
describe(Notices, () => {
633+
const cliVersion = '2.1005.0';
634+
633635
beforeEach(() => {
634636
// disable caching
635637
jest.spyOn(CachedDataSource.prototype as any, 'save').mockImplementation((_: any) => Promise.resolve());
@@ -642,7 +644,7 @@ describe(Notices, () => {
642644

643645
describe('addBootstrapVersion', () => {
644646
test('can add multiple values', async () => {
645-
const notices = Notices.create({ context: new Context(), ioHost });
647+
const notices = Notices.create({ context: new Context(), ioHost, cliVersion });
646648
notices.addBootstrappedEnvironment({ bootstrapStackVersion: 10, environment: { account: 'account', region: 'region', name: 'env' } });
647649
notices.addBootstrappedEnvironment({ bootstrapStackVersion: 11, environment: { account: 'account', region: 'region', name: 'env' } });
648650

@@ -656,13 +658,10 @@ describe(Notices, () => {
656658
});
657659

658660
test('deduplicates', async () => {
659-
const notices = Notices.create({ ioHost, context: new Context() });
661+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
660662
notices.addBootstrappedEnvironment({ bootstrapStackVersion: 10, environment: { account: 'account', region: 'region', name: 'env' } });
661663
notices.addBootstrappedEnvironment({ bootstrapStackVersion: 10, environment: { account: 'account', region: 'region', name: 'env' } });
662664

663-
// mock cli version number
664-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
665-
666665
notices.display();
667666

668667
const filter = jest.spyOn(NoticesFilter.prototype, 'filter');
@@ -687,10 +686,7 @@ describe(Notices, () => {
687686

688687
describe('refresh', () => {
689688
test('deduplicates notices', async () => {
690-
// within the affected version range of the notice
691-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
692-
693-
const notices = Notices.create({ ioHost, context: new Context() });
689+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
694690
await notices.refresh({
695691
dataSource: { fetch: async () => [BASIC_NOTICE, BASIC_NOTICE] },
696692
});
@@ -700,10 +696,7 @@ describe(Notices, () => {
700696
});
701697

702698
test('clears notices if empty', async () => {
703-
// within the affected version range of the notice
704-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
705-
706-
const notices = Notices.create({ ioHost, context: new Context() });
699+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
707700
await notices.refresh({
708701
dataSource: { fetch: async () => [] },
709702
});
@@ -713,7 +706,7 @@ describe(Notices, () => {
713706
});
714707

715708
test('doesnt throw', async () => {
716-
const notices = Notices.create({ ioHost, context: new Context() });
709+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion });
717710
await notices.refresh({
718711
dataSource: {
719712
fetch: async () => {
@@ -724,12 +717,9 @@ describe(Notices, () => {
724717
});
725718

726719
test('filters out acknowledged notices by default', async () => {
727-
// within the affected version range of both notices
728-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.126.0');
729-
730720
const context = new Context({ bag: new Settings({ 'acknowledged-issue-numbers': [MULTIPLE_AFFECTED_VERSIONS_NOTICE.issueNumber] }) });
731721

732-
const notices = Notices.create({ ioHost, context });
722+
const notices = Notices.create({ ioHost, context, cliVersion: '1.126.0' });
733723
await notices.refresh({
734724
dataSource: { fetch: async () => [BASIC_NOTICE, MULTIPLE_AFFECTED_VERSIONS_NOTICE] },
735725
});
@@ -740,12 +730,9 @@ describe(Notices, () => {
740730
});
741731

742732
test('preserves acknowledged notices if requested', async () => {
743-
// within the affected version range of both notices
744-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.126.0');
745-
746733
const context = new Context({ bag: new Settings({ 'acknowledged-issue-numbers': [MULTIPLE_AFFECTED_VERSIONS_NOTICE.issueNumber] }) });
747734

748-
const notices = Notices.create({ ioHost, context, includeAcknowledged: true });
735+
const notices = Notices.create({ ioHost, context, includeAcknowledged: true, cliVersion: '1.126.0' });
749736
await notices.refresh({
750737
dataSource: { fetch: async () => [BASIC_NOTICE, MULTIPLE_AFFECTED_VERSIONS_NOTICE] },
751738
});
@@ -758,10 +745,7 @@ describe(Notices, () => {
758745

759746
describe('display', () => {
760747
test('notices envelop', async () => {
761-
// within the affected version range of the notice
762-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
763-
764-
const notices = Notices.create({ ioHost, context: new Context() });
748+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
765749
await notices.refresh({
766750
dataSource: { fetch: async () => [BASIC_NOTICE, BASIC_NOTICE] },
767751
});
@@ -772,10 +756,7 @@ describe(Notices, () => {
772756
});
773757

774758
test('deduplicates notices', async () => {
775-
// within the affected version range of the notice
776-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
777-
778-
const notices = Notices.create({ ioHost, context: new Context() });
759+
const notices = Notices.create({ ioHost, context: new Context() , cliVersion: '1.0.0'});
779760
await notices.refresh({
780761
dataSource: { fetch: async () => [BASIC_NOTICE, BASIC_NOTICE] },
781762
});
@@ -786,22 +767,19 @@ describe(Notices, () => {
786767
});
787768

788769
test('nothing when there are no notices', async () => {
789-
Notices.create({ ioHost, context: new Context() }).display();
770+
Notices.create({ ioHost, context: new Context(), cliVersion }).display();
790771
// expect a single trace that the tree.json was not found, but nothing else
791772
expect(ioHost.messages.length).toBe(1);
792773
ioHost.expectMessage({ level: 'trace', containing: 'Failed to get tree.json file' });
793774
});
794775

795776
test('total count when show total is true', async () => {
796-
Notices.create({ ioHost, context: new Context() }).display({ showTotal: true });
777+
Notices.create({ ioHost, context: new Context(), cliVersion }).display({ showTotal: true });
797778
ioHost.expectMessage({ containing: 'There are 0 unacknowledged notice(s).' });
798779
});
799780

800781
test('warning', async () => {
801-
// within the affected version range of the notice
802-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
803-
804-
const notices = Notices.create({ ioHost, context: new Context() });
782+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
805783
await notices.refresh({
806784
dataSource: { fetch: async () => [BASIC_WARNING_NOTICE] },
807785
});
@@ -811,10 +789,7 @@ describe(Notices, () => {
811789
});
812790

813791
test('error', async () => {
814-
// within the affected version range of the notice
815-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
816-
817-
const notices = Notices.create({ ioHost, context: new Context() });
792+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
818793
await notices.refresh({
819794
dataSource: { fetch: async () => [BASIC_ERROR_NOTICE] },
820795
});
@@ -824,10 +799,7 @@ describe(Notices, () => {
824799
});
825800

826801
test('only relevant notices', async () => {
827-
// within the affected version range of the notice
828-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.0.0');
829-
830-
const notices = Notices.create({ ioHost, context: new Context() });
802+
const notices = Notices.create({ ioHost, context: new Context(), cliVersion: '1.0.0' });
831803
await notices.refresh({
832804
dataSource: { fetch: async () => [BASIC_NOTICE] },
833805
});
@@ -837,12 +809,9 @@ describe(Notices, () => {
837809
});
838810

839811
test('only unacknowledged notices', async () => {
840-
// within the affected version range of both notices
841-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.126.0');
842-
843812
const context = new Context({ bag: new Settings({ 'acknowledged-issue-numbers': [MULTIPLE_AFFECTED_VERSIONS_NOTICE.issueNumber] }) });
844813

845-
const notices = Notices.create({ ioHost, context });
814+
const notices = Notices.create({ ioHost, context, cliVersion: '1.126.0' });
846815
await notices.refresh({
847816
dataSource: { fetch: async () => [BASIC_NOTICE, MULTIPLE_AFFECTED_VERSIONS_NOTICE] },
848817
});
@@ -852,11 +821,8 @@ describe(Notices, () => {
852821
});
853822

854823
test('can include acknowledged notices if requested', async () => {
855-
// within the affected version range of both notices
856-
jest.spyOn(version, 'versionNumber').mockImplementation(() => '1.126.0');
857-
858824
const context = new Context({ bag: new Settings({ 'acknowledged-issue-numbers': [MULTIPLE_AFFECTED_VERSIONS_NOTICE.issueNumber] }) });
859-
const notices = Notices.create({ ioHost, context, includeAcknowledged: true });
825+
const notices = Notices.create({ ioHost, context, includeAcknowledged: true, cliVersion: '1.126.0' });
860826
await notices.refresh({
861827
dataSource: { fetch: async () => [BASIC_NOTICE, MULTIPLE_AFFECTED_VERSIONS_NOTICE] },
862828
});

0 commit comments

Comments
 (0)