Skip to content

Commit d6bfabc

Browse files
authored
chore(shared-ini-file-loader): rename normalizeConfigFile to getProfileData (#3569)
1 parent 98045e4 commit d6bfabc

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
1-
import { normalizeConfigFile } from "./normalizeConfigFile";
1+
import { getProfileData } from "./getProfileData";
22

3-
describe(normalizeConfigFile.name, () => {
3+
describe(getProfileData.name, () => {
44
it("returns empty for no data", () => {
5-
expect(normalizeConfigFile({})).toStrictEqual({});
5+
expect(getProfileData({})).toStrictEqual({});
66
});
77

88
it("returns default profile if present", () => {
99
const mockInput = { default: { key: "value" } };
10-
expect(normalizeConfigFile(mockInput)).toStrictEqual(mockInput);
10+
expect(getProfileData(mockInput)).toStrictEqual(mockInput);
1111
});
1212

1313
it("skips profiles without prefix profile", () => {
1414
const mockInput = { test: { key: "value" } };
15-
expect(normalizeConfigFile(mockInput)).toStrictEqual({});
15+
expect(getProfileData(mockInput)).toStrictEqual({});
16+
});
17+
18+
it("skips profiles with different prefix", () => {
19+
const mockInput = { "not-profile test": { key: "value" } };
20+
expect(getProfileData(mockInput)).toStrictEqual({});
1621
});
1722

1823
describe("normalizes profile names", () => {
19-
const getProfileData = (profileName: string) =>
24+
const getMockProfileData = (profileName: string) =>
2025
[1, 2, 3]
2126
.map((num) => [`key_${profileName}_${num}`, `value_${profileName}_${num}`])
2227
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
2328

2429
const getMockOutput = (profileNames: string[]) =>
25-
profileNames.reduce((acc, profileName) => ({ ...acc, [profileName]: getProfileData(profileName) }), {});
30+
profileNames.reduce((acc, profileName) => ({ ...acc, [profileName]: getMockProfileData(profileName) }), {});
2631

2732
const getMockInput = (mockOutput: { [key: string]: { [key: string]: string } }) =>
2833
Object.entries(mockOutput).reduce((acc, [key, value]) => ({ ...acc, [`profile ${key}`]: value }), {});
2934

3035
it("single profile", () => {
3136
const mockOutput = getMockOutput(["one"]);
3237
const mockInput = getMockInput(mockOutput);
33-
expect(normalizeConfigFile(mockInput)).toStrictEqual(mockOutput);
38+
expect(getProfileData(mockInput)).toStrictEqual(mockOutput);
3439
});
3540

3641
it("two profiles", () => {
3742
const mockOutput = getMockOutput(["one", "two"]);
3843
const mockInput = getMockInput(mockOutput);
39-
expect(normalizeConfigFile(mockInput)).toStrictEqual(mockOutput);
44+
expect(getProfileData(mockInput)).toStrictEqual(mockOutput);
4045
});
4146

4247
it("three profiles", () => {
4348
const mockOutput = getMockOutput(["one", "two", "three"]);
4449
const mockInput = getMockInput(mockOutput);
45-
expect(normalizeConfigFile(mockInput)).toStrictEqual(mockOutput);
50+
expect(getProfileData(mockInput)).toStrictEqual(mockOutput);
4651
});
4752

4853
it("with default", () => {
4954
const defaultInput = { default: { key: "value" } };
5055
const mockOutput = getMockOutput(["one"]);
5156
const mockInput = getMockInput(mockOutput);
52-
expect(normalizeConfigFile({ ...defaultInput, ...mockInput })).toStrictEqual({ ...defaultInput, ...mockOutput });
57+
expect(getProfileData({ ...defaultInput, ...mockInput })).toStrictEqual({ ...defaultInput, ...mockOutput });
5358
});
5459

5560
it("with profileName without prefix", () => {
5661
const profileWithPrefix = { test: { key: "value" } };
5762
const mockOutput = getMockOutput(["one"]);
5863
const mockInput = getMockInput(mockOutput);
59-
expect(normalizeConfigFile({ ...profileWithPrefix, ...mockInput })).toStrictEqual(mockOutput);
64+
expect(getProfileData({ ...profileWithPrefix, ...mockInput })).toStrictEqual(mockOutput);
6065
});
6166
});
6267
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ParsedIniData } from "@aws-sdk/types";
2+
3+
const profileKeyRegex = /^profile\s(["'])?([^\1]+)\1$/;
4+
5+
/**
6+
* Returns the profile data from parsed ini data.
7+
* * Returns data for `default`
8+
* * Reads profileName after profile prefix including/excluding quotes
9+
*/
10+
export const getProfileData = (data: ParsedIniData): ParsedIniData =>
11+
Object.entries(data)
12+
// filter out non-profile keys
13+
.filter(([key]) => profileKeyRegex.test(key))
14+
// replace profile key with profile name
15+
.reduce((acc, [key, value]) => ({ ...acc, [profileKeyRegex.exec(key)![2]]: value }), {
16+
// Populate default profile, if present.
17+
...(data.default && { default: data.default }),
18+
});

packages/shared-ini-file-loader/src/loadSharedConfigFiles.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { join } from "path";
22

33
import { getHomeDir } from "./getHomeDir";
4+
import { getProfileData } from "./getProfileData";
45
import { ENV_CONFIG_PATH, ENV_CREDENTIALS_PATH, loadSharedConfigFiles } from "./loadSharedConfigFiles";
5-
import { normalizeConfigFile } from "./normalizeConfigFile";
66
import { parseIni } from "./parseIni";
77
import { slurpFile } from "./slurpFile";
88

99
jest.mock("path");
1010
jest.mock("./getHomeDir");
11-
jest.mock("./normalizeConfigFile");
11+
jest.mock("./getProfileData");
1212
jest.mock("./parseIni");
1313
jest.mock("./slurpFile");
1414

@@ -34,7 +34,7 @@ describe("loadSharedConfigFiles", () => {
3434
(join as jest.Mock).mockImplementation((...args) => args.join(mockSeparator));
3535
(getHomeDir as jest.Mock).mockReturnValue(mockHomeDir);
3636
(parseIni as jest.Mock).mockImplementation((args) => args);
37-
(normalizeConfigFile as jest.Mock).mockImplementation((args) => args);
37+
(getProfileData as jest.Mock).mockImplementation((args) => args);
3838
(slurpFile as jest.Mock).mockImplementation((path) => Promise.resolve(path));
3939
});
4040

@@ -82,7 +82,7 @@ describe("loadSharedConfigFiles", () => {
8282
});
8383

8484
it("when normalizeConfigFile throws error", async () => {
85-
(normalizeConfigFile as jest.Mock).mockRejectedValue("error");
85+
(getProfileData as jest.Mock).mockRejectedValue("error");
8686
const sharedConfigFiles = await loadSharedConfigFiles();
8787
expect(sharedConfigFiles).toStrictEqual({
8888
configFile: {},

packages/shared-ini-file-loader/src/loadSharedConfigFiles.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SharedConfigFiles } from "@aws-sdk/types";
22
import { join } from "path";
33

44
import { getHomeDir } from "./getHomeDir";
5-
import { normalizeConfigFile } from "./normalizeConfigFile";
5+
import { getProfileData } from "./getProfileData";
66
import { parseIni } from "./parseIni";
77
import { slurpFile } from "./slurpFile";
88

@@ -34,7 +34,7 @@ export const loadSharedConfigFiles = async (init: SharedConfigInit = {}): Promis
3434
} = init;
3535

3636
const parsedFiles = await Promise.all([
37-
slurpFile(configFilepath).then(parseIni).then(normalizeConfigFile).catch(swallowError),
37+
slurpFile(configFilepath).then(parseIni).then(getProfileData).catch(swallowError),
3838
slurpFile(filepath).then(parseIni).catch(swallowError),
3939
]);
4040

packages/shared-ini-file-loader/src/normalizeConfigFile.ts

-21
This file was deleted.

0 commit comments

Comments
 (0)