Skip to content

Commit 520fcba

Browse files
committed
Log version and git properties at startup
1 parent 0b56c87 commit 520fcba

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
application
33
java
4+
alias(libs.plugins.gradle.git.properties)
45
alias(libs.plugins.shadow)
56
}
67

@@ -34,6 +35,7 @@ dependencies {
3435
// ********** distribution **********
3536

3637
tasks.shadowJar {
38+
dependsOn(tasks.generateGitProperties)
3739
mergeServiceFiles()
3840
archiveBaseName = "split-tests-java"
3941
archiveClassifier = ""
@@ -59,3 +61,16 @@ dependencies {
5961
tasks.test {
6062
useJUnitPlatform()
6163
}
64+
65+
// ********** git properties **********
66+
67+
gitProperties {
68+
dotGitDirectory = project.rootProject.layout.projectDirectory.dir(".git")
69+
gitPropertiesName = "split-tests-java.properties"
70+
keys = listOf("git.branch", "git.commit.id", "git.commit.id.abbrev", "git.commit.time")
71+
customProperty("version", version)
72+
extProperty = "gitProps"
73+
}
74+
tasks.generateGitProperties {
75+
outputs.upToDateWhen { false }
76+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher
2020
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
2121

2222
[plugins]
23+
gradle-git-properties = { id = "com.gorylenko.gradle-git-properties", version = "2.5.0" }
2324
shadow = { id = "com.gradleup.shadow", version = "8.3.6" }

src/main/java/de/donnerbart/split/TestSplitMain.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15+
import java.text.SimpleDateFormat;
16+
import java.time.Instant;
17+
import java.util.Date;
1518
import java.util.Objects;
19+
import java.util.Properties;
1620
import java.util.function.Consumer;
1721

1822
import static de.donnerbart.split.util.FormatUtil.formatTime;
@@ -46,6 +50,12 @@ public static void main(final @Nullable String @NotNull [] args) throws Exceptio
4650
if (!validateArguments(arguments, workingDirectory)) {
4751
exitConsumer.accept(1);
4852
}
53+
final var properties = readProperties("split-tests-java.properties");
54+
LOG.info("split-tests-java {} (commit: {} on branch: {}) built on {}",
55+
properties.getProperty("version", "unknown"),
56+
properties.getProperty("git.commit.id.abbrev", "unknown"),
57+
properties.getProperty("git.branch", "unknown"),
58+
getBuiltTime(properties.getProperty("git.commit.time", "unknown")));
4959
LOG.info("Split index {} (total: {})", arguments.splitIndex, arguments.splitTotal);
5060
LOG.info("Working directory: {}", workingDirectory);
5161
LOG.info("Glob: {}", arguments.glob);
@@ -140,4 +150,24 @@ static int calculateOptimalTotalSplit(final @NotNull Arguments arguments, final
140150
lastSlowestSplit = slowestSplit;
141151
}
142152
}
153+
154+
@VisibleForTesting
155+
static @NotNull Properties readProperties(final @NotNull String resourceFile) throws Exception {
156+
final var properties = new Properties();
157+
try (final var inputStream = TestSplitMain.class.getClassLoader().getResourceAsStream(resourceFile)) {
158+
if (inputStream != null) {
159+
properties.load(inputStream);
160+
}
161+
}
162+
return properties;
163+
}
164+
165+
@VisibleForTesting
166+
static @NotNull Date getBuiltTime(final @NotNull String dateString) {
167+
try {
168+
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
169+
} catch (final Exception e) {
170+
return Date.from(Instant.EPOCH);
171+
}
172+
}
143173
}

src/test/java/de/donnerbart/split/TestSplitMainTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.nio.file.attribute.PosixFilePermission;
12+
import java.time.Instant;
13+
import java.util.Date;
1214
import java.util.Set;
1315
import java.util.concurrent.atomic.AtomicReference;
1416

@@ -128,7 +130,18 @@ void calculateOptimalTotalSplit_withMaxCalculations() throws Exception {
128130
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
129131
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);
130132

131-
jCommander.parse("-i", "0", "-t", "4", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml", "-n", "max", "-m", "5");
133+
jCommander.parse("-i",
134+
"0",
135+
"-t",
136+
"4",
137+
"-g",
138+
"**/*Test.java",
139+
"-j",
140+
"**/junit-reports/*.xml",
141+
"-n",
142+
"max",
143+
"-m",
144+
"5");
132145
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(4);
133146
}
134147

@@ -137,7 +150,18 @@ void calculateOptimalTotalSplit_withTooLowMaxCalculations() throws Exception {
137150
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
138151
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);
139152

140-
jCommander.parse("-i", "0", "-t", "1", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml", "-n", "max", "-m", "4");
153+
jCommander.parse("-i",
154+
"0",
155+
"-t",
156+
"1",
157+
"-g",
158+
"**/*Test.java",
159+
"-j",
160+
"**/junit-reports/*.xml",
161+
"-n",
162+
"max",
163+
"-m",
164+
"4");
141165
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(0);
142166
}
143167

@@ -158,4 +182,32 @@ void calculateOptimalTotalSplit_withInvalidSplitIndex() throws Exception {
158182
jCommander.parse("-i", "1", "-t", "1", "-g", "**/*Test.java", "-j", "**/junit-reports/*.xml");
159183
assertThat(TestSplitMain.calculateOptimalTotalSplit(arguments, tmp)).isEqualTo(0);
160184
}
185+
186+
@Test
187+
void readProperties() throws Exception {
188+
final var properties = TestSplitMain.readProperties("split-tests-java-test.properties");
189+
assertThat(properties).isNotNull();
190+
assertThat(properties.getProperty("git.commit.time")).isEqualTo("2023-01-01T00:01:02+0000");
191+
assertThat(properties.getProperty("git.commit.id")).isEqualTo("4c204731e327bc2e06d2a1b02f46e4195c210d0e");
192+
assertThat(properties.getProperty("git.commit.id.abbrev")).isEqualTo("4c20473");
193+
assertThat(properties.getProperty("git.branch")).isEqualTo("master");
194+
assertThat(properties.getProperty("version")).isEqualTo("1.2.3-SNAPSHOT");
195+
}
196+
197+
@Test
198+
void readProperties_whenFileNotFound() throws Exception {
199+
final var properties = TestSplitMain.readProperties("not-found.properties");
200+
assertThat(properties).isNotNull();
201+
assertThat(properties).isEmpty();
202+
}
203+
204+
@Test
205+
void getBuiltTime() {
206+
assertThat(TestSplitMain.getBuiltTime("2023-01-01T00:01:02+0000")).isEqualTo("2023-01-01T00:01:02+0000");
207+
}
208+
209+
@Test
210+
void getBuiltTime_whenTimeNotParseable() {
211+
assertThat(TestSplitMain.getBuiltTime("")).isEqualTo(Date.from(Instant.EPOCH));
212+
}
161213
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version=1.2.3-SNAPSHOT
2+
git.branch=master
3+
git.commit.id=4c204731e327bc2e06d2a1b02f46e4195c210d0e
4+
git.commit.id.abbrev=4c20473
5+
git.commit.time=2023-01-01T00\:01\:02+0000

0 commit comments

Comments
 (0)