Skip to content

Commit c138e92

Browse files
committed
Fix profile test so that it doesn't pick up the credentials file if the config file is missing.
(And porting to env-var helper so we properly clean up the settings)
1 parent 8d8fae0 commit c138e92

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

core/src/test/java/software/amazon/awssdk/core/regions/providers/AwsProfileRegionProviderTest.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,30 @@
1919

2020
import java.net.URISyntaxException;
2121
import java.nio.file.Paths;
22-
import org.junit.After;
23-
import org.junit.Before;
22+
import org.junit.Rule;
2423
import org.junit.Test;
2524
import software.amazon.awssdk.core.AwsSystemSetting;
2625
import software.amazon.awssdk.core.regions.Region;
26+
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;
2727

2828
public class AwsProfileRegionProviderTest {
29-
private String initialDefaultProfile;
30-
private String initialProfileLocation;
3129

32-
@Before
33-
public void setup() {
34-
this.initialDefaultProfile = AwsSystemSetting.AWS_PROFILE.getStringValue().orElse(null);
35-
this.initialProfileLocation = AwsSystemSetting.AWS_CONFIG_FILE.getStringValue().orElse(null);
36-
}
37-
38-
@After
39-
public void teardown() {
40-
if (initialDefaultProfile == null) {
41-
System.clearProperty(AwsSystemSetting.AWS_PROFILE.property());
42-
} else {
43-
System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), initialDefaultProfile);
44-
}
45-
46-
if (initialProfileLocation == null) {
47-
System.clearProperty(AwsSystemSetting.AWS_CONFIG_FILE.property());
48-
} else {
49-
System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), initialProfileLocation);
50-
}
51-
}
30+
@Rule
31+
public EnvironmentVariableHelper settingsHelper = new EnvironmentVariableHelper();
5232

5333
@Test
5434
public void nonExistentDefaultConfigFile_ReturnsNull() {
55-
System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), "/var/tmp/this/is/invalid.txt");
35+
settingsHelper.set(AwsSystemSetting.AWS_CONFIG_FILE, "/var/tmp/this/is/invalid.txt");
36+
settingsHelper.set(AwsSystemSetting.AWS_SHARED_CREDENTIALS_FILE, "/var/tmp/this/is/also.invalid.txt");
5637
assertThat(new AwsProfileRegionProvider().getRegion()).isNull();
5738
}
5839

5940
@Test
6041
public void profilePresentAndRegionIsSet_ProvidesCorrectRegion() throws URISyntaxException {
6142
String testFile = "/resources/profileconfig/test-profiles.tst";
62-
System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), "test");
63-
System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(),
64-
Paths.get(getClass().getResource(testFile).toURI()).toString());
43+
44+
settingsHelper.set(AwsSystemSetting.AWS_PROFILE, "test");
45+
settingsHelper.set(AwsSystemSetting.AWS_CONFIG_FILE, Paths.get(getClass().getResource(testFile).toURI()).toString());
6546
assertThat(new AwsProfileRegionProvider().getRegion()).isEqualTo(Region.of("saa"));
6647
}
6748
}

0 commit comments

Comments
 (0)