Skip to content

Commit

Permalink
Migrate Hilt compiler tests from KSP1 to KSP2.
Browse files Browse the repository at this point in the history
Note: This was previously blocked by b/390685181 (google/ksp#2309), but that's fixed now
RELNOTES=N/A
PiperOrigin-RevId: 733876340
  • Loading branch information
bcorso authored and Dagger Team committed Mar 7, 2025
1 parent 6f64d4b commit 84cdf34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 24 additions & 3 deletions java/dagger/hilt/android/testing/compile/HiltCompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ public final class HiltCompilerTests {

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

private static final ImmutableList<String> KSP1_DEFAULT_KOTLINC_OPTIONS =
ImmutableList.<String>builder()
.addAll(DEFAULT_KOTLINC_OPTIONS)
.add("-api-version=1.9")
.add("-language-version=1.9")
.build();

/** Returns the {@link XProcessingEnv.Backend} for the given {@link CompilationResultSubject}. */
public static XProcessingEnv.Backend backend(CompilationResultSubject subject) {
return CompilerTests.backend(subject);
Expand Down Expand Up @@ -281,6 +286,22 @@ public HiltCompiler withJavacArguments(ImmutableCollection<String> arguments) {
abstract Builder toBuilder();

public void compile(Consumer<CompilationResultSubject> onCompilationResult) {
compileInternal(onCompilationResult, DEFAULT_KOTLINC_OPTIONS);
}

/**
* Returns a compiler with the KSP1 options.
*
* @deprecated This is only intended to be used for tests that are not yet compatible with KSP2.
*/
@Deprecated // TODO(b/378271452): Remove once we've fixed issues with KSP2
public void legacyCompile(Consumer<CompilationResultSubject> onCompilationResult) {
compileInternal(onCompilationResult, KSP1_DEFAULT_KOTLINC_OPTIONS);
}

private void compileInternal(
Consumer<CompilationResultSubject> onCompilationResult,
ImmutableList<String> kotlincArguments) {
ProcessorTestExtKt.runProcessorTest(
sources().asList(),
/* classpath= */ ImmutableList.of(CompilerTests.compilerDepsJar()),
Expand All @@ -290,7 +311,7 @@ public void compile(Consumer<CompilationResultSubject> onCompilationResult) {
.addAll(DEFAULT_JAVAC_OPTIONS)
.addAll(javacArguments())
.build(),
/* kotlincArguments= */ DEFAULT_KOTLINC_OPTIONS,
/* kotlincArguments= */ kotlincArguments,
/* config= */ HiltProcessingEnvConfigs.CONFIGS,
/* javacProcessors= */ ImmutableList.<Processor>builder()
.addAll(mergeProcessors(defaultProcessors(), additionalJavacProcessors()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package dagger.hilt.processor.internal.aggregateddeps;

import static com.google.common.truth.Truth.assertThat;

import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import dagger.hilt.android.testing.compile.HiltCompilerTests;
import dagger.hilt.processor.internal.GeneratedImport;
import org.junit.Rule;
Expand Down Expand Up @@ -292,10 +289,7 @@ public void testInnerModuleInTest_succeeds() {
" static final class InnerModule {}",
"}");

// TODO(danysantiago): Add KSP test once b/288966076 is resolved.
HiltCompilerTests.compileWithKapt(
ImmutableList.of(source),
tempFolderRule,
result -> assertThat(result.getSuccess()).isTrue());
HiltCompilerTests.hiltCompiler(source)
.compile(subject -> subject.hasErrorCount(0));
}
}

0 comments on commit 84cdf34

Please sign in to comment.