Skip to content

Commit

Permalink
Add a LibraryCompiler to compile sources into a library and use it as…
Browse files Browse the repository at this point in the history
… input for other processor tests.

RELNOTES=N/A
PiperOrigin-RevId: 719401126
  • Loading branch information
danysantiago authored and Dagger Team committed Jan 28, 2025
1 parent 08cd89c commit e1512aa
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 110 deletions.
54 changes: 47 additions & 7 deletions java/dagger/testing/compile/CompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,17 @@ public static InvocationCompiler invocationCompiler(Source... sources) {
ImmutableList.copyOf(sources), DEFAULT_PROCESSOR_OPTIONS);
}

/** */
/**
* Used to compile regular java or kotlin sources and use its compiled artifacts as inputs for
* further compilation or processing.
*
* @see DaggerCompiler#withAdditionalClasspath(ImmutableList)
*/
public static LibraryCompiler libraryCompiler(Source... sources) {
return new AutoValue_CompilerTests_LibraryCompiler(ImmutableList.copyOf(sources));
}

/** Used to compile regular java or kotlin sources and inspect the elements processed. */
@AutoValue
public abstract static class InvocationCompiler {
/** Returns the sources being compiled */
Expand All @@ -162,6 +172,25 @@ public void compile(Consumer<XTestInvocation> onInvocation) {
}
}

/** Used to compile regular java or kotlin sources into a library artifact. */
@AutoValue
public abstract static class LibraryCompiler {
/** Returns the sources being compiled */
abstract ImmutableList<Source> sources();

public ImmutableList<File> compile() {
return ImmutableList.copyOf(
ProcessorTestExtKt.compileFiles(
sources(),
/* options= */ ImmutableMap.of(),
/* annotationProcessors= */ ImmutableList.of(),
/* symbolProcessorProviders= */ ImmutableList.of(),
/* javacArguments= */ DEFAULT_JAVAC_OPTIONS,
/* kotlincArguments= */ DEFAULT_KOTLINC_OPTIONS,
/* includeSystemClasspath= */ true));
}
}

/** Used to compile Dagger sources and inspect the compiled results. */
@AutoValue
public abstract static class DaggerCompiler {
Expand All @@ -170,6 +199,7 @@ static Builder builder() {
// Set default values
return builder
.processorOptions(DEFAULT_PROCESSOR_OPTIONS)
.additionalClasspath(ImmutableList.of())
.additionalJavacProcessors(ImmutableList.of())
.additionalKspProcessors(ImmutableList.of())
.processingStepSuppliers(ImmutableSet.of())
Expand All @@ -182,6 +212,9 @@ static Builder builder() {
/** Returns the annotation processor options */
abstract ImmutableMap<String, String> processorOptions();

/** Returns extra files for the classpath. */
abstract ImmutableList<File> additionalClasspath();

/** Returns the extra Javac processors. */
abstract ImmutableCollection<Processor> additionalJavacProcessors();

Expand All @@ -204,11 +237,11 @@ private ImmutableList<BindingGraphPlugin> bindingGraphPlugins() {
return bindingGraphPluginSuppliers().stream().map(Supplier::get).collect(toImmutableList());
}

/** Returns a builder with the current values of this {@link Compiler} as default. */
/** Returns a builder with the current values of this {@link DaggerCompiler} as default. */
abstract Builder toBuilder();

/**
* Returns a new {@link Compiler} instance with the given processor options.
* Returns a new {@link DaggerCompiler} instance with the given processor options.
*
* <p>Note that the default processor options are still applied unless they are explicitly
* overridden by the given processing options.
Expand All @@ -220,17 +253,17 @@ public DaggerCompiler withProcessingOptions(Map<String, String> processorOptions
return toBuilder().processorOptions(newProcessorOptions).build();
}

/** Returns a new {@link HiltCompiler} instance with the additional Javac processors. */
/** Returns a new {@link DaggerCompiler} instance with the additional Javac processors. */
public DaggerCompiler withAdditionalJavacProcessors(Processor... processors) {
return toBuilder().additionalJavacProcessors(ImmutableList.copyOf(processors)).build();
}

/** Returns a new {@link HiltCompiler} instance with the additional KSP processors. */
/** Returns a new {@link DaggerCompiler} instance with the additional KSP processors. */
public DaggerCompiler withAdditionalKspProcessors(SymbolProcessorProvider... processors) {
return toBuilder().additionalKspProcessors(ImmutableList.copyOf(processors)).build();
}

/** Returns a new {@link Compiler} instance with the given processing steps. */
/** Returns a new {@link DaggerCompiler} instance with the given processing steps. */
public DaggerCompiler withProcessingSteps(Supplier<XProcessingStep>... suppliers) {
return toBuilder().processingStepSuppliers(ImmutableList.copyOf(suppliers)).build();
}
Expand All @@ -239,6 +272,11 @@ public DaggerCompiler withBindingGraphPlugins(Supplier<BindingGraphPlugin>... su
return toBuilder().bindingGraphPluginSuppliers(ImmutableList.copyOf(suppliers)).build();
}

/** Returns a new {@link DaggerCompiler} instance with the additional files in the classpath. */
public DaggerCompiler withAdditionalClasspath(ImmutableList<File> libs) {
return toBuilder().additionalClasspath(libs).build();
}

public void compile(Consumer<CompilationResultSubject> onCompilationResult) {
compileInternal(onCompilationResult, DEFAULT_KOTLINC_OPTIONS);
}
Expand All @@ -258,7 +296,7 @@ private void compileInternal(
ImmutableList<String> kotlincArguments) {
ProcessorTestExtKt.runProcessorTest(
sources().asList(),
/* classpath= */ ImmutableList.of(),
additionalClasspath(),
processorOptions(),
/* javacArguments= */ DEFAULT_JAVAC_OPTIONS,
/* kotlincArguments= */ kotlincArguments,
Expand Down Expand Up @@ -295,6 +333,8 @@ public abstract static class Builder {
abstract Builder sources(ImmutableCollection<Source> sources);
abstract Builder processorOptions(Map<String, String> processorOptions);

abstract Builder additionalClasspath(ImmutableList<File> libs);

abstract Builder additionalJavacProcessors(ImmutableCollection<Processor> processors);

abstract Builder additionalKspProcessors(
Expand Down
13 changes: 0 additions & 13 deletions javatests/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ kt_jvm_library(
],
)

# These sources are purposely compiled without the Dagger compiler for certain tests.
java_library(
name = "java_lib_no_dagger_compiler",
srcs = [
"ComponentProcessorTestClasses.java",
"InvalidInjectConstructor.java",
],
deps = [
"//third_party/java/jsr330_inject",
],
)

# TODO(bcorso): Move this into a subpackage.
java_library(
name = "compilers",
Expand Down Expand Up @@ -105,7 +93,6 @@ MEDIUM_TESTS = [

DEPS = [
":compilers",
":java_lib_no_dagger_compiler",
":kt_lib_no_dagger_compiler",
"//third_party/java/guava/base",
"//third_party/java/guava/collect",
Expand Down
41 changes: 41 additions & 0 deletions javatests/dagger/internal/codegen/ComponentProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,46 @@ public void componentSupertypeDependsOnGeneratedType() {
*/
@Test
public void unprocessedMembersInjectorNotes() {
Source testClasses =
CompilerTests.javaSource(
"dagger.internal.codegen.ComponentProcessorTestClasses",
"package dagger.internal.codegen;",
"",
"import javax.inject.Inject;",
"",
"public final class ComponentProcessorTestClasses {",
" public static final class NoInjectMemberNoConstructor {}",
"",
" public static final class NoInjectMemberWithConstructor {",
" @Inject",
" NoInjectMemberWithConstructor() {}",
" }",
"",
" public abstract static class LocalInjectMemberNoConstructor {",
" @Inject Object object;",
" }",
"",
" public static final class LocalInjectMemberWithConstructor {",
" @SuppressWarnings(\"BadInject\") // Ignore this check as we want to test this case"
+ " in particular.",
" @Inject Object object;",
"",
" @Inject",
" LocalInjectMemberWithConstructor() {}",
" }",
"",
" public static final class ParentInjectMemberNoConstructor extends"
+ " LocalInjectMemberNoConstructor {",
" }",
"",
" public static final class ParentInjectMemberWithConstructor",
" extends LocalInjectMemberNoConstructor {",
" @Inject",
" ParentInjectMemberWithConstructor() {}",
" }",
"",
" private ComponentProcessorTestClasses() {}",
"}");
Source component =
CompilerTests.javaSource(
"test.TestComponent",
Expand Down Expand Up @@ -1426,6 +1466,7 @@ public void unprocessedMembersInjectorNotes() {
" }",
"}");
CompilerTests.daggerCompiler(module, component)
.withAdditionalClasspath(CompilerTests.libraryCompiler(testClasses).compile())
.withProcessingOptions(
ImmutableMap.<String, String>builder()
.putAll(compilerMode.processorOptions())
Expand Down

This file was deleted.

33 changes: 0 additions & 33 deletions javatests/dagger/internal/codegen/InvalidInjectConstructor.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import dagger.testing.compile.CompilerTests;
import java.io.File;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void usedInvalidConstructorFails() {
"}");
CompilerTests.daggerCompiler(component)
.withProcessingOptions(compilerMode.processorOptions())
.withAdditionalClasspath(getInvalidInjectConstructorLib())
.compile(
subject -> {
// subject.hasErrorCount(2);
Expand Down Expand Up @@ -89,6 +91,7 @@ public void unusedInvalidConstructorFails() {
"}");
CompilerTests.daggerCompiler(component)
.withProcessingOptions(compilerMode.processorOptions())
.withAdditionalClasspath(getInvalidInjectConstructorLib())
.compile(
subject -> {
// subject.hasErrorCount(2);
Expand All @@ -104,4 +107,26 @@ public void unusedInvalidConstructorFails() {
+ "an @Provides-annotated method.");
});
}

private ImmutableList<File> getInvalidInjectConstructorLib() {
// A class that invalidly declares 2 inject constructors.
Source source =
CompilerTests.javaSource(
"dagger.internal.codegen.InvalidInjectConstructor",
"package dagger.internal.codegen;",
"",
"import javax.inject.Inject;",
"",
"public final class InvalidInjectConstructor {",
"",
" @Inject String str;",
"",
" @Inject",
" InvalidInjectConstructor() {}",
"",
" @Inject",
" InvalidInjectConstructor(String str) {}",
"}");
return CompilerTests.libraryCompiler(source).compile();
}
}

0 comments on commit e1512aa

Please sign in to comment.