Skip to content

Instantly share code, notes, and snippets.

@gerin98
Last active April 10, 2025 03:00
Show Gist options
  • Select an option

  • Save gerin98/1ea38c5e55376228d115f69681a89247 to your computer and use it in GitHub Desktop.

Select an option

Save gerin98/1ea38c5e55376228d115f69681a89247 to your computer and use it in GitHub Desktop.
Heuristic for determining which tests to run
fun getTestFilesForChangedFeatureModules(projectDir: File, testDir: File, featureModulesNames: Set<String>): Set<String> {
val testFilesToRun = HashSet<String>()
testDir.walkTopDown().filter { !it.isDirectory }.forEach { testFile ->
val modulesRelevantToTests = analyzeEndToEndTestFile(testFile.absolutePath)
val shouldRunTests = modulesRelevantToTests.intersect(featureModulesNames).any()
if (shouldRunTests) {
val testPattern = testFile.relativeTo(projectDir).path
.substringAfter("java/")
.replace('/', '.')
.removeSuffix(".kt")
testFilesToRun.add(testPattern)
}
}
return testFilesToRun
}
fun analyzeEndToEndTestFile(filename: String): Set<String> {
val importPrefix = "com.wf.test.actions"
val parsed = KotlinGrammarAntlrKotlinParser.parseKotlinFile(AstSource.File(filename)) as DefaultAstNode
return (parsed.children.filter("importList").firstOrNull() as? DefaultAstNode)
?.children
?.filter("importHeader")
?.asSequence()
?.filterIsInstance<DefaultAstNode>()
?.flatMap { it.children.filter("identifier").filterIsInstance<DefaultAstNode>() }
?.map { node -> extractTerminals(node).joinToString("") { it.text } }
?.filter { it.startsWith(importPrefix) }
?.map { it.removePrefix("$importPrefix.").substringBefore('.') }
?.toSet()
.orEmpty()
}
fun extractTerminals(node: DefaultAstNode): List<DefaultAstTerminal> =
node.children
.filterIsInstance<DefaultAstNode>()
.flatMap(::extractTerminals) +
node.children.filterIsInstance<DefaultAstTerminal>()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment