Skip to content

Commit debcfed

Browse files
authored
chore(shared-ini-file-loader): move code from util-credentials (#3420)
1 parent ad6890d commit debcfed

File tree

8 files changed

+154
-33
lines changed

8 files changed

+154
-33
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { DEFAULT_PROFILE, ENV_PROFILE, getProfileName } from "./getProfileName";
2+
3+
describe(getProfileName.name, () => {
4+
const OLD_ENV = process.env;
5+
const mockProfileNameFromEnv = "mockProfileNameFromEnv";
6+
7+
beforeEach(() => {
8+
process.env = {
9+
...OLD_ENV,
10+
[ENV_PROFILE]: mockProfileNameFromEnv,
11+
};
12+
});
13+
14+
afterEach(() => {
15+
process.env = OLD_ENV;
16+
});
17+
18+
it("returns profile if present in param", () => {
19+
const profile = "mockProfile";
20+
expect(getProfileName({ profile })).toBe(profile);
21+
});
22+
23+
it(`returns profile from env var '${ENV_PROFILE}' if present`, () => {
24+
expect(getProfileName({})).toBe(mockProfileNameFromEnv);
25+
});
26+
27+
it(`returns profile '${DEFAULT_PROFILE}' as default`, () => {
28+
process.env[ENV_PROFILE] = undefined;
29+
expect(getProfileName({})).toBe(DEFAULT_PROFILE);
30+
});
31+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const ENV_PROFILE = "AWS_PROFILE";
2+
export const DEFAULT_PROFILE = "default";
3+
4+
export const getProfileName = (init: { profile?: string }): string =>
5+
init.profile || process.env[ENV_PROFILE] || DEFAULT_PROFILE;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from "./getHomeDir";
2+
export * from "./getProfileName";
23
export * from "./loadSharedConfigFiles";
4+
export * from "./parseKnownFiles";
35
export * from "./types";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { loadSharedConfigFiles } from "./loadSharedConfigFiles";
2+
import { parseKnownFiles } from "./parseKnownFiles";
3+
4+
jest.mock("./loadSharedConfigFiles");
5+
6+
describe(parseKnownFiles.name, () => {
7+
const mockConfigFile = {
8+
mockConfigProfileName1: { configKey1: "configValue1" },
9+
mockConfigProfileName2: { configKey2: "configValue2" },
10+
};
11+
const mockCredentialsFile = {
12+
mockCredentialsProfileName1: { credsKey1: "credsValue1" },
13+
mockCredentialsProfileName2: { credsKey2: "credsValue2" },
14+
};
15+
16+
afterEach(() => {
17+
jest.clearAllMocks();
18+
});
19+
20+
it("gets parsedFiles from loadedConfig if provided in init", async () => {
21+
const parsedFiles = await parseKnownFiles({
22+
loadedConfig: Promise.resolve({
23+
configFile: mockConfigFile,
24+
credentialsFile: mockCredentialsFile,
25+
}),
26+
});
27+
28+
expect(loadSharedConfigFiles).not.toHaveBeenCalled();
29+
expect(parsedFiles).toEqual({
30+
...mockConfigFile,
31+
...mockCredentialsFile,
32+
});
33+
});
34+
35+
it("gets parsedFiles from loadSharedConfigFiles if not provided in init", async () => {
36+
(loadSharedConfigFiles as jest.Mock).mockReturnValue(
37+
Promise.resolve({
38+
configFile: mockConfigFile,
39+
credentialsFile: mockCredentialsFile,
40+
})
41+
);
42+
const mockInit = { profile: "mockProfile" };
43+
const parsedFiles = await parseKnownFiles(mockInit);
44+
45+
expect(loadSharedConfigFiles).toHaveBeenCalledWith(mockInit);
46+
expect(parsedFiles).toEqual({
47+
...mockConfigFile,
48+
...mockCredentialsFile,
49+
});
50+
});
51+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ParsedIniData, SharedConfigFiles } from "@aws-sdk/types";
2+
3+
import { loadSharedConfigFiles, SharedConfigInit } from "./loadSharedConfigFiles";
4+
5+
export interface SourceProfileInit extends SharedConfigInit {
6+
/**
7+
* The configuration profile to use.
8+
*/
9+
profile?: string;
10+
11+
/**
12+
* A promise that will be resolved with loaded and parsed credentials files.
13+
* Used to avoid loading shared config files multiple times.
14+
*
15+
* @internal
16+
*/
17+
loadedConfig?: Promise<SharedConfigFiles>;
18+
}
19+
20+
/**
21+
* Load profiles from credentials and config INI files and normalize them into a
22+
* single profile list.
23+
*
24+
* @internal
25+
*/
26+
export const parseKnownFiles = async (init: SourceProfileInit): Promise<ParsedIniData> => {
27+
const { loadedConfig = loadSharedConfigFiles(init) } = init;
28+
29+
const parsedFiles = await loadedConfig;
30+
return {
31+
...parsedFiles.configFile,
32+
...parsedFiles.credentialsFile,
33+
};
34+
};
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
export const ENV_PROFILE = "AWS_PROFILE";
2-
export const DEFAULT_PROFILE = "default";
1+
import {
2+
DEFAULT_PROFILE as __DEFAULT_PROFILE,
3+
ENV_PROFILE as __ENV_PROFILE,
4+
getProfileName,
5+
} from "@aws-sdk/shared-ini-file-loader";
36

4-
export const getMasterProfileName = (init: { profile?: string }): string =>
5-
init.profile || process.env[ENV_PROFILE] || DEFAULT_PROFILE;
7+
/**
8+
* @deprecated Use DEFAULT_PROFILE from "@aws-sdk/shared-ini-file-loader" instead.
9+
*/
10+
export const DEFAULT_PROFILE = __DEFAULT_PROFILE;
11+
12+
/**
13+
* @deprecated Use ENV_PROFILE from "@aws-sdk/shared-ini-file-loader" instead.
14+
*/
15+
export const ENV_PROFILE = __ENV_PROFILE;
16+
17+
/**
18+
* @deprecated Use getProfileName from "@aws-sdk/shared-ini-file-loader" instead.
19+
*/
20+
export const getMasterProfileName = getProfileName;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
// ToDo: Delete and deprecate util-credentials package after all dependents are moved
2+
// to use "@aws-sdk/shared-inin-file-loader" and a release is done.
13
export * from "./get-master-profile-name";
24
export * from "./parse-known-profiles";
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
import { loadSharedConfigFiles, SharedConfigInit } from "@aws-sdk/shared-ini-file-loader";
2-
import { ParsedIniData, SharedConfigFiles } from "@aws-sdk/types";
3-
4-
export interface SourceProfileInit extends SharedConfigInit {
5-
/**
6-
* The configuration profile to use.
7-
*/
8-
profile?: string;
9-
10-
/**
11-
* A promise that will be resolved with loaded and parsed credentials files.
12-
* Used to avoid loading shared config files multiple times.
13-
*
14-
* @internal
15-
*/
16-
loadedConfig?: Promise<SharedConfigFiles>;
17-
}
1+
import {
2+
parseKnownFiles as __parseKnownFiles,
3+
SourceProfileInit as __SourceProfileInit,
4+
} from "@aws-sdk/shared-ini-file-loader";
185

196
/**
20-
* Load profiles from credentials and config INI files and normalize them into a
21-
* single profile list.
22-
*
23-
* @internal
7+
* @deprecated Use SourceProfileInit from "@aws-sdk/shared-ini-file-loader" instead.
248
*/
25-
export const parseKnownFiles = async (init: SourceProfileInit): Promise<ParsedIniData> => {
26-
const { loadedConfig = loadSharedConfigFiles(init) } = init;
9+
export interface SourceProfileInit extends __SourceProfileInit {}
2710

28-
const parsedFiles = await loadedConfig;
29-
return {
30-
...parsedFiles.configFile,
31-
...parsedFiles.credentialsFile,
32-
};
33-
};
11+
/**
12+
* @deprecated Use parseKnownFiles from "@aws-sdk/shared-ini-file-loader" instead.
13+
*/
14+
export const parseKnownFiles = __parseKnownFiles;

0 commit comments

Comments
 (0)