Skip to content

Commit 466e220

Browse files
bcorsoDagger Team
authored andcommitted
Switch Dagger compiler tests to use KSP2.
There are a few tests that have issues in KSP2, so I've created a legacy configuration for them and left TODOs to clean them up. RELNOTES=N/A PiperOrigin-RevId: 720182115
1 parent ba58937 commit 466e220

9 files changed

+75
-25
lines changed

java/dagger/testing/compile/CompilerTests.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ public final class CompilerTests {
7272

7373
private static final ImmutableList<String> DEFAULT_KOTLINC_OPTIONS =
7474
ImmutableList.of(
75-
"-api-version=1.9",
76-
"-language-version=1.9",
7775
"-jvm-target=11",
7876
"-Xjvm-default=all",
7977
"-P",
8078
"plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true");
8179

80+
private static final ImmutableList<String> KSP1_DEFAULT_KOTLINC_OPTIONS =
81+
ImmutableList.<String>builder()
82+
.addAll(DEFAULT_KOTLINC_OPTIONS)
83+
.add("-api-version=1.9")
84+
.add("-language-version=1.9")
85+
.build();
86+
8287
/** Returns the {@link XProcessingEnv.Backend} for the given {@link CompilationResultSubject}. */
8388
public static XProcessingEnv.Backend backend(CompilationResultSubject subject) {
8489
// TODO(bcorso): Create a more official API for this in XProcessing testing.
@@ -235,12 +240,28 @@ public DaggerCompiler withBindingGraphPlugins(Supplier<BindingGraphPlugin>... su
235240
}
236241

237242
public void compile(Consumer<CompilationResultSubject> onCompilationResult) {
243+
compileInternal(onCompilationResult, DEFAULT_KOTLINC_OPTIONS);
244+
}
245+
246+
/**
247+
* Returns a compiler with the KSP1 options.
248+
*
249+
* @deprecated This is only intended to be used for tests that are not yet compatible with KSP2.
250+
*/
251+
@Deprecated // TODO(b/378271452): Remove once we've fixed issues with KSP2
252+
public void legacyCompile(Consumer<CompilationResultSubject> onCompilationResult) {
253+
compileInternal(onCompilationResult, KSP1_DEFAULT_KOTLINC_OPTIONS);
254+
}
255+
256+
private void compileInternal(
257+
Consumer<CompilationResultSubject> onCompilationResult,
258+
ImmutableList<String> kotlincArguments) {
238259
ProcessorTestExtKt.runProcessorTest(
239260
sources().asList(),
240261
/* classpath= */ ImmutableList.of(),
241262
processorOptions(),
242263
/* javacArguments= */ DEFAULT_JAVAC_OPTIONS,
243-
/* kotlincArguments= */ DEFAULT_KOTLINC_OPTIONS,
264+
/* kotlincArguments= */ kotlincArguments,
244265
/* config= */ PROCESSING_ENV_CONFIG,
245266
/* javacProcessors= */ mergeProcessors(
246267
ImmutableList.of(

javatests/dagger/internal/codegen/BindsMethodValidationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ public void setElements_primitiveArgument() {
193193
@Test
194194
public void elementsIntoSet_withRawSets() {
195195
assertThatMethod("@Binds @ElementsIntoSet abstract Set bindRawSet(HashSet hashSet);")
196-
.hasError("cannot return a raw Set");
196+
// TODO(b/381557054): Remove legacy KSP1 usage after KSP2 issue has been fixed.
197+
.legacyHasError("cannot return a raw Set");
197198
}
198199

199200
@Test

javatests/dagger/internal/codegen/ComponentCreatorTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ public void testMoreThanOneCreatorOfSameTypeFails() {
180180

181181
CompilerTests.daggerCompiler(componentFile)
182182
.withProcessingOptions(compilerOptions)
183-
.compile(
183+
// TODO(b/381556660): Remove legacy KSP1 usage after KSP2 issue has been fixed.
184+
.legacyCompile(
184185
subject -> {
185186
subject.hasErrorCount(1);
186187
subject.hasErrorContaining(
@@ -215,7 +216,8 @@ public void testBothBuilderAndFactoryFails() {
215216
"}");
216217
CompilerTests.daggerCompiler(componentFile)
217218
.withProcessingOptions(compilerOptions)
218-
.compile(
219+
// TODO(b/381556660): Remove legacy KSP1 usage after KSP2 issue has been fixed.
220+
.legacyCompile(
219221
subject -> {
220222
subject.hasErrorCount(1);
221223
subject.hasErrorContaining(

javatests/dagger/internal/codegen/DaggerModuleMethodSubject.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertAbout;
2020

21+
import androidx.room.compiler.processing.util.CompilationResultSubject;
2122
import androidx.room.compiler.processing.util.Source;
2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.truth.FailureMetadata;
@@ -26,10 +27,12 @@
2627
import dagger.Module;
2728
import dagger.producers.ProducerModule;
2829
import dagger.testing.compile.CompilerTests;
30+
import dagger.testing.compile.CompilerTests.DaggerCompiler;
2931
import java.io.PrintWriter;
3032
import java.io.StringWriter;
3133
import java.util.Arrays;
3234
import java.util.List;
35+
import java.util.function.Consumer;
3336

3437
/** A {@link Truth} subject for testing Dagger module methods. */
3538
final class DaggerModuleMethodSubject extends Subject {
@@ -132,21 +135,35 @@ DaggerModuleMethodSubject withAdditionalSources(Source... sources) {
132135
return this;
133136
}
134137

138+
// Remove this once we've fixed issues with KSP2.
139+
void legacyHasError(String errorSubstring) {
140+
hasError(errorSubstring, /* useKsp1= */ true);
141+
}
142+
143+
void hasError(String errorSubstring) {
144+
hasError(errorSubstring, /* useKsp1= */ false);
145+
}
146+
135147
/**
136148
* Fails if compiling the module with the method doesn't report an error at the method
137149
* declaration whose message contains {@code errorSubstring}.
138150
*/
139-
void hasError(String errorSubstring) {
151+
private void hasError(String errorSubstring, boolean useKsp1) {
140152
String source = moduleSource();
141153
Source module = CompilerTests.javaSource("test.TestModule", source);
142-
CompilerTests.daggerCompiler(
143-
ImmutableList.<Source>builder().add(module).addAll(additionalSources).build())
144-
.compile(
145-
subject ->
146-
subject
147-
.hasErrorContaining(errorSubstring)
148-
.onSource(module)
149-
.onLine(methodLine(source)));
154+
DaggerCompiler compiler = CompilerTests.daggerCompiler(
155+
ImmutableList.<Source>builder().add(module).addAll(additionalSources).build());
156+
Consumer<CompilationResultSubject> onCompilationResult =
157+
subject ->
158+
subject
159+
.hasErrorContaining(errorSubstring)
160+
.onSource(module)
161+
.onLine(methodLine(source));
162+
if (useKsp1) {
163+
compiler.legacyCompile(onCompilationResult);
164+
} else {
165+
compiler.compile(onCompilationResult);
166+
}
150167
}
151168

152169
private int methodLine(String source) {

javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public final class InjectConstructorFactoryGeneratorTest {
297297
" String s) {}",
298298
"}");
299299
CompilerTests.daggerCompiler(file, QUALIFIER_A, QUALIFIER_B)
300-
.compile(
300+
.legacyCompile(
301301
subject -> {
302302
subject.hasErrorCount(2);
303303
subject.hasErrorContaining(
@@ -787,7 +787,8 @@ public final class InjectConstructorFactoryGeneratorTest {
787787
" String s) {}",
788788
"}");
789789
CompilerTests.daggerCompiler(file, QUALIFIER_A, QUALIFIER_B)
790-
.compile(
790+
// TODO(b/381557487): Remove legacy KSP1 usage after KSP2 issue has been fixed.
791+
.legacyCompile(
791792
subject -> {
792793
subject.hasErrorCount(2);
793794
subject.hasErrorContaining(

javatests/dagger/internal/codegen/LazyClassKeyMapBindingComponentProcessorTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ public void testProguardFile() throws Exception {
310310
"}");
311311
CompilerTests.daggerCompiler(fooKey, fooKeyModule)
312312
.withProcessingOptions(compilerMode.processorOptions())
313-
.compile(
313+
// TODO(b/390652173): Remove legacy KSP1 usage after KSP2 issue has been fixed.
314+
.legacyCompile(
314315
subject -> {
315316
subject.hasErrorCount(0);
316317
PrimitiveByteArraySubject proguardFile =
@@ -350,7 +351,8 @@ public void testProguardFile_nestedModule() throws Exception {
350351
"}");
351352
CompilerTests.daggerCompiler(fooKey, outerClass)
352353
.withProcessingOptions(compilerMode.processorOptions())
353-
.compile(
354+
// TODO(b/390652173): Remove legacy KSP1 usage after KSP2 issue has been fixed.
355+
.legacyCompile(
354356
subject -> {
355357
subject.hasErrorCount(0);
356358
PrimitiveByteArraySubject proguardFile =
@@ -411,7 +413,8 @@ public void testProguardFile_multipleModules() throws Exception {
411413
"}");
412414
CompilerTests.daggerCompiler(fooKey, fooKeyModule, barKey, barKeyModule)
413415
.withProcessingOptions(compilerMode.processorOptions())
414-
.compile(
416+
// TODO(b/390652173): Remove legacy KSP1 usage after KSP2 issue has been fixed.
417+
.legacyCompile(
415418
subject -> {
416419
subject.hasErrorCount(0);
417420
PrimitiveByteArraySubject fooKeyModuleProguardFile =
@@ -466,7 +469,8 @@ public void testProguardFile_multipleKeys() throws Exception {
466469
"}");
467470
CompilerTests.daggerCompiler(fooKey, barKey, fooKeyAndBarKeyModule)
468471
.withProcessingOptions(compilerMode.processorOptions())
469-
.compile(
472+
// TODO(b/390652173): Remove legacy KSP1 usage after KSP2 issue has been fixed.
473+
.legacyCompile(
470474
subject -> {
471475
subject.hasErrorCount(0);
472476
PrimitiveByteArraySubject proguardFile =

javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,8 @@ public void providesMethodMultipleQualifiersOnParameter() {
995995
" }",
996996
"}");
997997
CompilerTests.daggerCompiler(moduleFile, QUALIFIER_A, QUALIFIER_B)
998-
.compile(
998+
// TODO(b/381557487): Remove legacy KSP1 usage after KSP2 issue has been fixed.
999+
.legacyCompile(
9991000
subject -> {
10001001
// There are two errors -- 1 per qualifier.
10011002
subject.hasErrorCount(2);

javatests/dagger/internal/codegen/ModuleValidationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ public void moduleIncludesSelfCycle() {
444444
"class OtherModule {}");
445445

446446
CompilerTests.daggerCompiler(module, otherModule)
447-
.compile(
447+
// TODO(b/381557487): Remove legacy KSP1 usage after KSP2 issue has been fixed.
448+
.legacyCompile(
448449
subject -> {
449450
subject.hasErrorCount(2);
450451
String error =

javatests/dagger/internal/codegen/SubcomponentCreatorValidationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public void testMoreThanOneCreatorFails() {
161161
" }",
162162
"}");
163163
CompilerTests.daggerCompiler(componentFile, childComponentFile)
164-
.compile(
164+
// TODO(b/381556660): Remove legacy KSP1 usage after KSP2 issue has been fixed.
165+
.legacyCompile(
165166
subject -> {
166167
subject.hasErrorCount(1);
167168
subject.hasErrorContaining(
@@ -203,7 +204,8 @@ public void testMoreThanOneCreatorFails_differentTypes() {
203204
" }",
204205
"}");
205206
CompilerTests.daggerCompiler(componentFile, childComponentFile)
206-
.compile(
207+
// TODO(b/381556660): Remove legacy KSP1 usage after KSP2 issue has been fixed.
208+
.legacyCompile(
207209
subject -> {
208210
subject.hasErrorCount(1);
209211
subject.hasErrorContaining(

0 commit comments

Comments
 (0)