Skip to content

Commit 504d081

Browse files
committed
chore: use booleanSelector
1 parent 461ae0f commit 504d081

File tree

3 files changed

+37
-68
lines changed

3 files changed

+37
-68
lines changed

packages/middleware-bucket-endpoint/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
"license": "Apache-2.0",
2020
"dependencies": {
2121
"@aws-sdk/protocol-http": "3.38.0",
22+
"@aws-sdk/node-config-provider": "3.39.0",
2223
"@aws-sdk/types": "3.38.0",
2324
"@aws-sdk/util-arn-parser": "3.37.0",
2425
"tslib": "^2.3.0"
2526
},
2627
"devDependencies": {
2728
"@aws-sdk/middleware-stack": "3.38.0",
28-
"@aws-sdk/node-config-provider": "3.39.0",
2929
"@types/jest": "^26.0.4",
3030
"jest": "^26.1.0",
3131
"typescript": "~4.3.5"
Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,50 @@
1+
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";
2+
13
import {
2-
NODE_USE_ARN_REGION_CONFIG_OPTIONS as loadOptions,
4+
NODE_USE_ARN_REGION_CONFIG_OPTIONS,
35
NODE_USE_ARN_REGION_ENV_NAME,
46
NODE_USE_ARN_REGION_INI_NAME,
57
} from "./NodeUseArnRegionConfigOptions";
68

7-
describe("Node useArnRegion config loader", () => {
8-
describe("environment variable selector", () => {
9-
const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any;
9+
jest.mock("@aws-sdk/node-config-provider");
1010

11-
beforeEach(() => {
12-
delete env[NODE_USE_ARN_REGION_ENV_NAME];
13-
});
11+
describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => {
12+
afterEach(() => {
13+
jest.clearAllMocks();
14+
});
1415

15-
it(`should return undefined if the environment variable doesn't have ${NODE_USE_ARN_REGION_ENV_NAME}`, () => {
16-
expect(loadOptions.environmentVariableSelector(env)).toBeUndefined();
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);
1721
});
1822

19-
[
20-
{ envValue: "true", parsed: true },
21-
{ envValue: "false", parsed: false },
22-
].forEach(({ envValue, parsed }) => {
23-
it(`should return boolean if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => {
24-
env[NODE_USE_ARN_REGION_ENV_NAME] = envValue;
25-
expect(loadOptions.environmentVariableSelector(env)).toBe(parsed);
23+
it("throws error", () => {
24+
const mockError = new Error("error");
25+
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
26+
throw mockError;
2627
});
28+
expect(() => {
29+
func(obj);
30+
}).toThrow(mockError);
2731
});
32+
};
2833

29-
["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((envValue) => {
30-
it(`should throw if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => {
31-
env[NODE_USE_ARN_REGION_ENV_NAME] = envValue as any;
32-
expect(() => loadOptions.environmentVariableSelector(env)).toThrow(
33-
`Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${envValue}.`
34-
);
35-
});
36-
});
34+
describe("calls booleanSelector for environmentVariableSelector", () => {
35+
const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any;
36+
const { environmentVariableSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
37+
test(environmentVariableSelector, env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV);
3738
});
3839

39-
describe("shared INI files selector", () => {
40+
describe("calls booleanSelector for configFileSelector", () => {
4041
const profileContent: { [NODE_USE_ARN_REGION_INI_NAME]: any } = {} as any;
42+
const { configFileSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
43+
test(configFileSelector, profileContent, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG);
44+
});
4145

42-
beforeEach(() => {
43-
delete profileContent[NODE_USE_ARN_REGION_INI_NAME];
44-
});
45-
46-
it(`should return undefined if shared config profile doesn't have ${NODE_USE_ARN_REGION_INI_NAME}`, () => {
47-
expect(loadOptions.configFileSelector(profileContent)).toBeUndefined();
48-
});
49-
50-
[
51-
{ value: "true", parsed: true },
52-
{ value: "false", parsed: false },
53-
].forEach(({ value, parsed }) => {
54-
it(`should return boolean if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => {
55-
profileContent[NODE_USE_ARN_REGION_INI_NAME] = value;
56-
expect(loadOptions.configFileSelector(profileContent)).toBe(parsed);
57-
});
58-
});
59-
60-
["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((value) => {
61-
it(`should throw if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => {
62-
profileContent[NODE_USE_ARN_REGION_INI_NAME] = value as any;
63-
expect(() => loadOptions.configFileSelector(profileContent)).toThrow(
64-
`Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${value}.`
65-
);
66-
});
67-
});
46+
it("returns false for default", () => {
47+
const { default: defaultValue } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
48+
expect(defaultValue).toEqual(false);
6849
});
6950
});
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
2+
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";
23

34
export const NODE_USE_ARN_REGION_ENV_NAME = "AWS_S3_USE_ARN_REGION";
45
export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region";
@@ -9,21 +10,8 @@ export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region";
910
* @api private
1011
*/
1112
export const NODE_USE_ARN_REGION_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
12-
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
13-
if (!Object.prototype.hasOwnProperty.call(env, NODE_USE_ARN_REGION_ENV_NAME)) return undefined;
14-
if (env[NODE_USE_ARN_REGION_ENV_NAME] === "true") return true;
15-
if (env[NODE_USE_ARN_REGION_ENV_NAME] === "false") return false;
16-
throw new Error(
17-
`Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${env[NODE_USE_ARN_REGION_ENV_NAME]}.`
18-
);
19-
},
20-
configFileSelector: (profile) => {
21-
if (!Object.prototype.hasOwnProperty.call(profile, NODE_USE_ARN_REGION_INI_NAME)) return undefined;
22-
if (profile[NODE_USE_ARN_REGION_INI_NAME] === "true") return true;
23-
if (profile[NODE_USE_ARN_REGION_INI_NAME] === "false") return false;
24-
throw new Error(
25-
`Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${profile[NODE_USE_ARN_REGION_INI_NAME]}.`
26-
);
27-
},
13+
environmentVariableSelector: (env: NodeJS.ProcessEnv) =>
14+
booleanSelector(env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV),
15+
configFileSelector: (profile) => booleanSelector(profile, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG),
2816
default: false,
2917
};

0 commit comments

Comments
 (0)