Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(region-info): facts only returned from constant region list #27506

Merged
merged 9 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/aws-cdk-lib/region-info/lib/fact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)])];
}

/**
Expand All @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions packages/aws-cdk-lib/region-info/test/fact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/region-info/test/region-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});