Skip to content

Commit 3a87bb5

Browse files
committed
Add output format option
1 parent 07c1024 commit 3a87bb5

File tree

7 files changed

+164
-146
lines changed

7 files changed

+164
-146
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package de.donnerbart.split;
22

3+
import com.beust.jcommander.IStringConverter;
34
import com.beust.jcommander.Parameter;
45
import org.jetbrains.annotations.NotNull;
56
import org.jetbrains.annotations.Nullable;
67

78
import java.nio.file.Path;
9+
import java.util.Arrays;
810

911
class Arguments {
1012

@@ -31,10 +33,29 @@ class Arguments {
3133
description = "Glob pattern to find JUnit reports. Make sure to single-quote the pattern to avoid shell expansion.")
3234
@Nullable String junitGlob;
3335

36+
@Parameter(names = {"--format", "-f"}, description = """
37+
The output format:
38+
* list: prints a space delimited list.
39+
* gradle: adds a '--tests' filter for each test.
40+
Defaults to 'list'.
41+
""", converter = FormatOptionConverter.class)
42+
@NotNull FormatOption format = FormatOption.LIST;
43+
3444
@Parameter(names = {"--working-directory", "-w"},
3545
description = "The working directory. Defaults to the current directory.")
3646
@Nullable Path workingDirectory;
3747

3848
@Parameter(names = {"--debug", "-d"}, description = "Enables debug logging.")
39-
boolean debug;
49+
boolean debug = false;
50+
51+
public static class FormatOptionConverter implements IStringConverter<FormatOption> {
52+
53+
@Override
54+
public @NotNull FormatOption convert(final @NotNull String value) {
55+
return Arrays.stream(FormatOption.values())
56+
.filter(option -> option.getParameterValue().equals(value))
57+
.findFirst()
58+
.orElseThrow();
59+
}
60+
}
4061
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.donnerbart.split;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public enum FormatOption {
6+
7+
LIST("list"),
8+
GRADLE("gradle");
9+
10+
private final @NotNull String parameterValue;
11+
12+
FormatOption(final @NotNull String parameterValue) {
13+
this.parameterValue = parameterValue;
14+
}
15+
16+
public @NotNull String getParameterValue() {
17+
return parameterValue;
18+
}
19+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.Comparator;
2525
import java.util.HashSet;
26+
import java.util.List;
2627
import java.util.Set;
2728
import java.util.function.Consumer;
2829
import java.util.stream.Collectors;
@@ -42,6 +43,7 @@ public class TestSplit {
4243
private final @NotNull String glob;
4344
private final @Nullable String excludeGlob;
4445
private final @Nullable String junitGlob;
46+
private final @NotNull FormatOption format;
4547
private final @NotNull Path workingDirectory;
4648
private final boolean debug;
4749
private final @NotNull Consumer<Integer> exitCodeConsumer;
@@ -52,6 +54,7 @@ public TestSplit(
5254
final @NotNull String glob,
5355
final @Nullable String excludeGlob,
5456
final @Nullable String junitGlob,
57+
final @NotNull FormatOption format,
5558
final @NotNull Path workingDirectory,
5659
final boolean debug,
5760
final @NotNull Consumer<Integer> exitCodeConsumer) {
@@ -60,12 +63,13 @@ public TestSplit(
6063
this.glob = glob;
6164
this.excludeGlob = excludeGlob;
6265
this.junitGlob = junitGlob;
66+
this.format = format;
6367
this.workingDirectory = workingDirectory;
6468
this.debug = debug;
6569
this.exitCodeConsumer = exitCodeConsumer;
6670
}
6771

68-
public @NotNull String run() throws Exception {
72+
public @NotNull List<String> run() throws Exception {
6973
LOG.info("Split index {} (total: {})", splitIndex, splitTotal);
7074
LOG.info("Working directory: {}", workingDirectory);
7175
LOG.info("Glob: {}", glob);
@@ -75,6 +79,7 @@ public TestSplit(
7579
if (junitGlob != null) {
7680
LOG.info("JUnit glob: {}", junitGlob);
7781
}
82+
LOG.info("Output format: {}", format.getParameterValue());
7883
final var testPaths = getPaths(workingDirectory, glob, excludeGlob);
7984
final var classNames = fileToClassName(testPaths, exitCodeConsumer);
8085
if (classNames.isEmpty()) {
@@ -153,7 +158,11 @@ public TestSplit(
153158
.stream()
154159
.sorted(Comparator.reverseOrder())
155160
.map(TestCase::name)
156-
.collect(Collectors.joining(" "));
161+
.map(test -> switch (format) {
162+
case LIST -> test;
163+
case GRADLE -> "--tests " + test;
164+
})
165+
.collect(Collectors.toList());
157166
}
158167

159168
private static @NotNull Set<Path> getPaths(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ public static void main(final @Nullable String @NotNull [] args) throws Exceptio
3939
arguments.glob,
4040
arguments.excludeGlob,
4141
arguments.junitGlob,
42+
arguments.format,
4243
workingDirectory,
4344
arguments.debug,
4445
System::exit);
45-
System.out.print(testSplit.run());
46+
System.out.print(String.join(" ", testSplit.run()));
4647
}
4748

4849
@VisibleForTesting
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.donnerbart.split;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.NoSuchElementException;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9+
10+
class ArgumentsTest {
11+
12+
@Test
13+
void formatOptionConvert() {
14+
final var converter = new Arguments.FormatOptionConverter();
15+
assertThat(converter.convert("list")).isEqualTo(FormatOption.LIST);
16+
assertThat(converter.convert("gradle")).isEqualTo(FormatOption.GRADLE);
17+
assertThatThrownBy(() -> converter.convert("unknown")).isInstanceOf(NoSuchElementException.class);
18+
}
19+
}

0 commit comments

Comments
 (0)