• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.hilt.processor.internal.uninstallmodules;
18 
19 import static com.google.testing.compile.CompilationSubject.assertThat;
20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compiler;
21 
22 import com.google.testing.compile.Compilation;
23 import com.google.testing.compile.JavaFileObjects;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 @RunWith(JUnit4.class)
29 public class UninstallModulesProcessorTest {
30 
31   @Test
testInvalidModuleNoInstallIn_fails()32   public void testInvalidModuleNoInstallIn_fails() {
33     Compilation compilation =
34         compiler()
35             .compile(
36                 JavaFileObjects.forSourceLines(
37                     "test.MyTest",
38                     "package test;",
39                     "",
40                     "import dagger.hilt.android.testing.HiltAndroidTest;",
41                     "import dagger.hilt.android.testing.UninstallModules;",
42                     "",
43                     "@UninstallModules(InvalidModule.class)",
44                     "@HiltAndroidTest",
45                     "public class MyTest {}"),
46                 JavaFileObjects.forSourceLines(
47                     "test.InvalidModule",
48                     "package test;",
49                     "",
50                     "import dagger.Module;",
51                     "import dagger.hilt.migration.DisableInstallInCheck;",
52                     "",
53                     "@DisableInstallInCheck",
54                     "@Module",
55                     "public class InvalidModule {}"));
56     assertThat(compilation).failed();
57     assertThat(compilation).hadErrorCount(1);
58     assertThat(compilation)
59         .hadErrorContaining(
60             "@UninstallModules should only include modules annotated with both @Module and "
61                 + "@InstallIn, but found: [test.InvalidModule].");
62   }
63 
64   @Test
testInvalidModuleNoModule_fails()65   public void testInvalidModuleNoModule_fails() {
66     Compilation compilation =
67         compiler()
68             .compile(
69                 JavaFileObjects.forSourceLines(
70                     "test.MyTest",
71                     "package test;",
72                     "",
73                     "import dagger.hilt.android.testing.HiltAndroidTest;",
74                     "import dagger.hilt.android.testing.UninstallModules;",
75                     "",
76                     "@UninstallModules(InvalidModule.class)",
77                     "@HiltAndroidTest",
78                     "public class MyTest {}"),
79                 JavaFileObjects.forSourceLines(
80                     "test.InvalidModule",
81                     "package test;",
82                     "",
83                     "public class InvalidModule {",
84                     "}"));
85     assertThat(compilation).failed();
86     assertThat(compilation).hadErrorCount(1);
87     assertThat(compilation)
88         .hadErrorContaining(
89             "@UninstallModules should only include modules annotated with both @Module and "
90                 + "@InstallIn, but found: [test.InvalidModule].");
91   }
92 
93   @Test
testInvalidTest_fails()94   public void testInvalidTest_fails() {
95     Compilation compilation =
96         compiler()
97             .compile(
98                 JavaFileObjects.forSourceLines(
99                     "test.InvalidTest",
100                     "package test;",
101                     "",
102                     "import dagger.hilt.android.testing.UninstallModules;",
103                     "",
104                     "@UninstallModules(ValidModule.class)",
105                     "public class InvalidTest {}"),
106                 JavaFileObjects.forSourceLines(
107                     "test.ValidModule",
108                     "package test;",
109                     "",
110                     "import dagger.Module;",
111                     "import dagger.hilt.InstallIn;",
112                     "import dagger.hilt.components.SingletonComponent;",
113                     "",
114                     "@Module",
115                     "@InstallIn(SingletonComponent.class)",
116                     "public class ValidModule {}"));
117     assertThat(compilation).failed();
118     assertThat(compilation).hadErrorCount(1);
119     assertThat(compilation)
120         .hadErrorContaining(
121             "@UninstallModules should only be used on test classes annotated with @HiltAndroidTest,"
122                 + " but found: test.InvalidTest");
123   }
124 
125   @Test
testInvalidTestModule_fails()126   public void testInvalidTestModule_fails() {
127     Compilation compilation =
128         compiler()
129             .compile(
130                 JavaFileObjects.forSourceLines(
131                     "test.MyTest",
132                     "package test;",
133                     "",
134                     "import dagger.Module;",
135                     "import dagger.hilt.InstallIn;",
136                     "import dagger.hilt.components.SingletonComponent;",
137                     "import dagger.hilt.android.testing.HiltAndroidTest;",
138                     "import dagger.hilt.android.testing.UninstallModules;",
139                     "",
140                     "@UninstallModules({",
141                     "    MyTest.PkgPrivateInvalidModule.class,",
142                     "    MyTest.PublicInvalidModule.class,",
143                     "})",
144                     "@HiltAndroidTest",
145                     "public class MyTest {",
146                     "  @Module",
147                     "  @InstallIn(SingletonComponent.class)",
148                     "  interface PkgPrivateInvalidModule {}",
149                     "",
150                     "  @Module",
151                     "  @InstallIn(SingletonComponent.class)",
152                     "  public interface PublicInvalidModule {}",
153                     "}"));
154     assertThat(compilation).failed();
155     assertThat(compilation).hadErrorCount(1);
156     // TODO(bcorso): Consider unwrapping pkg-private modules before reporting the error.
157     assertThat(compilation)
158         .hadErrorContaining(
159             "@UninstallModules should not contain test modules, but found: "
160                 + "[test.MyTest.PkgPrivateInvalidModule, test.MyTest.PublicInvalidModule]");
161   }
162 }
163