Commit f10286ca authored by Phillip Webb's avatar Phillip Webb Committed by Andy Wilkinson

Fix spring.profiles.default with profile enabled by configuration file

Previously, if the user configured a custom default profile and then
enabled another profile using a configuration file, the custom default
profile would be activated when it should not have been.

This commit updates ConfigFileApplicationListener so that when a
profile is activated via a configuration file, any profiles
that are queued purely because they are a default profile are removed
from the queue. This ensures that a default profile is not active
when another profile is activated via a configuration file.

Closes gh-5998
parent 05452310
......@@ -793,6 +793,40 @@ public class ConfigFileApplicationListenerTests {
assertThat(property).isEqualTo("frompropertiesfile");
}
@Test
public void customDefaultProfile() throws Exception {
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
this.context = application.run("--spring.profiles.default=customdefault");
String property = this.context.getEnvironment().getProperty("customdefault");
assertThat(property).isEqualTo("true");
}
@Test
public void customDefaultProfileAndActive() throws Exception {
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
this.context = application.run("--spring.profiles.default=customdefault",
"--spring.profiles.active=dev");
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdevpropertiesfile");
assertThat(this.context.getEnvironment().containsProperty("customdefault"))
.isFalse();
}
@Test
public void customDefaultProfileAndActiveFromFile() throws Exception {
// gh-5998
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
this.context = application.run("--spring.config.name=customprofile",
"--spring.profiles.default=customdefault");
ConfigurableEnvironment environment = this.context.getEnvironment();
assertThat(environment.containsProperty("customprofile")).isTrue();
assertThat(environment.containsProperty("customprofile-specific")).isTrue();
assertThat(environment.containsProperty("customprofile-customdefault")).isFalse();
}
private Condition<ConfigurableEnvironment> matchingPropertySource(
final String sourceName) {
return new Condition<ConfigurableEnvironment>(
......
spring.profiles.active=specific
customprofile=true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment