| Conditions | 7 | 
| Total Lines | 78 | 
| Code Lines | 60 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package providers | ||
| 191 | func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) { | ||
| 192 | originHttpDo := httpDo | ||
| 193 | 	defer func() { httpDo = originHttpDo }() | ||
| 194 | 	rollback := utils.Memory("ALIBABA_CLOUD_CONFIG_FILE") | ||
| 195 | 	defer func() { | ||
| 196 | getHomePath = utils.GetHomePath | ||
| 197 | rollback() | ||
| 198 | }() | ||
| 199 | |||
| 200 | 	getHomePath = func() string { | ||
| 201 | return "" | ||
| 202 | } | ||
| 203 | provider, err := NewCLIProfileCredentialsProviderBuilder().Build() | ||
| 204 | assert.Nil(t, err) | ||
| 205 | _, err = provider.GetCredentials() | ||
| 206 | assert.EqualError(t, err, "cannot found home dir") | ||
| 207 | |||
| 208 | 	getHomePath = func() string { | ||
| 209 | return "/path/invalid/home/dir" | ||
| 210 | } | ||
| 211 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() | ||
| 212 | assert.Nil(t, err) | ||
| 213 | _, err = provider.GetCredentials() | ||
| 214 | assert.EqualError(t, err, "reading aliyun cli config from '/path/invalid/home/dir/.aliyun/config.json' failed open /path/invalid/home/dir/.aliyun/config.json: no such file or directory") | ||
| 215 | |||
| 216 | // testcase: specify credentials file with env | ||
| 217 | 	os.Setenv("ALIBABA_CLOUD_CONFIG_FILE", "/path/to/config.invalid") | ||
| 218 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() | ||
| 219 | assert.Nil(t, err) | ||
| 220 | _, err = provider.GetCredentials() | ||
| 221 | assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config.invalid' failed open /path/to/config.invalid: no such file or directory") | ||
| 222 | 	os.Unsetenv("ALIBABA_CLOUD_CONFIG_FILE") | ||
| 223 | |||
| 224 | 	getHomePath = func() string { | ||
| 225 | wd, _ := os.Getwd() | ||
| 226 | return path.Join(wd, "fixtures") | ||
| 227 | } | ||
| 228 | |||
| 229 | // get credentials by current profile | ||
| 230 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() | ||
| 231 | assert.Nil(t, err) | ||
| 232 | cc, err := provider.GetCredentials() | ||
| 233 | assert.Nil(t, err) | ||
| 234 | 	assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "cli_profile/static_ak"}, cc) | ||
| 235 | |||
| 236 | 	provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("inexist").Build() | ||
| 237 | assert.Nil(t, err) | ||
| 238 | _, err = provider.GetCredentials() | ||
| 239 | assert.EqualError(t, err, "unable to get profile with 'inexist'") | ||
| 240 | |||
| 241 | // The get_credentials_error profile is invalid | ||
| 242 | 	provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("get_credentials_error").Build() | ||
| 243 | assert.Nil(t, err) | ||
| 244 | _, err = provider.GetCredentials() | ||
| 245 | assert.Contains(t, err.Error(), "InvalidAccessKeyId.NotFound") | ||
| 246 | |||
| 247 | 	httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { | ||
| 248 | 		res = &httputil.Response{ | ||
| 249 | StatusCode: 200, | ||
| 250 | 			Body:       []byte(`{"Credentials": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}}`), | ||
| 251 | } | ||
| 252 | return | ||
| 253 | } | ||
| 254 | 	provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("ChainableRamRoleArn").Build() | ||
| 255 | assert.Nil(t, err) | ||
| 256 | cc, err = provider.GetCredentials() | ||
| 257 | assert.Nil(t, err) | ||
| 258 | assert.Equal(t, "akid", cc.AccessKeyId) | ||
| 259 | assert.Equal(t, "aksecret", cc.AccessKeySecret) | ||
| 260 | assert.Equal(t, "ststoken", cc.SecurityToken) | ||
| 261 | assert.Equal(t, "cli_profile/ram_role_arn/ram_role_arn/static_ak", cc.ProviderName) | ||
| 262 | |||
| 263 | provider.innerProvider = new(testProvider) | ||
| 264 | cc, err = provider.GetCredentials() | ||
| 265 | assert.Nil(t, err) | ||
| 266 | assert.Equal(t, "test", cc.AccessKeyId) | ||
| 267 | assert.Equal(t, "test", cc.AccessKeySecret) | ||
| 268 | assert.Equal(t, "cli_profile/test", cc.ProviderName) | ||
| 269 | } | ||
| 270 |