Skip to content

Commit c56af9f

Browse files
committed
Make it pretty, make it nice
1 parent 54d8556 commit c56af9f

2 files changed

Lines changed: 10 additions & 56 deletions

File tree

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/GradlewExecutor.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ import java.util.concurrent.TimeUnit
2424
import java.util.stream.Collectors
2525
import java.util.stream.Stream
2626

27+
/**
28+
* IntegrationTestKitSpec currently [loads more than what it needs into the classpath](https://github.com/nebula-plugins/nebula-test/blob/c5d3af9004898276bde5c68da492c6b0b4c5facc/src/main/groovy/nebula/test/IntegrationTestKitBase.groovy#L136)
29+
* This means the Formatter is loaded onto the classpath eagerly, [erroneously](https://github.com/palantir/palantir-java-format/blob/00b08d2f471d66382d6c4cd2d05f56b6bb546ad3/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/spotless/PalantirJavaFormatStep.java#L83).
30+
* As a workaround, let's use the classpath produced by Gradle Test Kit in plugin-under-test-metadata.properties
31+
* This classpath only contains the dependencies required by the plugin, as well as the plugin itself.
32+
* This means no more eager loading of Formatters onto the classpath, no more complaints from PalantirJavaFormatStep
33+
*/
2734
class GradlewExecutor {
2835
File projectDir
2936

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatSpotlessPluginTest.groovy

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
3131
private static final NATIVE_IMAGE_FILE = new File("build/nativeImage.path")
3232
private static final NATIVE_CONFIG = String.format("palantirJavaFormatNative files(\"%s\")", NATIVE_IMAGE_FILE.text)
3333

34+
private final executor = new GradlewExecutor(projectDir)
35+
3436
@Unroll
3537
def "formats with spotless when spotless is applied"(String extraGradleProperties, String javaVersion, String expectedOutput) {
3638
def extraDependencies = extraGradleProperties.isEmpty() ? "" : NATIVE_CONFIG
@@ -109,7 +111,7 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
109111

110112

111113
when:
112-
def result = runGradlewTasks('spotlessApply', '--info')
114+
def result = executor.runGradlewTasks('spotlessApply', '--info')
113115

114116
then:
115117
result.standardOutput.contains(expectedOutput)
@@ -123,61 +125,6 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
123125

124126
}
125127

126-
private static Iterable<File> getBuildPluginClasspathInjector() {
127-
return getPluginClasspathInjector(Path.of("../gradle-palantir-java-format/build/pluginUnderTestMetadata/plugin-under-test-metadata.properties"))
128-
}
129-
130-
private static Iterable<File> getPluginClasspathInjector(Path path) {
131-
File propertiesFile = path.toFile()
132-
Properties properties = new Properties()
133-
propertiesFile.withInputStream { inputStream ->
134-
properties.load(inputStream)
135-
}
136-
String classpath = properties.getProperty('implementation-classpath')
137-
return classpath.split(File.pathSeparator).collect { new File(it) }
138-
}
139-
140-
private GradlewExecutionResult runGradlewTasks(String... tasks) {
141-
ProcessBuilder processBuilder = getProcessBuilder(tasks)
142-
Process process = processBuilder.start()
143-
String output = readAllInput(process.getInputStream())
144-
process.waitFor(1, TimeUnit.MINUTES)
145-
GradlewExecutionResult result = new GradlewExecutionResult(process.exitValue(), output)
146-
assert result.success
147-
return result
148-
}
149-
150-
final class GradlewExecutionResult {
151-
152-
private final Boolean success
153-
private final String standardOutput
154-
private final Throwable failure
155-
156-
GradlewExecutionResult(int exitValue, String output) {
157-
this.success = exitValue == 0
158-
this.standardOutput = output
159-
this.failure = exitValue != 0 ? new RuntimeException(String.format("Build failed with exitCode %s", exitValue)) : null
160-
}
161-
}
162-
163-
static String readAllInput(InputStream inputStream) {
164-
try (Stream<String> lines =
165-
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()) {
166-
return lines.collect(Collectors.joining("\n"));
167-
}
168-
}
169-
170-
private ProcessBuilder getProcessBuilder(String... tasks) {
171-
File initScript = new File(projectDir, "init.gradle")
172-
ClasspathAddingInitScriptBuilder.build(initScript, getBuildPluginClasspathInjector().toList())
173-
List<String> arguments = ["./gradlew", "--init-script", initScript.toPath().toString()]
174-
Arrays.asList(tasks).forEach(arguments::add)
175-
ProcessBuilder processBuilder = new ProcessBuilder()
176-
.command(arguments)
177-
.directory(projectDir)
178-
.redirectErrorStream(true)
179-
return processBuilder
180-
}
181128

182129
def validJavaFile = '''\
183130
package test;

0 commit comments

Comments
 (0)