Skip to content

Commit

Permalink
Fixes an issue when env var and parent/default env values are same (#83)
Browse files Browse the repository at this point in the history
## 1.2.0

### Updated

- Bump slf4j-api from 1.7.36 to 2.0.0

### Fixed

- Fixes an issue when env var and parent/default env values are same
- Fixes an issue with flaky unit tests
  • Loading branch information
haroon-sheikh authored Sep 15, 2022
1 parent 3a44462 commit 7003c1b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.0

### Updated

- Bump slf4j-api from 1.7.36 to 2.0.0

### Fixed

- Fixes an issue when env var and parent/default env values are same
- Fixes an issue with flaky unit tests

## 1.1.1

### Added
Expand Down
File renamed without changes.
Empty file added config/empty-env/empty.txt
Empty file.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.sitture</groupId>
<artifactId>env-config</artifactId>
<packaging>jar</packaging>
<version>1.1.1</version>
<version>1.2.0</version>

<name>env-config</name>
<description>A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.</description>
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/github/sitture/envconfig/EnvConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class EnvConfigLoader {

private static final Logger LOG = LoggerFactory.getLogger(EnvConfigLoader.class);
private static final int MIN_ENVIRONMENTS = 1;
protected final CompositeConfiguration configuration = new CompositeConfiguration();
protected final EnvConfigProperties configProperties = new EnvConfigProperties();

Expand Down Expand Up @@ -47,13 +48,19 @@ private void loadEnvConfigurations(final List<String> envs) {
LOG.debug("Loading properties from system.properties");
this.configuration.addConfiguration(envVars.getSystemConfiguration());
final Configuration envOverrides = envVars.getEnvironmentConfiguration();
final EnvConfigFileList fileList = new EnvConfigFileList(configProperties.getConfigPath(EnvConfigUtils.CONFIG_ENV_DEFAULT));
if (envs.size() > MIN_ENVIRONMENTS) {
envs.stream().filter(env -> !env.equals(EnvConfigUtils.CONFIG_ENV_DEFAULT)).forEach(env -> processEnvOverrides(envOverrides, env));
}
LOG.debug("Loading properties from system.env");
this.configuration.addConfiguration(envOverrides);
}

private void processEnvOverrides(final Configuration envOverrides, final String env) {
try {
for (final File file : fileList.listFiles()) {
final Configuration defaultConfig = new Configurations().properties(file);
defaultConfig.getKeys().forEachRemaining(property -> {
if (envs.size() > 2 && envOverrides.containsKey(property)
&& envOverrides.getProperty(property).equals(defaultConfig.getProperty(property))) {
for (final File file : new EnvConfigFileList(configProperties.getConfigPath(env)).listFiles()) {
final Configuration properties = new Configurations().properties(file);
properties.getKeys().forEachRemaining(property -> {
if (envOverrides.containsKey(property) && envOverrides.getProperty(property).equals(properties.getProperty(property))) {
envOverrides.clearProperty(property);
}
});
Expand All @@ -63,8 +70,6 @@ private void loadEnvConfigurations(final List<String> envs) {
LOG.debug("Could not load configuration files. \n {}", e.getMessage());
}
}
LOG.debug("Loading properties from system.env");
this.configuration.addConfiguration(envOverrides);
}

private void loadFileConfigurations(final EnvConfigFileList fileList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ void testCanGetFromDefaultProfileWhenMultipleEnvs() {
@Test
void testThrowsExceptionWhenNoPropertiesInProfile() {
// given env is default and empty-profile exists in env properties
setEnvironment("empty-env");
setEnvironment("default");
// when an and empty-profile directory does not contain any valid properties files
setProfile("empty-profile");
// then an exception is thrown
final EnvConfigException exception = Assertions.assertThrows(EnvConfigException.class,
() -> EnvConfig.getOrThrow("non.existing"));
Assertions.assertTrue(exception.getMessage().startsWith("No property files found under"), exception.getMessage());
Assertions.assertTrue(exception.getMessage().endsWith("/env-config/config/empty-env/empty-profile'"));
Assertions.assertTrue(exception.getMessage().endsWith("/env-config/config/default/empty-profile'"));
}

private void setEnvironment(final String environment) {
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/com/github/sitture/envconfig/EnvConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ void testCanGetEntryWhenEnvVarAndDefaultValueSameWithParentEnv() {
// and does not exist in test-env
// and exists in default env with same value as env var
// and exists in test env with different value
// then value in test env takes priority
Assertions.assertEquals("test", EnvConfig.get("property.five"));
// then value from env variable takes priority
Assertions.assertEquals("default", EnvConfig.get("property.five"));
}

@Test
Expand Down Expand Up @@ -189,6 +189,18 @@ void testCanGetEntryWhenEnvVarSetInMultiEnvs() {
Assertions.assertEquals("test", EnvConfig.get("property.seven"));
}

@Test
void testCanGetEntryWhenEnvVarAndDefaultValueSameWithMultipleEnv() {
final String key = "property.one";
// when property.env is set in default env
// and PROPERTY_ENV is set as env var to same value as default
environmentVariables.set("PROPERTY_ONE", "default");
// and property.one is also set in test-env and test env properties
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test,test-env");
// then value from env variable takes priority
Assertions.assertEquals("default", EnvConfig.get(key));
}

@Test
void testCanGetPropertyFromEnvVars() {
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, TEST_ENVIRONMENT);
Expand Down

0 comments on commit 7003c1b

Please sign in to comment.