Skip to content

Commit b5422d8

Browse files
authored
Client configuration should prefer system properties (#775)
1 parent 0c65bfb commit b5422d8

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

build-info-extractor/src/main/java/org/jfrog/build/extractor/BuildInfoExtractorUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ public abstract class BuildInfoExtractorUtils {
7575

7676
public static Properties mergePropertiesWithSystemAndPropertyFile(Properties existingProps, Log log) {
7777
Properties mergedProps = new Properties();
78-
mergedProps.putAll(addSystemProperties(existingProps));
78+
// Add properties from file.
7979
mergedProps.putAll(searchAdditionalPropertiesFile(mergedProps, log));
80+
// Merge properties from system properties - should be second to override any properties defined in the file.
81+
mergedProps.putAll(addSystemProperties(existingProps));
8082
return mergedProps;
8183
}
8284

build-info-extractor/src/test/java/org/jfrog/build/extractor/BuildExtractorUtilsTest.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Field;
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25+
import java.util.Arrays;
2526
import java.util.Base64;
2627
import java.util.Map;
2728
import java.util.Properties;
@@ -48,6 +49,8 @@
4849
public class BuildExtractorUtilsTest {
4950
private static final String POPO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "popo";
5051
private static final String MOMO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "momo";
52+
private static final String KOKO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "koko";
53+
private static final String GOGO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "gogo";
5154
private static final String ENV_POPO_KEY = BuildInfoProperties.BUILD_INFO_ENVIRONMENT_PREFIX + "popo";
5255
private static final String ENV_MOMO_KEY = BuildInfoProperties.BUILD_INFO_ENVIRONMENT_PREFIX + "momo";
5356

@@ -67,8 +70,6 @@ public void getBuildInfoPropertiesFromSystemProps() {
6770
assertEquals(props.size(), 2, "there should only be 2 properties after the filtering");
6871
assertEquals(props.getProperty(POPO_KEY), "buildname", "popo property does not match");
6972
assertEquals(props.getProperty(MOMO_KEY), "1", "momo property does not match");
70-
System.clearProperty(POPO_KEY);
71-
System.clearProperty(MOMO_KEY);
7273
}
7374

7475
public void getBuildInfoPropertiesFromFile() throws IOException {
@@ -87,10 +88,31 @@ public void getBuildInfoPropertiesFromFile() throws IOException {
8788
assertEquals(fileProps.getProperty(MOMO_KEY), "1", "momo property does not match");
8889
}
8990

91+
public void getBuildInfoPropertiesFromFileAndSystemProps() throws IOException {
92+
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
93+
createProperties().store(fileOutputStream, "");
94+
}
95+
96+
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
97+
System.setProperty(KOKO_KEY, "3");
98+
// Override MOMO_KEY from file
99+
System.setProperty(MOMO_KEY, "2");
100+
101+
Properties fileProps = filterDynamicProperties(
102+
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
103+
BUILD_INFO_PROP_PREDICATE);
104+
105+
assertEquals(fileProps.size(), 3, "there should only be 2 properties after the filtering");
106+
assertEquals(fileProps.getProperty(POPO_KEY), "buildname", "popo property does not match");
107+
assertEquals(fileProps.getProperty(MOMO_KEY), "2", "momo property does not match");
108+
assertEquals(fileProps.getProperty(KOKO_KEY), "3", "koko property does not match");
109+
}
110+
90111
@AfterMethod
91112
private void tearDown() throws Exception {
92113
Files.deleteIfExists(tempFile);
93114

115+
Arrays.asList(POPO_KEY, MOMO_KEY, KOKO_KEY, GOGO_KEY).forEach(System::clearProperty);
94116
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE);
95117
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY);
96118
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY_IV);
@@ -103,10 +125,8 @@ public void getBuildInfoProperties() throws IOException {
103125
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
104126

105127
// Put system properties
106-
String kokoKey = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "koko";
107-
String gogoKey = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "gogo";
108-
System.setProperty(kokoKey, "parent");
109-
System.setProperty(gogoKey, "2");
128+
System.setProperty(KOKO_KEY, "parent");
129+
System.setProperty(GOGO_KEY, "2");
110130

111131
Properties buildInfoProperties = filterDynamicProperties(
112132
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
@@ -115,11 +135,8 @@ public void getBuildInfoProperties() throws IOException {
115135
assertEquals(buildInfoProperties.size(), 4, "There should be 4 properties");
116136
assertEquals(buildInfoProperties.getProperty(POPO_KEY), "buildname", "popo property does not match");
117137
assertEquals(buildInfoProperties.getProperty(MOMO_KEY), "1", "momo number property does not match");
118-
assertEquals(buildInfoProperties.getProperty(kokoKey), "parent", "koko parent name property does not match");
119-
assertEquals(buildInfoProperties.getProperty(gogoKey), "2", "gogo parent number property does not match");
120-
121-
System.clearProperty(kokoKey);
122-
System.clearProperty(gogoKey);
138+
assertEquals(buildInfoProperties.getProperty(KOKO_KEY), "parent", "koko parent name property does not match");
139+
assertEquals(buildInfoProperties.getProperty(GOGO_KEY), "2", "gogo parent number property does not match");
123140
}
124141

125142
public void getEnvPropertiesFromFile() throws IOException {

0 commit comments

Comments
 (0)