Last active
April 10, 2025 03:00
-
-
Save gerin98/1ea38c5e55376228d115f69681a89247 to your computer and use it in GitHub Desktop.
Heuristic for determining which tests to run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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