1616
1717package com .palantir .javaformat .gradle .testing ;
1818
19+ import java .io .File ;
1920import java .io .IOException ;
21+ import java .io .InputStream ;
2022import java .io .UncheckedIOException ;
2123import java .nio .charset .StandardCharsets ;
2224import java .nio .file .Files ;
2527import java .util .Arrays ;
2628import java .util .LinkedHashSet ;
2729import java .util .List ;
30+ import java .util .Objects ;
31+ import java .util .Properties ;
2832import java .util .Set ;
2933import org .gradle .testkit .runner .BuildResult ;
3034import org .gradle .testkit .runner .GradleRunner ;
@@ -46,11 +50,15 @@ public final class GradleTestProject {
4650 */
4751 private static final String GRADLE_VERSION = "9.3.1" ;
4852
53+ /** Written by java-gradle-plugin: the classpath {@link GradleRunner#withPluginClasspath()} injects. */
54+ private static final String PLUGIN_METADATA = "plugin-under-test-metadata.properties" ;
55+
4956 private final Path projectDir ;
5057 private final Set <String > plugins = new LinkedHashSet <>();
5158 private final StringBuilder buildGradle = new StringBuilder ();
5259 private final StringBuilder gradleProperties = new StringBuilder ();
5360 private boolean configurationCache ;
61+ private boolean spotlessOnClasspath = true ;
5462
5563 public GradleTestProject (Path projectDir ) {
5664 this .projectDir = projectDir ;
@@ -96,6 +104,16 @@ public GradleTestProject withConfigurationCache() {
96104 return this ;
97105 }
98106
107+ /**
108+ * Leaves the Spotless jars out of the classpath injected next to the plugin under test. build.gradle puts them there
109+ * so that generated projects can apply Spotless, which gives every other test Spotless classes whether it applies
110+ * Spotless or not — unlike a real project that never does.
111+ */
112+ public GradleTestProject withoutSpotlessOnClasspath () {
113+ this .spotlessOnClasspath = false ;
114+ return this ;
115+ }
116+
99117 public Path file (String relativePath ) {
100118 return projectDir .resolve (relativePath );
101119 }
@@ -132,12 +150,27 @@ private GradleRunner runner(String... arguments) {
132150 List <String > allArguments = new ArrayList <>(Arrays .asList (arguments ));
133151 allArguments .add ("--stacktrace" );
134152 allArguments .add (configurationCache ? "--configuration-cache" : "--no-configuration-cache" );
135- return GradleRunner .create ()
153+ GradleRunner runner = GradleRunner .create ()
136154 .withProjectDir (projectDir .toFile ())
137- .withPluginClasspath ()
138155 .withGradleVersion (GRADLE_VERSION )
139156 .withArguments (allArguments )
140157 .forwardOutput ();
158+ return spotlessOnClasspath
159+ ? runner .withPluginClasspath ()
160+ : runner .withPluginClasspath (pluginClasspathWithoutSpotless ());
161+ }
162+
163+ private static List <File > pluginClasspathWithoutSpotless () {
164+ Properties metadata = new Properties ();
165+ try (InputStream in = GradleTestProject .class .getClassLoader ().getResourceAsStream (PLUGIN_METADATA )) {
166+ metadata .load (Objects .requireNonNull (in , PLUGIN_METADATA + " is not on the test classpath" ));
167+ } catch (IOException e ) {
168+ throw new UncheckedIOException ("Could not read " + PLUGIN_METADATA , e );
169+ }
170+ return Arrays .stream (metadata .getProperty ("implementation-classpath" ).split (File .pathSeparator ))
171+ .map (File ::new )
172+ .filter (file -> !file .getName ().startsWith ("spotless-" ))
173+ .toList ();
141174 }
142175
143176 private void write () {
0 commit comments