forked from square/dagger
/
IncrementalProcessorTest.kt
1241 lines (1137 loc) · 41.3 KB
/
IncrementalProcessorTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020 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.
*/
import com.google.common.truth.Expect
import java.io.File
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
/**
* Tests to verify Gradle annotation processor incremental compilation.
*
* To run these tests first deploy artifacts to local maven via util/install-local-snapshot.sh.
*/
@RunWith(Parameterized::class)
class IncrementalProcessorTest(private val incapMode: String) {
@get:Rule
val testProjectDir = TemporaryFolder()
@get:Rule
val expect: Expect = Expect.create()
// Original source files
private lateinit var srcApp: File
private lateinit var srcActivity1: File
private lateinit var srcActivity2: File
private lateinit var srcModule1: File
private lateinit var srcModule2: File
private lateinit var srcTest1: File
private lateinit var srcTest2: File
// Generated source files
private lateinit var genHiltApp: File
private lateinit var genHiltActivity1: File
private lateinit var genHiltActivity2: File
private lateinit var genAppInjector: File
private lateinit var genActivityInjector1: File
private lateinit var genActivityInjector2: File
private lateinit var genAppInjectorDeps: File
private lateinit var genActivityInjectorDeps1: File
private lateinit var genActivityInjectorDeps2: File
private lateinit var genModuleDeps1: File
private lateinit var genModuleDeps2: File
private lateinit var genComponentTreeDeps: File
private lateinit var genHiltComponents: File
private lateinit var genDaggerHiltApplicationComponent: File
private lateinit var genTest1ComponentTreeDeps: File
private lateinit var genTest2ComponentTreeDeps: File
private lateinit var genTest1HiltComponents: File
private lateinit var genTest2HiltComponents: File
private lateinit var genTest1DaggerHiltApplicationComponent: File
private lateinit var genTest2DaggerHiltApplicationComponent: File
// Compiled classes
private lateinit var classSrcApp: File
private lateinit var classSrcActivity1: File
private lateinit var classSrcActivity2: File
private lateinit var classSrcModule1: File
private lateinit var classSrcModule2: File
private lateinit var classSrcTest1: File
private lateinit var classSrcTest2: File
private lateinit var classGenHiltApp: File
private lateinit var classGenHiltActivity1: File
private lateinit var classGenHiltActivity2: File
private lateinit var classGenAppInjector: File
private lateinit var classGenActivityInjector1: File
private lateinit var classGenActivityInjector2: File
private lateinit var classGenAppInjectorDeps: File
private lateinit var classGenActivityInjectorDeps1: File
private lateinit var classGenActivityInjectorDeps2: File
private lateinit var classGenModuleDeps1: File
private lateinit var classGenModuleDeps2: File
private lateinit var classGenComponentTreeDeps: File
private lateinit var classGenHiltComponents: File
private lateinit var classGenDaggerHiltApplicationComponent: File
private lateinit var classGenTest1ComponentTreeDeps: File
private lateinit var classGenTest2ComponentTreeDeps: File
private lateinit var classGenTest1HiltComponents: File
private lateinit var classGenTest2HiltComponents: File
private lateinit var classGenTest1DaggerHiltApplicationComponent: File
private lateinit var classGenTest2DaggerHiltApplicationComponent: File
// Timestamps of files
private lateinit var fileToTimestampMap: Map<File, Long>
// Sets of files that have changed/not changed/deleted
private lateinit var changedFiles: Set<File>
private lateinit var unchangedFiles: Set<File>
private lateinit var deletedFiles: Set<File>
private val compileTaskName = if (incapMode == ISOLATING_MODE) {
":hiltJavaCompileDebug"
} else {
":compileDebugJavaWithJavac"
}
private val testCompileTaskName = if (incapMode == ISOLATING_MODE) {
":hiltJavaCompileDebugUnitTest"
} else {
":compileDebugUnitTestJavaWithJavac"
}
private val aggregatingTaskName = ":hiltAggregateDepsDebug"
private val testAggregatingTaskName = ":hiltAggregateDepsDebugUnitTest"
@Before
fun setup() {
val projectRoot = testProjectDir.root
// copy test project
File("src/test/data/simple-project").copyRecursively(projectRoot)
// set up build file
File(projectRoot, "build.gradle").writeText(
"""
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
}
}
plugins {
id 'com.android.application'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 32
buildToolsVersion "32.0.0"
defaultConfig {
applicationId "hilt.simple"
minSdkVersion 21
targetSdkVersion 32
javaCompileOptions {
annotationProcessorOptions {
arguments += ["dagger.hilt.shareTestComponents" : "true"]
}
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
repositories {
mavenLocal()
google()
mavenCentral()
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.dagger:dagger:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT'
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'androidx.test:runner:1.3.0'
testImplementation 'org.robolectric:robolectric:4.4'
testImplementation 'com.google.dagger:hilt-android-testing:LOCAL-SNAPSHOT'
testAnnotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
hilt {
enableAggregatingTask = ${if (incapMode == ISOLATING_MODE) "true" else "false"}
}
""".trimIndent()
)
// Compute directory paths
val defaultGenSrcDir = "build/generated/ap_generated_sources/debug/out/"
val testDefaultGenSrcDir = "build/generated/ap_generated_sources/debugUnitTest/out/"
fun getComponentTreeDepsGenSrcDir(variant: String) = if (incapMode == ISOLATING_MODE) {
"build/generated/hilt/component_trees/$variant/"
} else {
"build/generated/ap_generated_sources/$variant/out/"
}
val componentTreeDepsGenSrcDir = getComponentTreeDepsGenSrcDir("debug")
val testComponentTreeDepsGenSrcDir = getComponentTreeDepsGenSrcDir("debugUnitTest")
fun getRootGenSrcDir(variant: String) = if (incapMode == ISOLATING_MODE) {
"build/generated/hilt/component_sources/$variant/"
} else {
"build/generated/ap_generated_sources/$variant/out/"
}
val rootGenSrcDir = getRootGenSrcDir("debug")
val testRootGenSrcDir = getRootGenSrcDir("debugUnitTest")
val defaultClassesDir = "build/intermediates/javac/debug/classes"
val testDefaultClassesDir = "build/intermediates/javac/debugUnitTest/classes"
fun getRootClassesDir(variant: String) = if (incapMode == ISOLATING_MODE) {
"build/intermediates/hilt/component_classes/$variant/"
} else {
"build/intermediates/javac/$variant/classes"
}
val rootClassesDir = getRootClassesDir("debug")
val testRootClassesDir = getRootClassesDir("debugUnitTest")
// Compute file paths
srcApp = File(projectRoot, "$MAIN_SRC_DIR/simple/SimpleApp.java")
srcActivity1 = File(projectRoot, "$MAIN_SRC_DIR/simple/Activity1.java")
srcActivity2 = File(projectRoot, "$MAIN_SRC_DIR/simple/Activity2.java")
srcModule1 = File(projectRoot, "$MAIN_SRC_DIR/simple/Module1.java")
srcModule2 = File(projectRoot, "$MAIN_SRC_DIR/simple/Module2.java")
srcTest1 = File(projectRoot, "$TEST_SRC_DIR/simple/Test1.java")
srcTest2 = File(projectRoot, "$TEST_SRC_DIR/simple/Test2.java")
genHiltApp = File(projectRoot, "$rootGenSrcDir/simple/Hilt_SimpleApp.java")
genHiltActivity1 = File(projectRoot, "$defaultGenSrcDir/simple/Hilt_Activity1.java")
genHiltActivity2 = File(projectRoot, "$defaultGenSrcDir/simple/Hilt_Activity2.java")
genAppInjector = File(projectRoot, "$defaultGenSrcDir/simple/SimpleApp_GeneratedInjector.java")
genActivityInjector1 =
File(projectRoot, "$defaultGenSrcDir/simple/Activity1_GeneratedInjector.java")
genActivityInjector2 =
File(projectRoot, "$defaultGenSrcDir/simple/Activity2_GeneratedInjector.java")
genAppInjectorDeps = File(
projectRoot,
"$defaultGenSrcDir/hilt_aggregated_deps/_simple_SimpleApp_GeneratedInjector.java"
)
genActivityInjectorDeps1 = File(
projectRoot,
"$defaultGenSrcDir/hilt_aggregated_deps/_simple_Activity1_GeneratedInjector.java"
)
genActivityInjectorDeps2 = File(
projectRoot,
"$defaultGenSrcDir/hilt_aggregated_deps/_simple_Activity2_GeneratedInjector.java"
)
genModuleDeps1 = File(
projectRoot,
"$defaultGenSrcDir/hilt_aggregated_deps/_simple_Module1.java"
)
genModuleDeps2 =
File(projectRoot, "$defaultGenSrcDir/hilt_aggregated_deps/_simple_Module2.java")
genComponentTreeDeps =
File(projectRoot, "$componentTreeDepsGenSrcDir/simple/SimpleApp_ComponentTreeDeps.java")
genHiltComponents = File(projectRoot, "$rootGenSrcDir/simple/SimpleApp_HiltComponents.java")
genDaggerHiltApplicationComponent = File(
projectRoot,
"$rootGenSrcDir/simple/DaggerSimpleApp_HiltComponents_SingletonC.java"
)
genTest1ComponentTreeDeps = File(
projectRoot,
testComponentTreeDepsGenSrcDir +
"/dagger/hilt/android/internal/testing/root/Test1_ComponentTreeDeps.java"
)
genTest2ComponentTreeDeps = File(
projectRoot,
testComponentTreeDepsGenSrcDir +
"/dagger/hilt/android/internal/testing/root/Test2_ComponentTreeDeps.java"
)
genTest1HiltComponents = File(
projectRoot,
"$testRootGenSrcDir/dagger/hilt/android/internal/testing/root/Test1_HiltComponents.java"
)
genTest2HiltComponents = File(
projectRoot,
"$testRootGenSrcDir/dagger/hilt/android/internal/testing/root/Test2_HiltComponents.java"
)
genTest1DaggerHiltApplicationComponent = File(
projectRoot,
testRootGenSrcDir +
"/dagger/hilt/android/internal/testing/root/DaggerTest1_HiltComponents_SingletonC.java"
)
genTest2DaggerHiltApplicationComponent = File(
projectRoot,
testRootGenSrcDir +
"/dagger/hilt/android/internal/testing/root/DaggerTest2_HiltComponents_SingletonC.java"
)
classSrcApp = File(projectRoot, "$defaultClassesDir/simple/SimpleApp.class")
classSrcActivity1 = File(projectRoot, "$defaultClassesDir/simple/Activity1.class")
classSrcActivity2 = File(projectRoot, "$defaultClassesDir/simple/Activity2.class")
classSrcModule1 = File(projectRoot, "$defaultClassesDir/simple/Module1.class")
classSrcModule2 = File(projectRoot, "$defaultClassesDir/simple/Module2.class")
classSrcTest1 = File(projectRoot, "$testDefaultClassesDir/simple/Test1.class")
classSrcTest2 = File(projectRoot, "$testDefaultClassesDir/simple/Test2.class")
classGenHiltApp = File(projectRoot, "$rootClassesDir/simple/Hilt_SimpleApp.class")
classGenHiltActivity1 = File(projectRoot, "$defaultClassesDir/simple/Hilt_Activity1.class")
classGenHiltActivity2 = File(projectRoot, "$defaultClassesDir/simple/Hilt_Activity2.class")
classGenAppInjector =
File(projectRoot, "$defaultClassesDir/simple/SimpleApp_GeneratedInjector.class")
classGenActivityInjector1 = File(
projectRoot,
"$defaultClassesDir/simple/Activity1_GeneratedInjector.class"
)
classGenActivityInjector2 = File(
projectRoot,
"$defaultClassesDir/simple/Activity2_GeneratedInjector.class"
)
classGenAppInjectorDeps = File(
projectRoot,
"$defaultClassesDir/hilt_aggregated_deps/_simple_SimpleApp_GeneratedInjector.class"
)
classGenActivityInjectorDeps1 = File(
projectRoot,
"$defaultClassesDir/hilt_aggregated_deps/_simple_Activity1_GeneratedInjector.class"
)
classGenActivityInjectorDeps2 = File(
projectRoot,
"$defaultClassesDir/hilt_aggregated_deps/_simple_Activity2_GeneratedInjector.class"
)
classGenModuleDeps1 =
File(projectRoot, "$defaultClassesDir/hilt_aggregated_deps/_simple_Module1.class")
classGenModuleDeps2 =
File(projectRoot, "$defaultClassesDir/hilt_aggregated_deps/_simple_Module2.class")
classGenComponentTreeDeps = File(
projectRoot,
"$rootClassesDir/simple/SimpleApp_ComponentTreeDeps.class"
)
classGenHiltComponents = File(
projectRoot,
"$rootClassesDir/simple/SimpleApp_HiltComponents.class"
)
classGenDaggerHiltApplicationComponent = File(
projectRoot,
"$rootClassesDir/simple/DaggerSimpleApp_HiltComponents_SingletonC.class"
)
classGenTest1ComponentTreeDeps = File(
projectRoot,
testRootClassesDir +
"/dagger/hilt/android/internal/testing/root/Test1_ComponentTreeDeps.class"
)
classGenTest2ComponentTreeDeps = File(
projectRoot,
testRootClassesDir +
"/dagger/hilt/android/internal/testing/root/Test2_ComponentTreeDeps.class"
)
classGenTest1HiltComponents = File(
projectRoot,
"$testRootClassesDir/dagger/hilt/android/internal/testing/root/Test1_HiltComponents.class"
)
classGenTest2HiltComponents = File(
projectRoot,
"$testRootClassesDir/dagger/hilt/android/internal/testing/root/Test2_HiltComponents.class"
)
classGenTest1DaggerHiltApplicationComponent = File(
projectRoot,
testRootClassesDir +
"/dagger/hilt/android/internal/testing/root/DaggerTest1_HiltComponents_SingletonC.class"
)
classGenTest2DaggerHiltApplicationComponent = File(
projectRoot,
testRootClassesDir +
"/dagger/hilt/android/internal/testing/root/DaggerTest2_HiltComponents_SingletonC.class"
)
}
@Test
fun firstFullBuild() {
// This test verifies the results of the first full (non-incremental) build. The other tests
// verify the results of the second incremental build based on different change scenarios.
val result = runFullBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
assertFilesExist(
listOf(
genHiltApp,
genHiltActivity1,
genHiltActivity2,
genAppInjector,
genActivityInjector1,
genActivityInjector2,
genAppInjectorDeps,
genActivityInjectorDeps1,
genActivityInjectorDeps2,
genModuleDeps1,
genModuleDeps2,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
)
// Check compilation outputs
assertFilesExist(
listOf(
classSrcApp,
classSrcActivity1,
classSrcActivity2,
classSrcModule1,
classSrcModule2,
classGenHiltApp,
classGenHiltActivity1,
classGenHiltActivity2,
classGenAppInjector,
classGenActivityInjector1,
classGenActivityInjector2,
classGenAppInjectorDeps,
classGenActivityInjectorDeps1,
classGenActivityInjectorDeps2,
classGenModuleDeps1,
classGenModuleDeps2,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
)
}
@Test
fun changeActivitySource_addPublicMethod() {
runFullBuild()
val componentTreeDepsFullBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
// Change Activity 1 source
searchAndReplace(
srcActivity1, "// Insert-change",
"""
@Override
public void onResume() {
super.onResume();
}
""".trimIndent()
)
val result = runIncrementalBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * Only activity 1 sources are re-generated, isolation in modules and from other activities
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task did not run, no change in deps
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE)
// * Components are re-generated due to a recompilation of a dep
listOf(
genHiltApp, // Re-gen because components got re-gen
genHiltActivity1,
genActivityInjector1,
genActivityInjectorDeps1,
genHiltComponents,
genDaggerHiltApplicationComponent
)
} else {
// * Root classes along with components are always re-generated (aggregated processor)
listOf(
genHiltApp,
genHiltActivity1,
genAppInjector,
genActivityInjector1,
genAppInjectorDeps,
genActivityInjectorDeps1,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
val componentTreeDepsIncrementalBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
expect.withMessage("Full build")
.that(componentTreeDepsFullBuild)
.isEqualTo(componentTreeDepsIncrementalBuild)
// Check compilation outputs
// * Gen sources from activity 1 are re-compiled
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classSrcActivity1,
classGenHiltApp,
classGenHiltActivity1,
classGenActivityInjector1,
classGenActivityInjectorDeps1,
classGenComponentTreeDeps, // Re-compiled because reference to activity injector
classGenHiltComponents,
classGenDaggerHiltApplicationComponent,
)
} else {
// * All aggregating processor gen sources are re-compiled
listOf(
classSrcActivity1,
classGenHiltApp,
classGenHiltActivity1,
classGenAppInjector,
classGenActivityInjector1,
classGenAppInjectorDeps,
classGenActivityInjectorDeps1,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun changeActivitySource_addPrivateMethod() {
runFullBuild()
val componentTreeDepsFullBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
// Change Activity 1 source
searchAndReplace(
srcActivity1, "// Insert-change",
"""
private void foo() { }
""".trimIndent()
)
val result = runIncrementalBuild()
val expectedOutcome = if (incapMode == ISOLATING_MODE) {
// In isolating mode, changes that do not affect ABI will not cause re-compilation.
TaskOutcome.UP_TO_DATE
} else {
TaskOutcome.SUCCESS
}
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(expectedOutcome)
// Check annotation processing outputs
// * Only activity 1 sources are re-generated, isolation in modules and from other activities
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task did not run, no change in deps
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE)
listOf(
genHiltActivity1,
genActivityInjector1,
genActivityInjectorDeps1,
)
} else {
// * Root classes along with components are always re-generated (aggregated processor)
listOf(
genHiltApp,
genHiltActivity1,
genAppInjector,
genActivityInjector1,
genAppInjectorDeps,
genActivityInjectorDeps1,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
val componentTreeDepsIncrementalBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
expect.withMessage("Full build")
.that(componentTreeDepsFullBuild)
.isEqualTo(componentTreeDepsIncrementalBuild)
// Check compilation outputs
// * Gen sources from activity 1 are re-compiled
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classSrcActivity1,
classGenHiltActivity1,
classGenActivityInjector1,
classGenActivityInjectorDeps1
)
} else {
// * All aggregating processor gen sources are re-compiled
listOf(
classSrcActivity1,
classGenHiltApp,
classGenHiltActivity1,
classGenAppInjector,
classGenActivityInjector1,
classGenAppInjectorDeps,
classGenActivityInjectorDeps1,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun changeModuleSource() {
runFullBuild()
val componentTreeDepsFullBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
// Change Module 1 source
searchAndReplace(
srcModule1, "// Insert-change",
"""
@Provides
static double provideDouble() {
return 10.10;
}
""".trimIndent()
)
val result = runIncrementalBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * Only module 1 sources are re-generated, isolation from other modules
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task did not run, no change in deps
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE)
// * Components are re-generated due to a recompilation of a dep
listOf(
genHiltApp, // Re-generated because components got re-generated
genModuleDeps1,
genHiltComponents,
genDaggerHiltApplicationComponent
)
} else {
// * Root classes along with components are always re-generated (aggregated processor)
listOf(
genHiltApp,
genAppInjector,
genAppInjectorDeps,
genModuleDeps1,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
val componentTreeDepsIncrementalBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
expect.withMessage("Full build")
.that(componentTreeDepsFullBuild)
.isEqualTo(componentTreeDepsIncrementalBuild)
// Check compilation outputs
// * Gen sources from module 1 are re-compiled
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classSrcModule1,
classGenHiltApp,
classGenModuleDeps1,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
} else {
// * All aggregating processor gen sources are re-compiled
listOf(
classSrcModule1,
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
classGenModuleDeps1,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun changeAppSource() {
runFullBuild()
val componentTreeDepsFullBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
// Change Application source
searchAndReplace(
srcApp, "// Insert-change",
"""
@Override
public void onCreate() {
super.onCreate();
}
""".trimIndent()
)
val result = runIncrementalBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * No modules or activities (or any other non-root) classes should be generated
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task did not run, no change in deps
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE)
// * Components are re-generated due to a recompilation of a dep
listOf(
genHiltApp, // Re-generated because components got re-generated
genAppInjector,
genAppInjectorDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
} else {
// * Root classes along with components are always re-generated (aggregated processor)
listOf(
genHiltApp,
genAppInjector,
genAppInjectorDeps,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
val componentTreeDepsIncrementalBuild = genComponentTreeDeps.readText(Charsets.UTF_8)
expect.withMessage("Full build")
.that(componentTreeDepsFullBuild)
.isEqualTo(componentTreeDepsIncrementalBuild)
// Check compilation outputs
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classSrcApp,
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
} else {
// * All aggregating processor gen sources are re-compiled
listOf(
classSrcApp,
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun deleteActivitySource() {
runFullBuild()
srcActivity2.delete()
val result = runIncrementalBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * All related gen classes from activity 2 should be deleted
// * Unrelated activities and modules are in isolation and should be unchanged
// * Root classes along with components are always re-generated (aggregated processor)
assertDeletedFiles(
listOf(
genHiltActivity2,
genActivityInjector2,
genActivityInjectorDeps2
)
)
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task ran due to a change in dep
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// * Components are re-generated since there was a change in dep
listOf(
genHiltApp, // Re-generated because components got re-generated
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
} else {
listOf(
genHiltApp,
genAppInjector,
genAppInjectorDeps,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
// Check compilation outputs
// * All compiled classes from activity 2 should be deleted
// * Unrelated activities and modules are in isolation and should be unchanged
assertDeletedFiles(
listOf(
classSrcActivity2,
classGenHiltActivity2,
classGenActivityInjector2,
classGenActivityInjectorDeps2
)
)
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classGenHiltApp,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
} else {
listOf(
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun deleteModuleSource() {
runFullBuild()
srcModule2.delete()
val result = runIncrementalBuild()
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * All related gen classes from module 2 should be deleted
// * Unrelated activities and modules are in isolation and should be unchanged
assertDeletedFiles(
listOf(genModuleDeps2)
)
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task ran due to a change in dep
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// * Components are re-generated since there was a change in dep
listOf(
genHiltApp, // Re-generated because components got re-generated
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
} else {
// * Root classes along with components are always re-generated (aggregated processor)
listOf(
genHiltApp,
genAppInjector,
genAppInjectorDeps,
genComponentTreeDeps,
genHiltComponents,
genDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
// Check compilation outputs
// * All compiled classes from module 2 should be deleted
// * Unrelated activities and modules are in isolation and should be unchanged
assertDeletedFiles(
listOf(
classSrcModule2,
classGenModuleDeps2
)
)
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
listOf(
classGenHiltApp,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
} else {
listOf(
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
classGenComponentTreeDeps,
classGenHiltComponents,
classGenDaggerHiltApplicationComponent
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun addNewSource() {
runFullBuild()
val newSource = File(testProjectDir.root, "$MAIN_SRC_DIR/simple/Foo.java")
newSource.writeText(
"""
package simple;
public class Foo { }
""".trimIndent()
)
val result = runIncrementalBuild()
val expectedOutcome = if (incapMode == ISOLATING_MODE) {
// In isolating mode, component compile task does not re-compile.
TaskOutcome.UP_TO_DATE
} else {
TaskOutcome.SUCCESS
}
expect.that(result.task(compileTaskName)!!.outcome).isEqualTo(expectedOutcome)
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
// * Aggregating task did not run, no change in deps
expect.that(result.task(aggregatingTaskName)!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE)
// * Non-DI related source causes no files to be generated
emptyList()
} else {
// * Root classes are always re-generated (aggregated processor)
listOf(
genHiltApp,
genAppInjector,
genAppInjectorDeps,
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)
val recompiledClassFiles = if (incapMode == ISOLATING_MODE) {
emptyList()
} else {
listOf(
classGenHiltApp,
classGenAppInjector,
classGenAppInjectorDeps,
)
}
assertChangedFiles(FileType.CLASS, recompiledClassFiles)
}
@Test
fun firstTestFullBuild() {
val result = runFullTestBuild()
expect.that(result.task(testCompileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertFilesExist(
listOf(
genTest1ComponentTreeDeps,
genTest2ComponentTreeDeps,
genTest1HiltComponents,
genTest2HiltComponents,
genTest1DaggerHiltApplicationComponent,
genTest2DaggerHiltApplicationComponent,
)
)
assertFilesExist(
listOf(
classSrcTest1,
classSrcTest2,
classGenTest1ComponentTreeDeps,
classGenTest2ComponentTreeDeps,
classGenTest1HiltComponents,
classGenTest2HiltComponents,
classGenTest1DaggerHiltApplicationComponent,
classGenTest2DaggerHiltApplicationComponent,
)
)
}
@Test
fun changeTestSource_addPublicMethod() {
runFullTestBuild()
val test1ComponentTreeDepsFullBuild = genTest1ComponentTreeDeps.readText(Charsets.UTF_8)
val test2ComponentTreeDepsFullBuild = genTest2ComponentTreeDeps.readText(Charsets.UTF_8)
// Change Test 1 source
searchAndReplace(
srcTest1, "// Insert-change",
"""
@Test
public void newTest() { }
""".trimIndent()
)
val result = runIncrementalTestBuild()
expect.that(result.task(testCompileTaskName)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
// Check annotation processing outputs
// * Unrelated test components should be unchanged
val regeneratedSourceFiles = if (incapMode == ISOLATING_MODE) {
listOf(
genTest1HiltComponents,
genTest1DaggerHiltApplicationComponent,
)
} else {
listOf(
genTest1ComponentTreeDeps,
genTest2ComponentTreeDeps,
genTest1HiltComponents,
genTest2HiltComponents,
genTest1DaggerHiltApplicationComponent,
genTest2DaggerHiltApplicationComponent,
)
}
assertChangedFiles(FileType.JAVA, regeneratedSourceFiles)