Skip to content

Commit 7354e94

Browse files
committed
Replace --averageTime option with --new-test-time
1 parent ed99e51 commit 7354e94

File tree

7 files changed

+230
-62
lines changed

7 files changed

+230
-62
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ java -jar split-tests-java.jar --split-index 0 --split-total 10 --glob 'project/
3838
```plain
3939
Usage: <main class> [options]
4040
Options:
41-
--averageTime, -a
42-
Use the average test time from tests with JUnit reports for tests
43-
without JUnit reports.
44-
Default: false
4541
--debug, -d
4642
Enables debug logging.
4743
Default: false
@@ -60,6 +56,11 @@ Usage: <main class> [options]
6056
--junit-glob, -j
6157
Glob pattern to find JUnit reports. Make sure to single-quote the
6258
pattern to avoid shell expansion.
59+
--new-test-time, -n
60+
Configures the calculation of the test time for tests without JUnit
61+
reports.
62+
Default: average
63+
Possible Values: [zero, average, min, max]
6364
* --split-index, -i
6465
This test split index.
6566
Default: 0

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ class Arguments {
3434
@Nullable String junitGlob;
3535

3636
@Parameter(names = {"--format", "-f"}, description = "The output format.", converter = FormatOptionConverter.class)
37-
@NotNull FormatOption format = FormatOption.LIST;
37+
@NotNull FormatOption formatOption = FormatOption.LIST;
3838

39+
@Deprecated
3940
@Parameter(names = {"--average-time", "-a"},
40-
description = "Use the average test time from tests with JUnit reports for tests without JUnit reports.")
41+
description = "This option is deprecated and should no longer be used. Use --newTestTimeOption instead.",
42+
hidden = true)
4143
boolean useAverageTimeForNewTests = false;
4244

45+
@Parameter(names = {"--new-test-time", "-n"},
46+
description = "Configures the calculation of the test time for tests without JUnit reports.",
47+
converter = NewTestTimeOptionConverter.class)
48+
@NotNull NewTestTimeOption newTestTimeOption = NewTestTimeOption.AVERAGE;
49+
4350
@Parameter(names = {"--working-directory", "-w"},
4451
description = "The working directory. Defaults to the current directory.")
4552
@Nullable Path workingDirectory;
@@ -57,4 +64,15 @@ public static class FormatOptionConverter implements IStringConverter<FormatOpti
5764
.orElseThrow();
5865
}
5966
}
67+
68+
public static class NewTestTimeOptionConverter implements IStringConverter<NewTestTimeOption> {
69+
70+
@Override
71+
public @NotNull NewTestTimeOption convert(final @NotNull String value) {
72+
return Arrays.stream(NewTestTimeOption.values())
73+
.filter(option -> option.toString().equals(value))
74+
.findFirst()
75+
.orElseThrow();
76+
}
77+
}
6078
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.donnerbart.split;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public enum NewTestTimeOption {
6+
7+
ZERO("zero"),
8+
AVERAGE("average"),
9+
MIN("min"),
10+
MAX("max");
11+
12+
private final @NotNull String parameterValue;
13+
14+
NewTestTimeOption(final @NotNull String parameterValue) {
15+
this.parameterValue = parameterValue;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return parameterValue;
21+
}
22+
}

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public class TestSplit {
4343
private final @NotNull String glob;
4444
private final @Nullable String excludeGlob;
4545
private final @Nullable String junitGlob;
46-
private final @NotNull FormatOption format;
47-
private final boolean useAverageTimeForNewTests;
46+
private final @NotNull FormatOption formatOption;
47+
private final @NotNull NewTestTimeOption newTestTimeOption;
4848
private final @NotNull Path workingDirectory;
4949
private final boolean debug;
5050
private final @NotNull Consumer<Integer> exitCodeConsumer;
@@ -55,8 +55,8 @@ public TestSplit(
5555
final @NotNull String glob,
5656
final @Nullable String excludeGlob,
5757
final @Nullable String junitGlob,
58-
final @NotNull FormatOption format,
59-
final boolean useAverageTimeForNewTests,
58+
final @NotNull FormatOption formatOption,
59+
final @NotNull NewTestTimeOption newTestTimeOption,
6060
final @NotNull Path workingDirectory,
6161
final boolean debug,
6262
final @NotNull Consumer<Integer> exitCodeConsumer) {
@@ -65,8 +65,8 @@ public TestSplit(
6565
this.glob = glob;
6666
this.excludeGlob = excludeGlob;
6767
this.junitGlob = junitGlob;
68-
this.format = format;
69-
this.useAverageTimeForNewTests = useAverageTimeForNewTests;
68+
this.formatOption = formatOption;
69+
this.newTestTimeOption = newTestTimeOption;
7070
this.workingDirectory = workingDirectory;
7171
this.debug = debug;
7272
this.exitCodeConsumer = exitCodeConsumer;
@@ -82,7 +82,7 @@ public TestSplit(
8282
if (junitGlob != null) {
8383
LOG.info("JUnit glob: {}", junitGlob);
8484
}
85-
LOG.info("Output format: {}", format);
85+
LOG.info("Output format: {}", formatOption);
8686
final var testPaths = getPaths(workingDirectory, glob, excludeGlob);
8787
final var classNames = fileToClassName(testPaths, exitCodeConsumer);
8888
if (classNames.isEmpty()) {
@@ -119,11 +119,11 @@ public TestSplit(
119119
}
120120
}
121121
// add tests without timing records
122-
final var newTestTime = getNewTestTime(useAverageTimeForNewTests, testCases);
122+
final var newTestTime = getNewTestTime(newTestTimeOption, testCases);
123123
classNames.forEach(className -> {
124124
final var testCase = new TestCase(className, newTestTime);
125125
if (testCases.add(testCase)) {
126-
LOG.debug("Adding test {}", testCase.name());
126+
LOG.debug("Adding test {} [estimated {}]", testCase.name(), formatTime(testCase.time()));
127127
}
128128
});
129129

@@ -164,7 +164,7 @@ public TestSplit(
164164
.stream()
165165
.sorted(Comparator.reverseOrder())
166166
.map(TestCase::name)
167-
.map(test -> switch (format) {
167+
.map(test -> switch (formatOption) {
168168
case LIST -> test;
169169
case GRADLE -> "--tests " + test;
170170
})
@@ -243,13 +243,29 @@ public TestSplit(
243243
}
244244

245245
private static double getNewTestTime(
246-
final boolean useAverageTimeForNewTests,
246+
final @NotNull NewTestTimeOption useAverageTimeForNewTests,
247247
final @NotNull Set<TestCase> testCases) {
248-
if (!useAverageTimeForNewTests || testCases.isEmpty()) {
248+
if (testCases.isEmpty()) {
249249
return 0d;
250250
}
251-
final var averageTime = testCases.stream().mapToDouble(TestCase::time).sum() / (double) testCases.size();
252-
LOG.info("Average test time is {}", formatTime(averageTime));
253-
return averageTime;
251+
return switch (useAverageTimeForNewTests) {
252+
case ZERO -> 0d;
253+
case AVERAGE -> {
254+
final var averageTime =
255+
testCases.stream().mapToDouble(TestCase::time).sum() / (double) testCases.size();
256+
LOG.info("Average test time is {}", formatTime(averageTime));
257+
yield averageTime;
258+
}
259+
case MIN -> {
260+
final var minTime = testCases.stream().mapToDouble(TestCase::time).min().orElseThrow();
261+
LOG.info("Minimum test time is {}", formatTime(minTime));
262+
yield minTime;
263+
}
264+
case MAX -> {
265+
final var maxTime = testCases.stream().mapToDouble(TestCase::time).max().orElseThrow();
266+
LOG.info("Maximum test time is {}", formatTime(maxTime));
267+
yield maxTime;
268+
}
269+
};
254270
}
255271
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public static void main(final @Nullable String @NotNull [] args) throws Exceptio
3939
arguments.glob,
4040
arguments.excludeGlob,
4141
arguments.junitGlob,
42-
arguments.format,
43-
arguments.useAverageTimeForNewTests,
42+
arguments.formatOption,
43+
arguments.newTestTimeOption,
4444
workingDirectory,
4545
arguments.debug,
4646
System::exit);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ void formatOptionConvert() {
1616
assertThat(converter.convert("gradle")).isEqualTo(FormatOption.GRADLE);
1717
assertThatThrownBy(() -> converter.convert("unknown")).isInstanceOf(NoSuchElementException.class);
1818
}
19+
20+
@Test
21+
void newTestTimeOptionConvert() {
22+
final var converter = new Arguments.NewTestTimeOptionConverter();
23+
assertThat(converter.convert("zero")).isEqualTo(NewTestTimeOption.ZERO);
24+
assertThat(converter.convert("average")).isEqualTo(NewTestTimeOption.AVERAGE);
25+
assertThat(converter.convert("min")).isEqualTo(NewTestTimeOption.MIN);
26+
assertThat(converter.convert("max")).isEqualTo(NewTestTimeOption.MAX);
27+
assertThatThrownBy(() -> converter.convert("unknown")).isInstanceOf(NoSuchElementException.class);
28+
}
1929
}

0 commit comments

Comments
 (0)