@@ -28,15 +28,14 @@ import org.utbot.intellij.plugin.ui.utils.findFrameworkLibrary
28
28
import org.utbot.intellij.plugin.ui.utils.getOrCreateTestResourcesPath
29
29
import org.utbot.intellij.plugin.ui.utils.kotlinTargetPlatform
30
30
import org.utbot.intellij.plugin.ui.utils.parseVersion
31
- import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
32
31
import org.utbot.intellij.plugin.ui.utils.testResourceRootTypes
32
+ import org.utbot.intellij.plugin.ui.utils.addSourceRootIfAbsent
33
33
import org.utbot.intellij.plugin.ui.utils.testRootType
34
34
import com.intellij.ide.impl.ProjectNewWindowDoNotAskOption
35
35
import com.intellij.openapi.application.runWriteAction
36
36
import com.intellij.openapi.command.WriteCommandAction
37
37
import com.intellij.openapi.components.service
38
38
import com.intellij.openapi.options.ShowSettingsUtil
39
- import com.intellij.openapi.projectRoots.JavaSdkVersion
40
39
import com.intellij.openapi.roots.ContentEntry
41
40
import com.intellij.openapi.roots.DependencyScope
42
41
import com.intellij.openapi.roots.ExternalLibraryDescriptor
@@ -48,10 +47,12 @@ import com.intellij.openapi.ui.DialogPanel
48
47
import com.intellij.openapi.ui.DialogWrapper
49
48
import com.intellij.openapi.ui.Messages
50
49
import com.intellij.openapi.ui.ValidationInfo
51
- import com.intellij.openapi.ui.popup.IconButton
50
+ import com.intellij.openapi.util.Computable
51
+ import com.intellij.openapi.vfs.StandardFileSystems
52
52
import com.intellij.openapi.vfs.VfsUtil
53
53
import com.intellij.openapi.vfs.VfsUtilCore.urlToPath
54
54
import com.intellij.openapi.vfs.VirtualFile
55
+ import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile
55
56
import com.intellij.psi.PsiClass
56
57
import com.intellij.psi.PsiManager
57
58
import com.intellij.psi.PsiMethod
@@ -74,14 +75,9 @@ import com.intellij.ui.layout.Row
74
75
import com.intellij.ui.layout.panel
75
76
import com.intellij.util.IncorrectOperationException
76
77
import com.intellij.util.io.exists
77
- import com.intellij.util.lang.JavaVersion
78
- import com.intellij.util.ui.JBUI
79
- import com.intellij.util.ui.JBUI.Borders.empty
80
- import com.intellij.util.ui.JBUI.Borders.merge
81
- import com.intellij.util.ui.JBUI.scale
82
78
import com.intellij.util.ui.JBUI.size
79
+ import com.intellij.util.ui.JBUI
83
80
import com.intellij.util.ui.UIUtil
84
- import com.intellij.util.ui.components.BorderLayoutPanel
85
81
import java.awt.BorderLayout
86
82
import java.nio.file.Files
87
83
import java.nio.file.Path
@@ -274,16 +270,16 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
274
270
275
271
private fun getTestRoot () : VirtualFile ? {
276
272
model.testSourceRoot?.let {
277
- if (it.isDirectory) return it
273
+ if (it.isDirectory || it is FakeVirtualFile ) return it
278
274
}
279
275
return null
280
276
}
281
277
282
278
override fun doValidate (): ValidationInfo ? {
283
- if (getTestRoot() == null ) {
284
- return ValidationInfo (" Test source root is not configured" , testSourceFolderField.childComponent)
285
- }
286
- if (getRootDirectoryAndContentEntry( ) == null ) {
279
+ val testRoot = getTestRoot()
280
+ ? : return ValidationInfo (" Test source root is not configured" , testSourceFolderField.childComponent)
281
+
282
+ if (findReadOnlyContentEntry(testRoot ) == null ) {
287
283
return ValidationInfo (" Test source root is located out of content entry" , testSourceFolderField.childComponent)
288
284
}
289
285
@@ -373,18 +369,33 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
373
369
* Creates test source root if absent and target packages for tests.
374
370
*/
375
371
private fun createTestRootAndPackages (): Boolean {
376
- val (sourceRoot, contentEntry) = getRootDirectoryAndContentEntry() ? : return false
377
- val modifiableModel = ModuleRootManager .getInstance(model.testModule).modifiableModel
378
- VfsUtil .createDirectoryIfMissing(urlToPath(sourceRoot.url))
379
- contentEntry.addSourceFolder(sourceRoot.url, codegenLanguages.item.testRootType())
380
- WriteCommandAction .runWriteCommandAction(model.project) { modifiableModel.commit() }
372
+ model.testSourceRoot = createDirectoryIfMissing(model.testSourceRoot)
373
+ val testSourceRoot = model.testSourceRoot ? : return false
374
+ if (model.testSourceRoot?.isDirectory != true ) return false
375
+ if (getOrCreateTestRoot(testSourceRoot)) {
376
+ if (cbSpecifyTestPackage.isSelected) {
377
+ createSelectedPackage(testSourceRoot)
378
+ } else {
379
+ createPackagesByClasses(testSourceRoot)
380
+ }
381
+ return true
382
+ }
383
+ return false
384
+ }
381
385
382
- if (cbSpecifyTestPackage.isSelected) {
383
- createSelectedPackage(sourceRoot)
386
+ private fun createDirectoryIfMissing (dir : VirtualFile ? ): VirtualFile ? {
387
+ val file = if (dir is FakeVirtualFile ) {
388
+ WriteCommandAction .runWriteCommandAction(model.project, Computable <VirtualFile > {
389
+ VfsUtil .createDirectoryIfMissing(dir.path)
390
+ })
384
391
} else {
385
- createPackagesByClasses(sourceRoot)
392
+ dir
393
+ }? : return null
394
+ return if (VfsUtil .virtualToIoFile(file).isFile) {
395
+ null
396
+ } else {
397
+ StandardFileSystems .local().findFileByPath(file.path)
386
398
}
387
- return true
388
399
}
389
400
390
401
private fun createPackagesByClasses (testSourceRoot : VirtualFile ) {
@@ -413,12 +424,33 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
413
424
" Generation error"
414
425
)
415
426
416
- private fun getRootDirectoryAndContentEntry () : Pair <VirtualFile , ContentEntry >? {
417
- val testSourceRoot = getTestRoot()? : return null
418
- val contentEntry = ModuleRootManager .getInstance(model.testModule).contentEntries
427
+ private fun findReadOnlyContentEntry (testSourceRoot : VirtualFile ? ): ContentEntry ? {
428
+ if (testSourceRoot == null ) return null
429
+ if (testSourceRoot is FakeVirtualFile ) {
430
+ return findReadOnlyContentEntry(testSourceRoot.parent)
431
+ }
432
+ return ModuleRootManager .getInstance(model.testModule).contentEntries
419
433
.filterNot { it.file == null }
420
- .firstOrNull { VfsUtil .isAncestor(it.file!! , testSourceRoot, true ) } ? : return null
421
- return Pair (testSourceRoot, contentEntry)
434
+ .firstOrNull { VfsUtil .isAncestor(it.file!! , testSourceRoot, false ) }
435
+ }
436
+
437
+ private fun getOrCreateTestRoot (testSourceRoot : VirtualFile ): Boolean {
438
+ val modifiableModel = ModuleRootManager .getInstance(model.testModule).modifiableModel
439
+ try {
440
+ val contentEntry = modifiableModel.contentEntries
441
+ .filterNot { it.file == null }
442
+ .firstOrNull { VfsUtil .isAncestor(it.file!! , testSourceRoot, true ) }
443
+ ? : return false
444
+
445
+ contentEntry.addSourceRootIfAbsent(
446
+ modifiableModel,
447
+ testSourceRoot.url,
448
+ codegenLanguages.item.testRootType()
449
+ )
450
+ return true
451
+ } finally {
452
+ if (modifiableModel.isWritable && ! modifiableModel.isDisposed) modifiableModel.dispose()
453
+ }
422
454
}
423
455
424
456
private fun createPackageWrapper (packageName : String? ): PackageWrapper =
@@ -648,7 +680,14 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
648
680
itemsToHelpTooltip.forEach { (box, tooltip) -> box.setHelpTooltipTextChanger(tooltip) }
649
681
650
682
testSourceFolderField.childComponent.addActionListener { event ->
651
- model.testSourceRoot = pathToFile((event.source as JComboBox <* >).selectedItem as String )
683
+ with ((event.source as JComboBox <* >).selectedItem) {
684
+ if (this is VirtualFile ) {
685
+ model.testSourceRoot = this @with
686
+ }
687
+ else {
688
+ model.testSourceRoot = null
689
+ }
690
+ }
652
691
}
653
692
654
693
mockStrategies.addActionListener { event ->
@@ -700,14 +739,6 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
700
739
}
701
740
}
702
741
703
- private fun pathToFile (path : String ): VirtualFile ? {
704
- val relativePath = path.substring(" .../" .length).replace(' \\ ' , ' /' )
705
- return model.testModule
706
- .suitableTestSourceRoots()
707
- .firstOrNull { it.path.contains(relativePath) }
708
- }
709
-
710
-
711
742
private lateinit var currentFrameworkItem: TestFramework
712
743
713
744
// We would like to remove JUnit4 from framework list in parametrized mode
0 commit comments