Skip to content

Commit 89b9a81

Browse files
committed
chore: use booleanSelector
1 parent 504d081 commit 89b9a81

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";
2+
3+
import {
4+
NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS,
5+
NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME,
6+
NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME,
7+
} from "./NodeDisableMultiregionAccessPointConfigOptions";
8+
9+
jest.mock("@aws-sdk/node-config-provider");
10+
11+
describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => {
12+
afterEach(() => {
13+
jest.clearAllMocks();
14+
});
15+
16+
const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => {
17+
it.each([true, false, undefined])("returns %s", (output) => {
18+
(booleanSelector as jest.Mock).mockReturnValueOnce(output);
19+
expect(func(obj)).toEqual(output);
20+
expect(booleanSelector).toBeCalledWith(obj, key, type);
21+
});
22+
23+
it("throws error", () => {
24+
const mockError = new Error("error");
25+
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
26+
throw mockError;
27+
});
28+
expect(() => {
29+
func(obj);
30+
}).toThrow(mockError);
31+
});
32+
};
33+
34+
describe("calls booleanSelector for environmentVariableSelector", () => {
35+
const env: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]: any } = {} as any;
36+
const { environmentVariableSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
37+
test(environmentVariableSelector, env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV);
38+
});
39+
40+
describe("calls booleanSelector for configFileSelector", () => {
41+
const profileContent: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]: any } = {} as any;
42+
const { configFileSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
43+
test(configFileSelector, profileContent, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG);
44+
});
45+
46+
it("returns false for default", () => {
47+
const { default: defaultValue } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
48+
expect(defaultValue).toEqual(false);
49+
});
50+
});
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
2+
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";
23

34
export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS";
45
export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME = "s3_disable_multiregion_access_points";
56

67
export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
7-
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
8-
if (!Object.prototype.hasOwnProperty.call(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME)) return undefined;
9-
if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "true") return true;
10-
if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "false") return false;
11-
throw new Error(
12-
`Cannot load env ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME}. Expected "true" or "false", got ${env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]}.`
13-
);
14-
},
15-
configFileSelector: (profile) => {
16-
if (!Object.prototype.hasOwnProperty.call(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME))
17-
return undefined;
18-
if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "true") return true;
19-
if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "false") return false;
20-
throw new Error(
21-
`Cannot load shared config entry ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME}. Expected "true" or "false", got ${profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]}.`
22-
);
23-
},
8+
environmentVariableSelector: (env: NodeJS.ProcessEnv) =>
9+
booleanSelector(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV),
10+
configFileSelector: (profile) =>
11+
booleanSelector(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG),
2412
default: false,
2513
};

0 commit comments

Comments
 (0)