package com.vayunmathur.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin import com.android.tools.lint.checks.infrastructure.TestMode import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class FixtureDeterminismDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = FixtureDeterminismDetector() override fun getIssues(): List = listOf(FixtureDeterminismDetector.ISSUE) @Test fun screenshotRelativeNowIsFlagged() { lint().files(kotlin( "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt", """ package com.vayunmathur.example.ui import kotlin.time.Clock private val now = Clock.System.now() class MetadataPreviews """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun screenshotFixedConstantIsClean() { lint().files(kotlin( "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt", """ package com.vayunmathur.example.ui import kotlin.time.Instant private val FIXED_NOW = Instant.fromEpochMilliseconds(1_768_651_200_000L) class MetadataPreviews """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun unitFixtureRelativeNowIsFlagged() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example class FooTest { val stamped = System.currentTimeMillis() } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun unitFixtureLocalDateNowIsFlagged() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example import java.time.LocalDate class FooTest { val today = LocalDate.now() } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun deadlinePollingIsClean() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example class FooTest { fun await() { val deadline = System.currentTimeMillis() + 5_000 while (System.currentTimeMillis() < deadline) { Thread.sleep(50) } } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun elapsedMeasurementIsClean() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example class FooTest { fun timed() { val started = System.currentTimeMillis() println(System.currentTimeMillis() - started) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun relativeFixtureOffsetIsFlagged() { // now() - builds a fixture timestamp, not a measurement. lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example class FooTest { val state = System.currentTimeMillis() - 60_000L } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun unseededRandomAndUuidAreFlagged() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example import java.util.UUID import kotlin.random.Random class FooTest { val a = Random() val b = UUID.randomUUID() } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(2) } @Test fun seededRandomIsClean() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example import kotlin.random.Random class FooTest { val a = Random(42) val b = Random(seed = 7) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun mentionInCommentAndStringIsClean() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example // Never call Clock.System.now() in a fixture. class FooTest { val doc = "System.currentTimeMillis() is banned here" } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun exceptionMarkerOptsOut() { lint().files(kotlin( "src/test/java/com/vayunmathur/example/FooTest.kt", """ package com.vayunmathur.example // FIXTURE RELATIVE-TIME EXCEPTION: seeds relative to the repository's own real-clock stamps class FooTest { val stamped = System.currentTimeMillis() } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun mainSourceIsOutOfScope() { lint().files(kotlin( "src/main/java/com/vayunmathur/example/Foo.kt", """ package com.vayunmathur.example class Foo { val stamped = System.currentTimeMillis() } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } } @RunWith(JUnit4::class) class FixtureMarkerFormatTest : LintDetectorTest() { override fun getDetector(): Detector = ExceptionMarkerFormatDetector() override fun getIssues(): List = listOf(ExceptionMarkerFormatDetector.ISSUE) @Test fun wellFormedFixtureMarkerIsClean() { lint().files(kotlin( """ package com.vayunmathur.example // FIXTURE RELATIVE-TIME EXCEPTION: SUT stamps real now internally class Foo """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun fixtureMarkerWithoutReasonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // FIXTURE RELATIVE-TIME EXCEPTION: class Foo """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun fixtureMarkerWithoutColonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // FIXTURE RELATIVE-TIME EXCEPTION SUT stamps real now class Foo """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } }