@@ -4,8 +4,6 @@ import org.utbot.common.PathUtil.toPath
44import org.utbot.common.WorkaroundReason
55import org.utbot.common.workaround
66import org.utbot.framework.plugin.api.CodegenLanguage
7- import org.utbot.intellij.plugin.ui.CommonErrorNotifier
8- import org.utbot.intellij.plugin.ui.UnsupportedJdkNotifier
97import com.intellij.openapi.command.WriteCommandAction
108import com.intellij.openapi.externalSystem.model.ProjectSystemId
119import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
@@ -14,9 +12,6 @@ import com.intellij.openapi.module.ModuleManager
1412import com.intellij.openapi.module.ModuleUtilCore
1513import com.intellij.openapi.project.Project
1614import com.intellij.openapi.project.guessModuleDir
17- import com.intellij.openapi.projectRoots.JavaSdk
18- import com.intellij.openapi.projectRoots.JavaSdkVersion
19- import com.intellij.openapi.projectRoots.Sdk
2015import com.intellij.openapi.roots.ContentEntry
2116import com.intellij.openapi.roots.ModifiableRootModel
2217import com.intellij.openapi.roots.ModuleRootManager
@@ -29,26 +24,30 @@ import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile
2924import com.intellij.util.PathUtil.getParentPath
3025import java.nio.file.Path
3126import mu.KotlinLogging
32- import org.jetbrains.android.sdk.AndroidSdkType
3327import org.jetbrains.jps.model.module.JpsModuleSourceRootType
3428import org.jetbrains.kotlin.config.KotlinFacetSettingsProvider
3529import org.jetbrains.kotlin.config.TestResourceKotlinRootType
3630import org.jetbrains.kotlin.platform.TargetPlatformVersion
3731
3832private val logger = KotlinLogging .logger {}
3933
40- data class TestSourceRoot (
41- val dir : VirtualFile ,
42- val expectedLanguage : CodegenLanguage
43- )
34+ interface ITestSourceRoot {
35+ val dirPath: String
36+ val dirName: String
37+ val dir: VirtualFile ?
38+ val expectedLanguage : CodegenLanguage
39+ }
4440
45- /* *
46- * @return jdk version of the module
47- */
48- fun Module.jdkVersion (): JavaSdkVersion {
49- val moduleRootManager = ModuleRootManager .getInstance(this )
50- val sdk = moduleRootManager.sdk
51- return jdkVersionBy(sdk)
41+ class TestSourceRoot (override val dir : VirtualFile , override val expectedLanguage : CodegenLanguage ) : ITestSourceRoot {
42+ override val dirPath: String = dir.toNioPath().toString()
43+ override val dirName: String = dir.name
44+
45+ override fun toString () = dirPath
46+
47+ override fun equals (other : Any? ) =
48+ other is TestSourceRoot && dir == other.dir && expectedLanguage == other.expectedLanguage
49+
50+ override fun hashCode () = 31 * dir.hashCode() + expectedLanguage.hashCode()
5251}
5352
5453/* *
@@ -107,11 +106,11 @@ private fun findPotentialModulesForTests(project: Project, srcModule: Module): L
107106 if (modules.isNotEmpty()) return modules
108107
109108 if (srcModule.suitableTestSourceFolders().isEmpty()) {
110- val modules = mutableSetOf<Module >()
111- ModuleUtilCore .collectModulesDependsOn(srcModule, modules)
112- modules.remove(srcModule)
109+ val modulesWithTestRoot = mutableSetOf<Module >().also {
110+ ModuleUtilCore .collectModulesDependsOn(srcModule, it)
111+ it.remove(srcModule)
112+ }.filter { it.suitableTestSourceFolders().isNotEmpty() }
113113
114- val modulesWithTestRoot = modules.filter { it.suitableTestSourceFolders().isNotEmpty() }
115114 if (modulesWithTestRoot.size == 1 ) return modulesWithTestRoot
116115 }
117116 return listOf (srcModule)
@@ -165,11 +164,11 @@ val Project.isBuildWithGradle get() =
165164
166165const val dedicatedTestSourceRootName = " utbot_tests"
167166
168- fun Module.addDedicatedTestRoot (testSourceRoots : MutableList <TestSourceRoot >, language : CodegenLanguage ): VirtualFile ? {
169- // Don't suggest new test source roots for Gradle project where 'unexpected' test roots won't work
167+ fun Module.addDedicatedTestRoot (testSourceRoots : MutableList <ITestSourceRoot >, language : CodegenLanguage ): VirtualFile ? {
168+ // Don't suggest new test source roots for a Gradle project where 'unexpected' test roots won't work
170169 if (project.isBuildWithGradle) return null
171170 // Dedicated test root already exists
172- if (testSourceRoots.any { root -> root.dir.name == dedicatedTestSourceRootName }) return null
171+ if (testSourceRoots.any { root -> root.dir? .name == dedicatedTestSourceRootName }) return null
173172
174173 val moduleInstance = ModuleRootManager .getInstance(this )
175174 val testFolder = moduleInstance.contentEntries.flatMap { it.sourceFolders.toList() }
@@ -256,34 +255,6 @@ fun ContentEntry.addSourceRootIfAbsent(
256255 }
257256}
258257
259- /* *
260- * Obtain JDK version and make sure that it is JDK8 or JDK11
261- */
262- private fun jdkVersionBy (sdk : Sdk ? ): JavaSdkVersion {
263- if (sdk == null ) {
264- CommonErrorNotifier .notify(" Failed to obtain JDK version of the project" )
265- }
266- requireNotNull(sdk)
267-
268- val jdkVersion = when (sdk.sdkType) {
269- is JavaSdk -> {
270- (sdk.sdkType as JavaSdk ).getVersion(sdk)
271- }
272- is AndroidSdkType -> {
273- ((sdk.sdkType as AndroidSdkType ).dependencyType as JavaSdk ).getVersion(sdk)
274- }
275- else -> null
276- }
277- if (jdkVersion == null ) {
278- CommonErrorNotifier .notify(" Failed to obtain JDK version of the project" )
279- }
280- requireNotNull(jdkVersion)
281- if (! jdkVersion.isAtLeast(JavaSdkVersion .JDK_1_8 )) {
282- UnsupportedJdkNotifier .notify(jdkVersion.description)
283- }
284- return jdkVersion
285- }
286-
287258private val SourceFolder .expectedLanguageForTests: CodegenLanguage ?
288259 get() {
289260 // unfortunately, Gradle creates Kotlin test source root with Java source root type, so type is misleading,
0 commit comments