Skip to content

Commit cab4799

Browse files
mmvpmNikita Stroganov
and
Nikita Stroganov
authored
Fix wrong test resources directory in Android Studio (#78)
* #70 Fix wrong test resources directory in Android Studio * #70 Change the order of suitable test folders * #70 Fix after review Co-authored-by: Nikita Stroganov <stroganov.nikita@huawei.com>
1 parent 7e521b9 commit cab4799

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ object TestGenerator {
9595
}
9696

9797
private fun mergeSarifReports(model: GenerateTestsModel) {
98-
val sarifReportsPath = model.testModule.getOrCreateSarifReportsPath()
98+
val sarifReportsPath = model.testModule.getOrCreateSarifReportsPath(model.testSourceRoot)
9999
val sarifReports = sarifReportsPath.toFile()
100100
.walkTopDown()
101101
.filter { it.extension == "sarif" }
@@ -294,7 +294,7 @@ object TestGenerator {
294294
}
295295

296296
private fun saveTestsReport(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) {
297-
val testResourcesDirPath = model.testModule.getOrCreateTestResourcesPath()
297+
val testResourcesDirPath = model.testModule.getOrCreateTestResourcesPath(model.testSourceRoot)
298298

299299
require(testResourcesDirPath.exists()) {
300300
"Test resources directory $testResourcesDirPath does not exist"

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/sarif/SarifReportIdea.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object SarifReportIdea {
2121
) {
2222
// building the path to the report file
2323
val classFqn = testCases.firstOrNull()?.method?.clazz?.qualifiedName ?: return
24-
val sarifReportsPath = model.testModule.getOrCreateSarifReportsPath()
24+
val sarifReportsPath = model.testModule.getOrCreateSarifReportsPath(model.testSourceRoot)
2525
val reportFilePath = sarifReportsPath.resolve("${classFqnToPath(classFqn)}-utbot.sarif")
2626

2727
// creating report related directory

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
575575
}
576576

577577
private fun configureStaticMocking() {
578-
val testResourcesUrl = model.testModule.getOrCreateTestResourcesPath()
578+
val testResourcesUrl = model.testModule.getOrCreateTestResourcesPath(model.testSourceRoot)
579579
configureMockitoResources(testResourcesUrl)
580580

581581
val staticsMockingValue = staticsMocking.item

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/ModuleUtils.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ fun Module.suitableTestSourceFolders(): List<SourceFolder> =
6464
*
6565
* If no roots exist, our suggestion is a folder named "resources" in the entry root.
6666
*/
67-
fun Module.getOrCreateTestResourcesPath(): Path {
68-
val testResourcesUrl = getOrCreateTestResourcesUrl(this)
67+
fun Module.getOrCreateTestResourcesPath(testSourceRoot: VirtualFile?): Path {
68+
val testResourcesUrl = getOrCreateTestResourcesUrl(this, testSourceRoot)
6969
return VfsUtilCore.urlToPath(testResourcesUrl).toPath()
7070
}
7171

7272
/**
7373
* Gets a path to Sarif reports directory or creates it.
74-
*
7574
*/
76-
fun Module.getOrCreateSarifReportsPath(): Path {
77-
val testResourcesPath = this.getOrCreateTestResourcesPath()
75+
fun Module.getOrCreateSarifReportsPath(testSourceRoot: VirtualFile?): Path {
76+
val testResourcesPath = this.getOrCreateTestResourcesPath(testSourceRoot)
7877
return "$testResourcesPath/sarif/".toPath()
7978
}
8079

@@ -137,21 +136,33 @@ private fun Module.suitableTestSourceFolders(codegenLanguage: CodegenLanguage):
137136

138137
return sourceFolders
139138
.filterNot { it.isForGeneratedSources() }
140-
.filter { folder -> folder.rootType == codegenLanguage.testRootType() }
139+
.filter { it.rootType == codegenLanguage.testRootType() }
140+
// Heuristics: User is more likely to choose the shorter path
141+
.sortedBy { it.url.length }
141142
}
142143

143144
private const val resourcesSuffix = "/resources"
144145

145-
private fun getOrCreateTestResourcesUrl(module: Module): String {
146+
private fun getOrCreateTestResourcesUrl(module: Module, testSourceRoot: VirtualFile?): String {
146147
val moduleInstance = ModuleRootManager.getInstance(module)
147148
val sourceFolders = moduleInstance.contentEntries.flatMap { it.sourceFolders.toList() }
148149

149-
val testResourcesFolder = sourceFolders.firstOrNull { it.rootType in testResourceRootTypes }
150+
val testResourcesFolder = sourceFolders
151+
.filter { sourceFolder ->
152+
sourceFolder.rootType in testResourceRootTypes && !sourceFolder.isForGeneratedSources()
153+
}
154+
// taking the source folder that has the maximum common prefix
155+
// with `testSourceRoot`, which was selected by the user
156+
.maxBy { sourceFolder ->
157+
val sourceFolderPath = sourceFolder.file?.path ?: ""
158+
val testSourceRootPath = testSourceRoot?.path ?: ""
159+
sourceFolderPath.commonPrefixWith(testSourceRootPath).length
160+
}
150161
if (testResourcesFolder != null) {
151162
return testResourcesFolder.url
152163
}
153164

154-
val testFolder = sourceFolders.firstOrNull { f -> f.rootType in testSourceRootTypes }
165+
val testFolder = sourceFolders.firstOrNull { it.rootType in testSourceRootTypes }
155166
val contentEntry = testFolder?.contentEntry ?: moduleInstance.contentEntries.first()
156167

157168
val parentFolderUrl = testFolder?.let { getParentPath(testFolder.url) }

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import org.jetbrains.kotlin.config.TestResourceKotlinRootType
1313
import org.jetbrains.kotlin.config.TestSourceKotlinRootType
1414

1515
val sourceRootTypes: Set<JpsModuleSourceRootType<JavaSourceRootProperties>> = setOf(JavaSourceRootType.SOURCE, SourceKotlinRootType)
16-
val testSourceRootTypes: Set<JpsModuleSourceRootType<JavaSourceRootProperties>> = setOf(JavaSourceRootType.TEST_SOURCE, TestSourceKotlinRootType)
17-
val resourceRootTypes: Set<JpsModuleSourceRootType<JavaResourceRootProperties>> = setOf(JavaResourceRootType.RESOURCE, ResourceKotlinRootType)
18-
val testResourceRootTypes: Set<JpsModuleSourceRootType<JavaResourceRootProperties>> = setOf(JavaResourceRootType.TEST_RESOURCE, TestResourceKotlinRootType)
16+
val testSourceRootTypes: Set<JpsModuleSourceRootType<JavaSourceRootProperties>> = setOf(JavaSourceRootType.TEST_SOURCE, TestSourceKotlinRootType)
17+
val resourceRootTypes: Set<JpsModuleSourceRootType<JavaResourceRootProperties>> = setOf(JavaResourceRootType.RESOURCE, ResourceKotlinRootType)
18+
val testResourceRootTypes: Set<JpsModuleSourceRootType<JavaResourceRootProperties>> = setOf(JavaResourceRootType.TEST_RESOURCE, TestResourceKotlinRootType)
1919

2020
/**
2121
* Defines test root type for selected codegen language.
@@ -43,4 +43,4 @@ fun SourceFolder.isForGeneratedSources(): Boolean {
4343
val resourceProperties = jpsElement.getProperties(resourceRootTypes + testResourceRootTypes)
4444

4545
return properties?.isForGeneratedSources == true && resourceProperties?.isForGeneratedSources == true
46-
}
46+
}

0 commit comments

Comments
 (0)