aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Chang <erichang@google.com>2024-02-20 13:44:28 -0800
committerDagger Team <dagger-dev+copybara@google.com>2024-02-20 13:52:02 -0800
commitc40811e71012c0838b83c3dd6b921f42332f2831 (patch)
treee8624b9c2e06f1c3557b473b423d70aab4ea554c
parent02d62d69329af9ba71773718cd0ae8a41bac4f3e (diff)
downloaddagger2-c40811e71012c0838b83c3dd6b921f42332f2831.tar.gz
Add a SkipTestInjection annotation to disable injecting the test class. This may be useful for outside rules that wants to inject the test class from some other Hilt component.
This annotation may be used directly on the test class or on some other annotation, as typically outside rules would have some other code generators on their own to generate needed entry points. RELNOTES=Add @SkipTestInjection PiperOrigin-RevId: 608724405
-rw-r--r--java/dagger/hilt/android/testing/BUILD9
-rw-r--r--java/dagger/hilt/android/testing/SkipTestInjection.java33
-rw-r--r--java/dagger/hilt/processor/internal/ClassNames.java2
-rw-r--r--java/dagger/hilt/processor/internal/root/RootProcessingStep.java6
-rw-r--r--java/dagger/hilt/processor/internal/root/TestComponentDataGenerator.java9
-rw-r--r--java/dagger/hilt/processor/internal/root/TestRootMetadata.java25
-rw-r--r--javatests/dagger/hilt/android/testing/BUILD34
-rw-r--r--javatests/dagger/hilt/android/testing/SkipTestInjectionAnnotationTest.java51
-rw-r--r--javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java48
9 files changed, 215 insertions, 2 deletions
diff --git a/java/dagger/hilt/android/testing/BUILD b/java/dagger/hilt/android/testing/BUILD
index 807dc19fd..ee3d78ab1 100644
--- a/java/dagger/hilt/android/testing/BUILD
+++ b/java/dagger/hilt/android/testing/BUILD
@@ -168,6 +168,15 @@ android_library(
],
)
+android_library(
+ name = "skip_test_injection",
+ testonly = 1,
+ srcs = ["SkipTestInjection.java"],
+ deps = [
+ ":package_info",
+ ],
+)
+
java_library(
name = "package_info",
srcs = ["package-info.java"],
diff --git a/java/dagger/hilt/android/testing/SkipTestInjection.java b/java/dagger/hilt/android/testing/SkipTestInjection.java
new file mode 100644
index 000000000..f8e015ba0
--- /dev/null
+++ b/java/dagger/hilt/android/testing/SkipTestInjection.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2024 The Dagger Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dagger.hilt.android.testing;
+
+import static java.lang.annotation.RetentionPolicy.CLASS;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used for skipping test injection in a Hilt Android Test. This may be useful if
+ * building separate custom test infrastructure to inject the test class from another Hilt
+ * component. This may be used on either the test class or an annotation that annotates
+ * the test class.
+ */
+@Retention(CLASS)
+@Target({ElementType.TYPE})
+public @interface SkipTestInjection {}
diff --git a/java/dagger/hilt/processor/internal/ClassNames.java b/java/dagger/hilt/processor/internal/ClassNames.java
index bc0591cb0..26b90a448 100644
--- a/java/dagger/hilt/processor/internal/ClassNames.java
+++ b/java/dagger/hilt/processor/internal/ClassNames.java
@@ -170,6 +170,8 @@ public final class ClassNames {
get("dagger.hilt.android.internal.testing", "TestInstanceHolder");
public static final ClassName HILT_ANDROID_TEST =
get("dagger.hilt.android.testing", "HiltAndroidTest");
+ public static final ClassName SKIP_TEST_INJECTION =
+ get("dagger.hilt.android.testing", "SkipTestInjection");
public static final ClassName CUSTOM_TEST_APPLICATION =
get("dagger.hilt.android.testing", "CustomTestApplication");
public static final ClassName ON_COMPONENT_READY_RUNNER =
diff --git a/java/dagger/hilt/processor/internal/root/RootProcessingStep.java b/java/dagger/hilt/processor/internal/root/RootProcessingStep.java
index 59c64af7f..d6de432bc 100644
--- a/java/dagger/hilt/processor/internal/root/RootProcessingStep.java
+++ b/java/dagger/hilt/processor/internal/root/RootProcessingStep.java
@@ -84,8 +84,10 @@ public final class RootProcessingStep extends BaseProcessingStep {
// for unrelated changes in Gradle.
RootType rootType = RootType.of(rootElement);
if (rootType.isTestRoot()) {
- new TestInjectorGenerator(processingEnv(), TestRootMetadata.of(processingEnv(), rootElement))
- .generate();
+ TestRootMetadata testRootMetadata = TestRootMetadata.of(processingEnv(), rootElement);
+ if (testRootMetadata.skipTestInjectionAnnotation().isEmpty()) {
+ new TestInjectorGenerator(processingEnv(), testRootMetadata).generate();
+ }
}
XTypeElement originatingRootElement =
diff --git a/java/dagger/hilt/processor/internal/root/TestComponentDataGenerator.java b/java/dagger/hilt/processor/internal/root/TestComponentDataGenerator.java
index 689dae7dc..bacb75b87 100644
--- a/java/dagger/hilt/processor/internal/root/TestComponentDataGenerator.java
+++ b/java/dagger/hilt/processor/internal/root/TestComponentDataGenerator.java
@@ -25,6 +25,7 @@ import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
import androidx.room.compiler.processing.JavaPoetExtKt;
+import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XConstructorElement;
import androidx.room.compiler.processing.XFiler.Mode;
import androidx.room.compiler.processing.XProcessingEnv;
@@ -41,6 +42,7 @@ import dagger.hilt.processor.internal.ComponentNames;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import java.util.List;
+import java.util.Optional;
/** Generates an implementation of {@link dagger.hilt.android.internal.TestComponentData}. */
public final class TestComponentDataGenerator {
@@ -222,6 +224,13 @@ public final class TestComponentDataGenerator {
}
private CodeBlock callInjectTest(XTypeElement testElement) {
+ Optional<XAnnotation> skipTestInjection =
+ rootMetadata.testRootMetadata().skipTestInjectionAnnotation();
+ if (skipTestInjection.isPresent()) {
+ return CodeBlock.of(
+ "throw new IllegalStateException(\"Cannot inject test when using @$L\")",
+ skipTestInjection.get().getName());
+ }
return CodeBlock.of(
"(($T) (($T) $T.getApplication($T.getApplicationContext()))"
+ ".generatedComponent()).injectTest(testInstance)",
diff --git a/java/dagger/hilt/processor/internal/root/TestRootMetadata.java b/java/dagger/hilt/processor/internal/root/TestRootMetadata.java
index 5dc6feeaa..b0523034f 100644
--- a/java/dagger/hilt/processor/internal/root/TestRootMetadata.java
+++ b/java/dagger/hilt/processor/internal/root/TestRootMetadata.java
@@ -16,6 +16,7 @@
package dagger.hilt.processor.internal.root;
+import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
@@ -25,6 +26,8 @@ import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.ProcessorErrors;
import dagger.hilt.processor.internal.Processors;
import dagger.internal.codegen.xprocessing.XElements;
+import java.util.Optional;
+import java.util.Set;
import javax.lang.model.element.TypeElement;
/** Metadata class for {@code InternalTestRoot} annotated classes. */
@@ -57,6 +60,28 @@ abstract class TestRootMetadata {
return Processors.append(Processors.getEnclosedClassName(testName()), "_GeneratedInjector");
}
+ /**
+ * Returns either the SkipTestInjection annotation or the first annotation that was annotated
+ * with SkipTestInjection, if present.
+ */
+ Optional<XAnnotation> skipTestInjectionAnnotation() {
+ XAnnotation skipTestAnnotation = testElement().getAnnotation(ClassNames.SKIP_TEST_INJECTION);
+ if (skipTestAnnotation != null) {
+ return Optional.of(skipTestAnnotation);
+ }
+
+ Set<XAnnotation> annotatedAnnotations = testElement().getAnnotationsAnnotatedWith(
+ ClassNames.SKIP_TEST_INJECTION);
+ if (!annotatedAnnotations.isEmpty()) {
+ // Just return the first annotation that skips test injection if there are multiple since
+ // at this point it doesn't really matter and the specific annotation is only really useful
+ // for communicating back to the user.
+ return Optional.of(annotatedAnnotations.iterator().next());
+ }
+
+ return Optional.empty();
+ }
+
static TestRootMetadata of(XProcessingEnv env, XElement element) {
XTypeElement testElement = XElements.asTypeElement(element);
diff --git a/javatests/dagger/hilt/android/testing/BUILD b/javatests/dagger/hilt/android/testing/BUILD
index 7dc1e48cf..973161087 100644
--- a/javatests/dagger/hilt/android/testing/BUILD
+++ b/javatests/dagger/hilt/android/testing/BUILD
@@ -158,3 +158,37 @@ android_local_test(
"//third_party/java/truth",
],
)
+
+android_local_test(
+ name = "SkipTestInjectionTest",
+ srcs = ["SkipTestInjectionTest.java"],
+ manifest_values = {
+ "minSdkVersion": "15",
+ "targetSdkVersion": "27",
+ },
+ deps = [
+ "//:android_local_test_exports",
+ "//:dagger_with_compiler",
+ "//java/dagger/hilt/android/testing:hilt_android_test",
+ "//java/dagger/hilt/android/testing:skip_test_injection",
+ "//third_party/java/jsr330_inject",
+ "//third_party/java/truth",
+ ],
+)
+
+android_local_test(
+ name = "SkipTestInjectionAnnotationTest",
+ srcs = ["SkipTestInjectionAnnotationTest.java"],
+ manifest_values = {
+ "minSdkVersion": "15",
+ "targetSdkVersion": "27",
+ },
+ deps = [
+ "//:android_local_test_exports",
+ "//:dagger_with_compiler",
+ "//java/dagger/hilt/android/testing:hilt_android_test",
+ "//java/dagger/hilt/android/testing:skip_test_injection",
+ "//third_party/java/jsr330_inject",
+ "//third_party/java/truth",
+ ],
+)
diff --git a/javatests/dagger/hilt/android/testing/SkipTestInjectionAnnotationTest.java b/javatests/dagger/hilt/android/testing/SkipTestInjectionAnnotationTest.java
new file mode 100644
index 000000000..60d2bb367
--- /dev/null
+++ b/javatests/dagger/hilt/android/testing/SkipTestInjectionAnnotationTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2024 The Dagger Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dagger.hilt.android.testing;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import javax.inject.Inject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+@HiltAndroidTest
+@SkipTestInjectionAnnotationTest.TestAnnotation
+@RunWith(AndroidJUnit4.class)
+@Config(application = HiltTestApplication.class)
+public final class SkipTestInjectionAnnotationTest {
+ @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);
+
+ @SkipTestInjection
+ @interface TestAnnotation {}
+
+ @Inject String string; // Never provided, shouldn't compile without @SkipTestInjection
+
+ @Test
+ public void testCannotCallInjectOnTestRule() throws Exception {
+ IllegalStateException exception =
+ assertThrows(
+ IllegalStateException.class,
+ () -> rule.inject());
+ assertThat(exception)
+ .hasMessageThat()
+ .isEqualTo("Cannot inject test when using @TestAnnotation");
+ }
+}
diff --git a/javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java b/javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java
new file mode 100644
index 000000000..bf4124ce2
--- /dev/null
+++ b/javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2024 The Dagger Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dagger.hilt.android.testing;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import javax.inject.Inject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+@HiltAndroidTest
+@SkipTestInjection
+@RunWith(AndroidJUnit4.class)
+@Config(application = HiltTestApplication.class)
+public final class SkipTestInjectionTest {
+ @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);
+
+ @Inject String string; // Never provided, shouldn't compile without @SkipTestInjection
+
+ @Test
+ public void testCannotCallInjectOnTestRule() throws Exception {
+ IllegalStateException exception =
+ assertThrows(
+ IllegalStateException.class,
+ () -> rule.inject());
+ assertThat(exception)
+ .hasMessageThat()
+ .isEqualTo("Cannot inject test when using @SkipTestInjection");
+ }
+}