Skip to content

Commit 08b9e7b

Browse files
committed
Add --averageTime option
1 parent b5c50f5 commit 08b9e7b

File tree

4 files changed

+109
-17
lines changed

4 files changed

+109
-17
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ class Arguments {
4141
""", converter = FormatOptionConverter.class)
4242
@NotNull FormatOption format = FormatOption.LIST;
4343

44+
@Parameter(names = {"--averageTime", "-a"},
45+
description = "Use the average test time from tests with JUnit reports for tests without JUnit reports. Defaults to 'false'.")
46+
boolean useAverageTimeForNewTests = false;
47+
4448
@Parameter(names = {"--working-directory", "-w"},
4549
description = "The working directory. Defaults to the current directory.")
4650
@Nullable Path workingDirectory;
4751

48-
@Parameter(names = {"--debug", "-d"}, description = "Enables debug logging.")
52+
@Parameter(names = {"--debug", "-d"}, description = "Enables debug logging. Defaults to 'false'.")
4953
boolean debug = false;
5054

5155
public static class FormatOptionConverter implements IStringConverter<FormatOption> {

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class TestSplit {
4444
private final @Nullable String excludeGlob;
4545
private final @Nullable String junitGlob;
4646
private final @NotNull FormatOption format;
47+
private final boolean useAverageTimeForNewTests;
4748
private final @NotNull Path workingDirectory;
4849
private final boolean debug;
4950
private final @NotNull Consumer<Integer> exitCodeConsumer;
@@ -55,6 +56,7 @@ public TestSplit(
5556
final @Nullable String excludeGlob,
5657
final @Nullable String junitGlob,
5758
final @NotNull FormatOption format,
59+
final boolean useAverageTimeForNewTests,
5860
final @NotNull Path workingDirectory,
5961
final boolean debug,
6062
final @NotNull Consumer<Integer> exitCodeConsumer) {
@@ -64,6 +66,7 @@ public TestSplit(
6466
this.excludeGlob = excludeGlob;
6567
this.junitGlob = junitGlob;
6668
this.format = format;
69+
this.useAverageTimeForNewTests = useAverageTimeForNewTests;
6770
this.workingDirectory = workingDirectory;
6871
this.debug = debug;
6972
this.exitCodeConsumer = exitCodeConsumer;
@@ -116,8 +119,9 @@ public TestSplit(
116119
}
117120
}
118121
// add tests without timing records
122+
final var newTestTime = getNewTestTime(useAverageTimeForNewTests, testCases);
119123
classNames.forEach(className -> {
120-
final var testCase = new TestCase(className, 0d);
124+
final var testCase = new TestCase(className, newTestTime);
121125
if (testCases.add(testCase)) {
122126
LOG.debug("Adding test {}", testCase.name());
123127
}
@@ -235,4 +239,15 @@ public TestSplit(
235239
}
236240
return classNames;
237241
}
242+
243+
private static double getNewTestTime(
244+
final boolean useAverageTimeForNewTests,
245+
final @NotNull Set<TestCase> testCases) {
246+
if (!useAverageTimeForNewTests || testCases.isEmpty()) {
247+
return 0d;
248+
}
249+
final var averageTime = testCases.stream().mapToDouble(TestCase::time).sum() / (double) testCases.size();
250+
LOG.info("Average test time is {}", formatTime(averageTime));
251+
return averageTime;
252+
}
238253
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static void main(final @Nullable String @NotNull [] args) throws Exceptio
4040
arguments.excludeGlob,
4141
arguments.junitGlob,
4242
arguments.format,
43+
arguments.useAverageTimeForNewTests,
4344
workingDirectory,
4445
arguments.debug,
4546
System::exit);

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

+87-15
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void setUp() throws Exception {
7171

7272
@Test
7373
void run_withoutJUnit_withOneSplit() throws Exception {
74-
final var splits = splitTests(1, false);
74+
final var splits = splitTests(1, false, false);
7575
assertThat(splits).hasSize(1).containsExactly( //
7676
List.of("de.donnerbart.example.FastTest",
7777
"de.donnerbart.example.NoTimingOneTest",
@@ -83,7 +83,7 @@ void run_withoutJUnit_withOneSplit() throws Exception {
8383

8484
@Test
8585
void run_withoutJUnit_withTwoSplits() throws Exception {
86-
final var splits = splitTests(2, false);
86+
final var splits = splitTests(2, false, false);
8787
assertThat(splits).hasSize(2).containsExactly( //
8888
List.of("de.donnerbart.example.FastTest",
8989
"de.donnerbart.example.NoTimingTwoTest",
@@ -94,7 +94,7 @@ void run_withoutJUnit_withTwoSplits() throws Exception {
9494

9595
@Test
9696
void run_withoutJUnit_withThreeSplits() throws Exception {
97-
final var splits = splitTests(3, false);
97+
final var splits = splitTests(3, false, false);
9898
assertThat(splits).hasSize(3).containsExactly( //
9999
List.of("de.donnerbart.example.FastTest", "de.donnerbart.example.SlowTest"),
100100
List.of("de.donnerbart.example.NoTimingOneTest", "de.donnerbart.example.SlowestTest"),
@@ -104,7 +104,7 @@ void run_withoutJUnit_withThreeSplits() throws Exception {
104104

105105
@Test
106106
void run_withoutJUnit_withThreeSplits_withGradleFormat() throws Exception {
107-
final var splits = splitTests(3, false, "**/example-project/**/*Test.java", tmp, FormatOption.GRADLE);
107+
final var splits = splitTests(3, false, "**/example-project/**/*Test.java", false, tmp, FormatOption.GRADLE);
108108
assertThat(splits).hasSize(3).containsExactly( //
109109
List.of("--tests de.donnerbart.example.FastTest", "--tests de.donnerbart.example.SlowTest"),
110110
List.of("--tests de.donnerbart.example.NoTimingOneTest", "--tests de.donnerbart.example.SlowestTest"),
@@ -114,7 +114,7 @@ void run_withoutJUnit_withThreeSplits_withGradleFormat() throws Exception {
114114

115115
@Test
116116
void run_withJUnit_withOneSplit() throws Exception {
117-
final var splits = splitTests(1, true);
117+
final var splits = splitTests(1, true, false);
118118
assertThat(splits).hasSize(1).containsExactly( //
119119
List.of("de.donnerbart.example.SlowestTest",
120120
"de.donnerbart.example.SlowTest",
@@ -126,7 +126,7 @@ void run_withJUnit_withOneSplit() throws Exception {
126126

127127
@Test
128128
void run_withJUnit_withTwoSplits() throws Exception {
129-
final var splits = splitTests(2, true);
129+
final var splits = splitTests(2, true, false);
130130
assertThat(splits).hasSize(2).containsExactly( //
131131
List.of("de.donnerbart.example.SlowestTest"),
132132
List.of("de.donnerbart.example.SlowTest",
@@ -138,7 +138,7 @@ void run_withJUnit_withTwoSplits() throws Exception {
138138

139139
@Test
140140
void run_withJUnit_withThreeSplits() throws Exception {
141-
final var splits = splitTests(3, true);
141+
final var splits = splitTests(3, true, false);
142142
assertThat(splits).hasSize(3).containsExactly( //
143143
List.of("de.donnerbart.example.SlowestTest"),
144144
List.of("de.donnerbart.example.SlowTest"),
@@ -150,7 +150,7 @@ void run_withJUnit_withThreeSplits() throws Exception {
150150

151151
@Test
152152
void run_withJUnit_withFourSplits() throws Exception {
153-
final var splits = splitTests(4, true);
153+
final var splits = splitTests(4, true, false);
154154
assertThat(splits).hasSize(4).containsExactly( //
155155
List.of("de.donnerbart.example.SlowestTest"),
156156
List.of("de.donnerbart.example.SlowTest"),
@@ -159,6 +159,51 @@ void run_withJUnit_withFourSplits() throws Exception {
159159
assertThat(exitCode).hasNullValue();
160160
}
161161

162+
@Test
163+
void run_withJUnit_withAverageTestTime_withOneSplit() throws Exception {
164+
final var splits = splitTests(1, true, true);
165+
assertThat(splits).hasSize(1).containsExactly( //
166+
List.of("de.donnerbart.example.SlowestTest",
167+
"de.donnerbart.example.NoTimingOneTest",
168+
"de.donnerbart.example.NoTimingTwoTest",
169+
"de.donnerbart.example.SlowTest",
170+
"de.donnerbart.example.FastTest"));
171+
assertThat(exitCode).hasNullValue();
172+
}
173+
174+
@Test
175+
void run_withJUnit_withAverageTestTime_withTwoSplits() throws Exception {
176+
final var splits = splitTests(2, true, true);
177+
assertThat(splits).hasSize(2).containsExactly( //
178+
List.of("de.donnerbart.example.SlowestTest"),
179+
List.of("de.donnerbart.example.NoTimingOneTest",
180+
"de.donnerbart.example.NoTimingTwoTest",
181+
"de.donnerbart.example.SlowTest",
182+
"de.donnerbart.example.FastTest"));
183+
assertThat(exitCode).hasNullValue();
184+
}
185+
186+
@Test
187+
void run_withJUnit_withAverageTestTime_withThreeSplits() throws Exception {
188+
final var splits = splitTests(3, true, true);
189+
assertThat(splits).hasSize(3).containsExactly( //
190+
List.of("de.donnerbart.example.SlowestTest"),
191+
List.of("de.donnerbart.example.NoTimingOneTest", "de.donnerbart.example.SlowTest"),
192+
List.of("de.donnerbart.example.NoTimingTwoTest", "de.donnerbart.example.FastTest"));
193+
assertThat(exitCode).hasNullValue();
194+
}
195+
196+
@Test
197+
void run_withJUnit_withAverageTestTime_withFourSplits() throws Exception {
198+
final var splits = splitTests(4, true, true);
199+
assertThat(splits).hasSize(4).containsExactly( //
200+
List.of("de.donnerbart.example.SlowestTest"),
201+
List.of("de.donnerbart.example.NoTimingOneTest"),
202+
List.of("de.donnerbart.example.NoTimingTwoTest"),
203+
List.of("de.donnerbart.example.SlowTest", "de.donnerbart.example.FastTest"));
204+
assertThat(exitCode).hasNullValue();
205+
}
206+
162207
@Test
163208
void run_whitespaceClassDefinition() throws Exception {
164209
final var projectFolder =
@@ -174,6 +219,7 @@ void run_whitespaceClassDefinition() throws Exception {
174219
null,
175220
null,
176221
FormatOption.LIST,
222+
false,
177223
projectFolder,
178224
true,
179225
exitCode::set);
@@ -191,8 +237,12 @@ void run_thirdPartyLibrary() throws Exception {
191237
"ThirdPartyLibraryTest.java",
192238
PERMISSIONS);
193239

194-
final var splits =
195-
splitTests(1, false, "**/third-party-library-project/**/*Test.java", projectFolder, FormatOption.LIST);
240+
final var splits = splitTests(1,
241+
false,
242+
"**/third-party-library-project/**/*Test.java",
243+
false,
244+
projectFolder,
245+
FormatOption.LIST);
196246
assertThat(splits).hasSize(1).containsExactly(List.of("de.donnerbart.example.ThirdPartyLibraryTest"));
197247
assertThat(exitCode).hasNullValue();
198248
}
@@ -203,7 +253,7 @@ void run_noPackage() throws Exception {
203253
copyResourceToTarget(projectFolder, "tests/NoPackageTest.java", "NoPackageTest.java", PERMISSIONS);
204254

205255
final var splits =
206-
splitTests(1, false, "**/no-package-project/**/*Test.java", projectFolder, FormatOption.LIST);
256+
splitTests(1, false, "**/no-package-project/**/*Test.java", false, projectFolder, FormatOption.LIST);
207257
assertThat(splits).hasSize(1).containsExactly(List.of("NoPackageTest"));
208258
assertThat(exitCode).hasNullValue();
209259
}
@@ -213,7 +263,19 @@ void run_noTests() throws Exception {
213263
final var projectFolder = tmp.resolve("no-tests-project").resolve("src").resolve("main").resolve("java");
214264
Files.createDirectories(projectFolder);
215265

216-
final var splits = splitTests(1, false, "**/no-tests-project/**/*Test.java", projectFolder, FormatOption.LIST);
266+
final var splits =
267+
splitTests(1, false, "**/no-tests-project/**/*Test.java", false, projectFolder, FormatOption.LIST);
268+
assertThat(splits).hasSize(1).containsExactly(List.of());
269+
assertThat(exitCode).hasValue(1);
270+
}
271+
272+
@Test
273+
void run_noTests_withAverageTestTime() throws Exception {
274+
final var projectFolder = tmp.resolve("no-tests-project").resolve("src").resolve("main").resolve("java");
275+
Files.createDirectories(projectFolder);
276+
277+
final var splits =
278+
splitTests(1, false, "**/no-tests-project/**/*Test.java", true, projectFolder, FormatOption.LIST);
217279
assertThat(splits).hasSize(1).containsExactly(List.of());
218280
assertThat(exitCode).hasValue(1);
219281
}
@@ -224,19 +286,28 @@ void run_noClassName() throws Exception {
224286
copyResourceToTarget(projectFolder, "tests/NoClassNameTest.java", "NoClassNameTest.java", PERMISSIONS);
225287

226288
final var splits =
227-
splitTests(1, false, "**/no-classname-project/**/*Test.java", projectFolder, FormatOption.LIST);
289+
splitTests(1, false, "**/no-classname-project/**/*Test.java", false, projectFolder, FormatOption.LIST);
228290
assertThat(splits).hasSize(1).containsExactly(List.of());
229291
assertThat(exitCode).hasValue(1);
230292
}
231293

232-
private @NotNull List<List<String>> splitTests(final int splitTotal, final boolean withJUnit) throws Exception {
233-
return splitTests(splitTotal, withJUnit, "**/example-project/**/*Test.java", tmp, FormatOption.LIST);
294+
private @NotNull List<List<String>> splitTests(
295+
final int splitTotal,
296+
final boolean withJUnit,
297+
final boolean useAverageTimeForNewTests) throws Exception {
298+
return splitTests(splitTotal,
299+
withJUnit,
300+
"**/example-project/**/*Test.java",
301+
useAverageTimeForNewTests,
302+
tmp,
303+
FormatOption.LIST);
234304
}
235305

236306
private @NotNull List<List<String>> splitTests(
237307
final int splitTotal,
238308
final boolean withJUnit,
239309
final @NotNull String glob,
310+
final boolean useAverageTimeForNewTests,
240311
final @NotNull Path workingDir,
241312
final @NotNull FormatOption format) throws Exception {
242313
final var splits = new ArrayList<List<String>>();
@@ -247,6 +318,7 @@ void run_noClassName() throws Exception {
247318
"**/example-project/**/*Abstract*.java",
248319
withJUnit ? "**/junit-reports/*.xml" : null,
249320
format,
321+
useAverageTimeForNewTests,
250322
workingDir,
251323
true,
252324
exitCode::set);

0 commit comments

Comments
 (0)