Skip to content

Commit ee286b7

Browse files
committed
refactor: move test runners and baseclasses to testlib
1 parent 49d9b2e commit ee286b7

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

testlib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ plugins {
55
dependencies {
66
implementation libs.bundles.test.libs
77
implementation libs.bundles.durian.libs
8+
implementation libs.picocli
89
}

app/src/test/java/com/diffplug/spotless/cli/CLIIntegrationHarness.java renamed to testlib/src/main/java/com/diffplug/spotless/cli/CLIIntegrationHarness.java

File renamed without changes.

app/src/test/java/com/diffplug/spotless/cli/SpotlessCLIRunner.java renamed to testlib/src/main/java/com/diffplug/spotless/cli/SpotlessCLIRunner.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.annotations.Nullable;
2525

26-
import com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep;
26+
import com.diffplug.spotless.ThrowingEx;
2727

2828
import picocli.CommandLine;
2929

@@ -76,12 +76,25 @@ public SpotlessCLIRunner withStep(@NotNull String stepName) {
7676
return this;
7777
}
7878

79-
public SpotlessCLIRunner withStep(@NotNull Class<? extends SpotlessCLIFormatterStep> stepClass) {
79+
/**
80+
* Adds a step to the command line arguments.
81+
* The step class must implement {@link SpotlessCLIFormatterStep}.
82+
*
83+
* @param stepClass the class of the step to add
84+
* @return this instance for method chaining
85+
* @throws IllegalArgumentException if the step class does not implement {@link SpotlessCLIFormatterStep}
86+
*/
87+
public SpotlessCLIRunner withStep(@NotNull Class<?> stepClass) {
88+
// use reflection to allow having this class in testlib
89+
if (!ThrowingEx.get(() -> Class.forName("com.diffplug.spotless.cli.steps.SpotlessCLIFormatterStep"))
90+
.isAssignableFrom(stepClass)) {
91+
throw new IllegalArgumentException("Step class must implement SpotlessCLIFormatterStep");
92+
}
8093
String stepName = determineStepName(stepClass);
8194
return withStep(stepName);
8295
}
8396

84-
private String determineStepName(Class<? extends SpotlessCLIFormatterStep> stepClass) {
97+
private String determineStepName(Class<?> stepClass) {
8598
CommandLine.Command annotation = stepClass.getAnnotation(CommandLine.Command.class);
8699
if (annotation == null) {
87100
throw new IllegalArgumentException("Step class must be annotated with @CommandLine.Command");

app/src/test/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInExternalJavaProcess.java renamed to testlib/src/main/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInExternalJavaProcess.java

File renamed without changes.

app/src/test/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInNativeExternalProcess.java renamed to testlib/src/main/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInNativeExternalProcess.java

File renamed without changes.

app/src/test/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInSameThread.java renamed to testlib/src/main/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInSameThread.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
import java.io.StringWriter;
2020
import java.util.List;
2121

22+
import com.diffplug.spotless.ThrowingEx;
23+
2224
import picocli.CommandLine;
2325

2426
public class SpotlessCLIRunnerInSameThread extends SpotlessCLIRunner {
2527

2628
static final String SPOTLESS_CLI_IN_SAME_THREAD = "spotless.cli.inSameThread";
2729

2830
protected Result executeCommand(List<String> args) {
29-
SpotlessCLI cli = SpotlessCLI.createInstance();
30-
CommandLine commandLine = SpotlessCLI.createCommandLine(cli);
31+
CommandLine commandLine = createCommandLine();
3132

3233
StringWriter out = new StringWriter();
3334
StringWriter err = new StringWriter();
@@ -51,6 +52,15 @@ protected Result executeCommand(List<String> args) {
5152
}
5253
}
5354

55+
private static CommandLine createCommandLine() {
56+
return ThrowingEx.get(() -> {
57+
Class<?> cliClass = Class.forName("com.diffplug.spotless.cli.SpotlessCLI");
58+
Object cliInstance = cliClass.getDeclaredMethod("createInstance").invoke(null);
59+
return (CommandLine)
60+
cliClass.getDeclaredMethod("createCommandLine", cliClass).invoke(null, cliClass.cast(cliInstance));
61+
});
62+
}
63+
5464
private String[] argsWithBaseDir(List<String> args) {
5565
// prepend the base dir
5666
String[] argsWithBaseDir = new String[args.size() + 2];

0 commit comments

Comments
 (0)