diff --git a/source/aws_profile.c b/source/aws_profile.c index 3e25536..0446a7a 100644 --- a/source/aws_profile.c +++ b/source/aws_profile.c @@ -1541,14 +1541,19 @@ struct aws_string *aws_get_config_file_path( AWS_STATIC_STRING_FROM_LITERAL(s_default_profile_env_variable_name, "AWS_PROFILE"); struct aws_string *aws_get_profile_name(struct aws_allocator *allocator, const struct aws_byte_cursor *override_name) { - + /** + * Profile name is resolved in the following order. + * 1. If the override_path variable is provided. + * 2. Check `AWS_PROFILE` environment variable and use the value if it is not empty. + * 3. Use "default". */ struct aws_string *profile_name = NULL; - - if (aws_get_environment_value(allocator, s_default_profile_env_variable_name, &profile_name) || - profile_name == NULL) { - if (override_name != NULL && override_name->ptr != NULL) { - profile_name = aws_string_new_from_array(allocator, override_name->ptr, override_name->len); - } else { + if (override_name != NULL && override_name->ptr != NULL) { + profile_name = aws_string_new_from_array(allocator, override_name->ptr, override_name->len); + } else { + /* Try to fetch profile from AWS_PROFILE environment variable */ + aws_get_environment_value(allocator, s_default_profile_env_variable_name, &profile_name); + /* Use default profile if it doesn't exist. */ + if (profile_name == NULL) { profile_name = aws_string_new_from_string(allocator, s_default_profile_name); } } diff --git a/tests/aws_profile_tests.c b/tests/aws_profile_tests.c index ec6d4b3..7f18221 100644 --- a/tests/aws_profile_tests.c +++ b/tests/aws_profile_tests.c @@ -1447,9 +1447,8 @@ AWS_STATIC_STRING_FROM_LITERAL(s_profile_override, "NotTheDefault"); static int s_profile_override_test(struct aws_allocator *allocator, void *ctx) { (void)ctx; - - /* Make sure the environment doesn't affect this test */ - aws_unset_environment_value(s_profile_env_var); + /* The envrionment value should only override the default when user not set one */ + aws_set_environment_value(s_profile_env_var, s_profile_override); struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_profile_override); struct aws_string *profile_name = aws_get_profile_name(allocator, &override_cursor);