From 7121c7ee9eed595acfed1fb09f55b8bd966c01e3 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 24 Oct 2023 07:44:42 -0600 Subject: [PATCH] fix(region-info): facts only returned from constant region list (#27506) Facts are only being returned from the regions in a constant list. This PR returns facts for all Regions in `AWS_REGIONS` + `this.database`. Closes #27260. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/region-info/lib/fact.ts | 10 +++++----- .../aws-cdk-lib/region-info/test/fact.test.ts | 19 +++++++++++++++++++ .../region-info/test/region-info.test.ts | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index 366141721c693..67bbe9ce42d52 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -5,12 +5,12 @@ import { AWS_REGIONS } from './aws-entities'; */ export class Fact { /** - * @returns the list of names of AWS regions for which there is at least one registered fact. This - * may not be an exhaustive list of all available AWS regions. + * @returns the list of names of AWS Regions for which there is at least one registered fact. This + * includes Regions defined in AWS_REGIONS plus custom defined regions. */ public static get regions(): string[] { - // Return by copy to ensure no modifications can be made to the undelying constant. - return Array.from(AWS_REGIONS); + // Return the union of regions in AWS_REGIONS and custom defined regions. + return [...new Set([...AWS_REGIONS, ...Object.keys(this.database)])]; } /** @@ -37,7 +37,7 @@ export class Fact { const foundFact = this.find(region, name); if (!foundFact) { - throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}`); + throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}.`); } return foundFact; diff --git a/packages/aws-cdk-lib/region-info/test/fact.test.ts b/packages/aws-cdk-lib/region-info/test/fact.test.ts index b3e3bab7fdab7..a77e34bade5d1 100644 --- a/packages/aws-cdk-lib/region-info/test/fact.test.ts +++ b/packages/aws-cdk-lib/region-info/test/fact.test.ts @@ -89,4 +89,23 @@ describe('register', () => { // Cleanup Fact.unregister(region, name); }); + + test('registering a fact with a new region adds the region', () => { + // GIVEN + const region = 'my-custom-region'; + const name = FactName.PARTITION; + const value = 'nebraska'; + + // WHEN + expect(Fact.find(region, name)).not.toBe(value); + expect(() => Fact.register({ region, name, value })).not.toThrowError(); + + // THEN + expect(Fact.regions.includes('my-custom-region')).toBeTruthy(); + expect(Fact.find(region, name)).toBe('nebraska'); + }); + + test('regions does not return duplicate regions', () => { + expect(new Set(Fact.regions).size == Fact.regions.length).toBeTruthy(); + }); }); diff --git a/packages/aws-cdk-lib/region-info/test/region-info.test.ts b/packages/aws-cdk-lib/region-info/test/region-info.test.ts index f014405a8b145..76130465d6567 100644 --- a/packages/aws-cdk-lib/region-info/test/region-info.test.ts +++ b/packages/aws-cdk-lib/region-info/test/region-info.test.ts @@ -72,4 +72,4 @@ test.each([ ['us-iso-west-1', false], ])('%p should be opt-in: %p', (region, expected) => { expect(RegionInfo.get(region).isOptInRegion).toEqual(expected); -}); \ No newline at end of file +});