Skip to content

Commit 6884910

Browse files
authored
Process config files for profile names containing prefix separator (#1100)
1 parent bc4911f commit 6884910

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

.changeset/sharp-pants-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/shared-ini-file-loader": patch
3+
---
4+
5+
Process config files for profile names containing prefix separator

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe(getConfigData.name, () => {
2121
it.each([IniSectionType.SSO_SESSION, IniSectionType.SERVICES])("includes sections with '%s' prefix", (prefix) => {
2222
const mockInput = { [[prefix, "test"].join(CONFIG_PREFIX_SEPARATOR)]: { key: "value" } };
2323
expect(getConfigData(mockInput)).toStrictEqual(mockInput);
24+
25+
// Profile name containing CONFIG_PREFIX_SEPARATOR
26+
const profileName = ["foo", "bar"].join(CONFIG_PREFIX_SEPARATOR);
27+
const mockInput2 = { [[prefix, profileName].join(CONFIG_PREFIX_SEPARATOR)]: { key: "value" } };
28+
expect(getConfigData(mockInput2)).toStrictEqual(mockInput2);
2429
});
2530

2631
describe("normalizes profile names", () => {
@@ -38,6 +43,13 @@ describe(getConfigData.name, () => {
3843
{}
3944
);
4045

46+
it("profile containing CONFIG_PREFIX_SEPARATOR", () => {
47+
const profileName = ["foo", "bar"].join(CONFIG_PREFIX_SEPARATOR);
48+
const mockOutput = getMockOutput([profileName]);
49+
const mockInput = getMockInput(mockOutput);
50+
expect(getConfigData(mockInput)).toStrictEqual(mockOutput);
51+
});
52+
4153
it("single profile", () => {
4254
const mockOutput = getMockOutput(["one"]);
4355
const mockInput = getMockInput(mockOutput);

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
1010
*/
1111
export const getConfigData = (data: ParsedIniData): ParsedIniData =>
1212
Object.entries(data)
13-
// filter out
1413
.filter(([key]) => {
15-
const sections = key.split(CONFIG_PREFIX_SEPARATOR);
16-
if (sections.length === 2 && Object.values(IniSectionType).includes(sections[0] as IniSectionType)) {
17-
return true;
14+
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
15+
if (indexOfSeparator === -1) {
16+
// filter out keys which do not contain CONFIG_PREFIX_SEPARATOR.
17+
return false;
1818
}
19-
return false;
19+
// Check if prefix is a valid IniSectionType.
20+
return Object.values(IniSectionType).includes(key.substring(0, indexOfSeparator) as IniSectionType);
2021
})
21-
// replace profile prefix, if present.
22+
// remove profile prefix, if present.
2223
.reduce(
2324
(acc, [key, value]) => {
24-
const updatedKey = key.startsWith(IniSectionType.PROFILE) ? key.split(CONFIG_PREFIX_SEPARATOR)[1] : key;
25+
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
26+
const updatedKey =
27+
key.substring(0, indexOfSeparator) === IniSectionType.PROFILE ? key.substring(indexOfSeparator + 1) : key;
2528
acc[updatedKey] = value;
2629
return acc;
2730
},

0 commit comments

Comments
 (0)