1
1
import { CredentialsProviderError } from "@aws-sdk/property-provider" ;
2
- import { loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
2
+ import { getProfileName , loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
3
3
import { ParsedIniData , Profile } from "@aws-sdk/types" ;
4
4
5
- import { ENV_PROFILE , fromSharedConfigFiles , GetterFromConfig , SharedConfigInit } from "./fromSharedConfigFiles" ;
5
+ import { fromSharedConfigFiles , GetterFromConfig , SharedConfigInit } from "./fromSharedConfigFiles" ;
6
6
7
7
jest . mock ( "@aws-sdk/shared-ini-file-loader" , ( ) => ( {
8
+ getProfileName : jest . fn ( ) ,
8
9
loadSharedConfigFiles : jest . fn ( ) ,
9
10
} ) ) ;
10
11
11
12
describe ( "fromSharedConfigFiles" , ( ) => {
12
- const envProfile = process . env [ ENV_PROFILE ] ;
13
13
const configKey = "config_key" ;
14
14
const configGetter : GetterFromConfig < string > = ( profile : Profile ) => profile [ configKey ] ;
15
15
16
- beforeEach ( ( ) => {
17
- delete process . env [ ENV_PROFILE ] ;
18
- } ) ;
19
-
20
- afterAll ( ( ) => {
21
- process . env [ ENV_PROFILE ] = envProfile ;
22
- } ) ;
23
-
24
16
const getCredentialsProviderError = ( profile : string , getter : GetterFromConfig < string > ) =>
25
17
new CredentialsProviderError (
26
18
`Cannot load config for profile ${ profile } in SDK configuration files with getter: ${ getter } `
@@ -122,6 +114,7 @@ describe("fromSharedConfigFiles", () => {
122
114
configFile : iniDataInConfig ,
123
115
credentialsFile : iniDataInCredentials ,
124
116
} ) ;
117
+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ?? "default" ) ;
125
118
return expect ( fromSharedConfigFiles ( configGetter , { profile, preferredFile } ) ( ) ) . resolves . toBe (
126
119
configValueToVerify
127
120
) ;
@@ -135,6 +128,7 @@ describe("fromSharedConfigFiles", () => {
135
128
configFile : iniDataInConfig ,
136
129
credentialsFile : iniDataInCredentials ,
137
130
} ) ;
131
+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ?? "default" ) ;
138
132
return expect ( fromSharedConfigFiles ( configGetter , { profile, preferredFile } ) ( ) ) . rejects . toMatchObject (
139
133
getCredentialsProviderError ( profile ?? "default" , configGetter )
140
134
) ;
@@ -165,24 +159,15 @@ describe("fromSharedConfigFiles", () => {
165
159
} ,
166
160
} ;
167
161
168
- describe ( "when profile is not defined" , ( ) => {
169
- beforeEach ( ( ) => {
170
- ( loadSharedConfigFiles as jest . Mock ) . mockResolvedValueOnce ( loadedConfigData ) ;
171
- } ) ;
172
-
173
- it ( `returns configValue from value in '${ ENV_PROFILE } ' env var if it is set` , ( ) => {
174
- const profile = "foo" ;
175
- process . env [ ENV_PROFILE ] = profile ;
176
- return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
177
- loadedConfigData . configFile [ profile ] [ configKey ]
178
- ) ;
179
- } ) ;
162
+ beforeEach ( ( ) => {
163
+ ( loadSharedConfigFiles as jest . Mock ) . mockResolvedValueOnce ( loadedConfigData ) ;
164
+ } ) ;
180
165
181
- it ( `returns configValue from default profile if ' ${ ENV_PROFILE } ' env var is not set` , ( ) => {
182
- return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
183
- loadedConfigData . configFile . default [ configKey ]
184
- ) ;
185
- } ) ;
166
+ it . each ( [ "foo" , " default" ] ) ( "returns config value from %s profile" , ( profile ) => {
167
+ ( getProfileName as jest . Mock ) . mockReturnValueOnce ( profile ) ;
168
+ return expect ( fromSharedConfigFiles ( configGetter ) ( ) ) . resolves . toBe (
169
+ loadedConfigData . configFile [ profile ] [ configKey ]
170
+ ) ;
186
171
} ) ;
187
172
} ) ;
188
173
} ) ;
0 commit comments