Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / lint-rules / src / test / java / com / vayunmathur / lint / FixtureDeterminismDetectorTest.kt

FixtureDeterminismDetectorTest.kt230 lines · 7.4 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.checks.infrastructure.LintDetectorTest
4import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin
5import com.android.tools.lint.checks.infrastructure.TestMode
6import com.android.tools.lint.detector.api.Detector
7import com.android.tools.lint.detector.api.Issue
8import org.junit.Test
9import org.junit.runner.RunWith
10import org.junit.runners.JUnit4
11
12@RunWith(JUnit4::class)
13class FixtureDeterminismDetectorTest : LintDetectorTest() {
14
15 override fun getDetector(): Detector = FixtureDeterminismDetector()
16
17 override fun getIssues(): List<Issue> = listOf(FixtureDeterminismDetector.ISSUE)
18
19 @Test
20 fun screenshotRelativeNowIsFlagged() {
21 lint().files(kotlin(
22 "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt",
23 """
24 package com.vayunmathur.example.ui
25 import kotlin.time.Clock
26 private val now = Clock.System.now()
27 class MetadataPreviews
28 """.trimIndent(),
29 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
30 }
31
32 @Test
33 fun screenshotFixedConstantIsClean() {
34 lint().files(kotlin(
35 "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt",
36 """
37 package com.vayunmathur.example.ui
38 import kotlin.time.Instant
39 private val FIXED_NOW = Instant.fromEpochMilliseconds(1_768_651_200_000L)
40 class MetadataPreviews
41 """.trimIndent(),
42 )).testModes(TestMode.DEFAULT).run().expectClean()
43 }
44
45 @Test
46 fun unitFixtureRelativeNowIsFlagged() {
47 lint().files(kotlin(
48 "src/test/java/com/vayunmathur/example/FooTest.kt",
49 """
50 package com.vayunmathur.example
51 class FooTest {
52 val stamped = System.currentTimeMillis()
53 }
54 """.trimIndent(),
55 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
56 }
57
58 @Test
59 fun unitFixtureLocalDateNowIsFlagged() {
60 lint().files(kotlin(
61 "src/test/java/com/vayunmathur/example/FooTest.kt",
62 """
63 package com.vayunmathur.example
64 import java.time.LocalDate
65 class FooTest {
66 val today = LocalDate.now()
67 }
68 """.trimIndent(),
69 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
70 }
71
72 @Test
73 fun deadlinePollingIsClean() {
74 lint().files(kotlin(
75 "src/test/java/com/vayunmathur/example/FooTest.kt",
76 """
77 package com.vayunmathur.example
78 class FooTest {
79 fun await() {
80 val deadline = System.currentTimeMillis() + 5_000
81 while (System.currentTimeMillis() < deadline) { Thread.sleep(50) }
82 }
83 }
84 """.trimIndent(),
85 )).testModes(TestMode.DEFAULT).run().expectClean()
86 }
87
88 @Test
89 fun elapsedMeasurementIsClean() {
90 lint().files(kotlin(
91 "src/test/java/com/vayunmathur/example/FooTest.kt",
92 """
93 package com.vayunmathur.example
94 class FooTest {
95 fun timed() {
96 val started = System.currentTimeMillis()
97 println(System.currentTimeMillis() - started)
98 }
99 }
100 """.trimIndent(),
101 )).testModes(TestMode.DEFAULT).run().expectClean()
102 }
103
104 @Test
105 fun relativeFixtureOffsetIsFlagged() {
106 // now() - <numeric offset> builds a fixture timestamp, not a measurement.
107 lint().files(kotlin(
108 "src/test/java/com/vayunmathur/example/FooTest.kt",
109 """
110 package com.vayunmathur.example
111 class FooTest {
112 val state = System.currentTimeMillis() - 60_000L
113 }
114 """.trimIndent(),
115 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
116 }
117
118 @Test
119 fun unseededRandomAndUuidAreFlagged() {
120 lint().files(kotlin(
121 "src/test/java/com/vayunmathur/example/FooTest.kt",
122 """
123 package com.vayunmathur.example
124 import java.util.UUID
125 import kotlin.random.Random
126 class FooTest {
127 val a = Random()
128 val b = UUID.randomUUID()
129 }
130 """.trimIndent(),
131 )).testModes(TestMode.DEFAULT).run().expectErrorCount(2)
132 }
133
134 @Test
135 fun seededRandomIsClean() {
136 lint().files(kotlin(
137 "src/test/java/com/vayunmathur/example/FooTest.kt",
138 """
139 package com.vayunmathur.example
140 import kotlin.random.Random
141 class FooTest {
142 val a = Random(42)
143 val b = Random(seed = 7)
144 }
145 """.trimIndent(),
146 )).testModes(TestMode.DEFAULT).run().expectClean()
147 }
148
149 @Test
150 fun mentionInCommentAndStringIsClean() {
151 lint().files(kotlin(
152 "src/test/java/com/vayunmathur/example/FooTest.kt",
153 """
154 package com.vayunmathur.example
155 // Never call Clock.System.now() in a fixture.
156 class FooTest {
157 val doc = "System.currentTimeMillis() is banned here"
158 }
159 """.trimIndent(),
160 )).testModes(TestMode.DEFAULT).run().expectClean()
161 }
162
163 @Test
164 fun exceptionMarkerOptsOut() {
165 lint().files(kotlin(
166 "src/test/java/com/vayunmathur/example/FooTest.kt",
167 """
168 package com.vayunmathur.example
169 // FIXTURE RELATIVE-TIME EXCEPTION: seeds relative to the repository's own real-clock stamps
170 class FooTest {
171 val stamped = System.currentTimeMillis()
172 }
173 """.trimIndent(),
174 )).testModes(TestMode.DEFAULT).run().expectClean()
175 }
176
177 @Test
178 fun mainSourceIsOutOfScope() {
179 lint().files(kotlin(
180 "src/main/java/com/vayunmathur/example/Foo.kt",
181 """
182 package com.vayunmathur.example
183 class Foo {
184 val stamped = System.currentTimeMillis()
185 }
186 """.trimIndent(),
187 )).testModes(TestMode.DEFAULT).run().expectClean()
188 }
189}
190
191@RunWith(JUnit4::class)
192class FixtureMarkerFormatTest : LintDetectorTest() {
193
194 override fun getDetector(): Detector = ExceptionMarkerFormatDetector()
195
196 override fun getIssues(): List<Issue> = listOf(ExceptionMarkerFormatDetector.ISSUE)
197
198 @Test
199 fun wellFormedFixtureMarkerIsClean() {
200 lint().files(kotlin(
201 """
202 package com.vayunmathur.example
203 // FIXTURE RELATIVE-TIME EXCEPTION: SUT stamps real now internally
204 class Foo
205 """.trimIndent(),
206 )).testModes(TestMode.DEFAULT).run().expectClean()
207 }
208
209 @Test
210 fun fixtureMarkerWithoutReasonIsFlagged() {
211 lint().files(kotlin(
212 """
213 package com.vayunmathur.example
214 // FIXTURE RELATIVE-TIME EXCEPTION:
215 class Foo
216 """.trimIndent(),
217 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
218 }
219
220 @Test
221 fun fixtureMarkerWithoutColonIsFlagged() {
222 lint().files(kotlin(
223 """
224 package com.vayunmathur.example
225 // FIXTURE RELATIVE-TIME EXCEPTION SUT stamps real now
226 class Foo
227 """.trimIndent(),
228 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
229 }
230}