Skip to content

Add Junit5-params dependency for projects without it when needed #620 #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ sealed class TestFramework(
override val displayName: String,
override val description: String = "Use $displayName as test framework",
) : CodeGenerationSettingItem {
var isParametrizedTestsConfigured = false
var isInstalled: Boolean = false
abstract val mainPackage: String
abstract val assertionsClass: ClassId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ fun TestFramework.patterns(): Patterns {
return Patterns(moduleLibraryPatterns, libraryPatterns)
}


fun TestFramework.parametrizedTestsPatterns(): Patterns {
val moduleLibraryPatterns = when (this) {
Junit4 -> emptyList()
Junit5 -> emptyList() // emptyList here because JUnit5 module may not be enough for parametrized tests if :junit-jupiter-params: is not installed
TestNg -> testNgModulePatterns
}
val libraryPatterns = when (this) {
Junit4 -> emptyList()
Junit5 -> junit5ParametrizedTestsPatterns
TestNg -> testNgPatterns
}

return Patterns(moduleLibraryPatterns, libraryPatterns)
}


fun MockFramework.patterns(): Patterns {
val moduleLibraryPatterns = when (this) {
MockFramework.MOCKITO -> mockitoModulePatterns
Expand All @@ -47,11 +64,16 @@ val JUNIT_5_MVN_PATTERN = Regex("org\\.junit\\.jupiter:junit-jupiter-api:5(\\.[0
val JUNIT_5_BASIC_PATTERN = Regex("JUnit5\\.4")
val junit5Patterns = listOf(JUNIT_5_JAR_PATTERN, JUNIT_5_MVN_PATTERN, JUNIT_5_BASIC_PATTERN)

val JUNIT_5_PARAMETRIZED_JAR_PATTERN = Regex("junit-jupiter-params-5(\\.[0-9]+){1,2}")
val JUNIT_5_PARAMETRIZED_MVN_PATTERN = Regex("org\\.junit\\.jupiter\\.junit-jupiter-params:5(\\.[0-9]+){1,2}")
val junit5ParametrizedTestsPatterns = listOf(JUNIT_5_JAR_PATTERN, JUNIT_5_BASIC_PATTERN,
JUNIT_5_PARAMETRIZED_JAR_PATTERN, JUNIT_5_PARAMETRIZED_MVN_PATTERN)

val JUNIT5_BASIC_MODULE_PATTERN = Regex("junit-jupiter")
val junit5ModulePatterns = listOf(JUNIT5_BASIC_MODULE_PATTERN)

val TEST_NG_JAR_PATTERN = Regex("testng-[0-9](\\.[0-9]+){2}")
val TEST_NG_MVN_PATTERN = Regex("org\\.testng:testng:(\\.[0-9]+){3}")
val TEST_NG_MVN_PATTERN = Regex("org\\.testng:testng:[0-9](\\.[0-9]+){2}")
val TEST_NG_BASIC_PATTERN = Regex("testng")
val testNgPatterns = listOf(TEST_NG_JAR_PATTERN, TEST_NG_MVN_PATTERN, TEST_NG_BASIC_PATTERN)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ fun jUnit5LibraryDescriptor(versionInProject: String?) =
fun testNgLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.testng", "testng", "6.8.8", null, versionInProject ?: "6.9.6")

fun jUnit5ParametrizedTestsLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.junit.jupiter", "junit-jupiter-params", "5.8.1", null, versionInProject ?: "5.8.1")

fun mockitoCoreLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.mockito", "mockito-core", "3.5.0", "4.2.0", versionInProject ?: "4.2.0")
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import org.utbot.framework.util.Conflict
import org.utbot.intellij.plugin.models.GenerateTestsModel
import org.utbot.intellij.plugin.models.jUnit4LibraryDescriptor
import org.utbot.intellij.plugin.models.jUnit5LibraryDescriptor
import org.utbot.intellij.plugin.models.jUnit5ParametrizedTestsLibraryDescriptor
import org.utbot.intellij.plugin.models.mockitoCoreLibraryDescriptor
import org.utbot.intellij.plugin.models.packageName
import org.utbot.intellij.plugin.models.testNgLibraryDescriptor
Expand All @@ -125,6 +126,7 @@ import org.utbot.intellij.plugin.ui.utils.LibrarySearchScope
import org.utbot.intellij.plugin.ui.utils.addSourceRootIfAbsent
import org.utbot.intellij.plugin.ui.utils.allLibraries
import org.utbot.intellij.plugin.ui.utils.findFrameworkLibrary
import org.utbot.intellij.plugin.ui.utils.findParametrizedTestsLibrary
import org.utbot.intellij.plugin.ui.utils.getOrCreateTestResourcesPath
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
import org.utbot.intellij.plugin.ui.utils.kotlinTargetPlatform
Expand Down Expand Up @@ -196,6 +198,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m

TestFramework.allItems.forEach {
it.isInstalled = findFrameworkLibrary(model.project, model.testModule, it) != null
it.isParametrizedTestsConfigured = findParametrizedTestsLibrary(model.project, model.testModule, it) != null
}
MockFramework.allItems.forEach {
it.isInstalled = findFrameworkLibrary(model.project, model.testModule, it) != null
Expand Down Expand Up @@ -532,6 +535,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
configureTestFrameworkIfRequired()
configureMockFrameworkIfRequired()
configureStaticMockingIfRequired()
configureParametrizedTestsIfRequired()

super.doOKAction()
}
Expand Down Expand Up @@ -665,8 +669,14 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
//region configure frameworks

private fun configureTestFrameworkIfRequired() {
if (!testFrameworks.item.isInstalled) {
val testFramework = testFrameworks.item
if (!testFramework.isInstalled) {
configureTestFramework()

// Configuring framework will configure parametrized tests automatically
// TODO: do something more general here
// Note: we can't just update isParametrizedTestsConfigured as before because project.allLibraries() won't be updated immediately
testFramework.isParametrizedTestsConfigured = true
}

model.conflictTriggers[Conflict.TestFrameworkConflict] = TestFramework.allItems.count { it.isInstalled } > 1
Expand All @@ -684,6 +694,12 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
}
}

private fun configureParametrizedTestsIfRequired() {
if (parametrizedTestSources.item != ParametrizedTestSource.DO_NOT_PARAMETRIZE && !testFrameworks.item.isParametrizedTestsConfigured) {
configureParametrizedTests()
}
}

private fun configureTestFramework() {
val selectedTestFramework = testFrameworks.item

Expand Down Expand Up @@ -742,6 +758,27 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
}
}

private fun configureParametrizedTests() {
// TODO: currently first three declarations are copy-pasted from configureTestFramework(), maybe fix this somehow?
val selectedTestFramework = testFrameworks.item

val libraryInProject =
findFrameworkLibrary(model.project, model.testModule, selectedTestFramework, LibrarySearchScope.Project)
val versionInProject = libraryInProject?.libraryName?.parseVersion()

val libraryDescriptor: ExternalLibraryDescriptor? = when (selectedTestFramework) {
Junit4 -> error("Parametrized tests are not supported for JUnit 4")
Junit5 -> jUnit5ParametrizedTestsLibraryDescriptor(versionInProject)
TestNg -> null // Parametrized tests come with TestNG by default
}

selectedTestFramework.isParametrizedTestsConfigured = true
libraryDescriptor?.let {
addDependency(model.testModule, it)
.onError { selectedTestFramework.isParametrizedTestsConfigured = false }
}
}

/**
* Adds the dependency for selected framework via [JavaProjectModelModificationService].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.utbot.framework.plugin.api.MockFramework
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.LibraryOrderEntry
import org.utbot.framework.codegen.model.util.parametrizedTestsPatterns
import org.utbot.framework.codegen.model.util.Patterns

fun findFrameworkLibrary(
Expand All @@ -24,6 +25,13 @@ fun findFrameworkLibrary(
scope: LibrarySearchScope = LibrarySearchScope.Module,
): LibraryOrderEntry? = findMatchingLibrary(project, testModule, mockFramework.patterns(), scope)

fun findParametrizedTestsLibrary(
project: Project,
testModule: Module,
testFramework: TestFramework,
scope: LibrarySearchScope = LibrarySearchScope.Module,
): LibraryOrderEntry? = findMatchingLibrary(project, testModule, testFramework.parametrizedTestsPatterns(), scope)

private fun findMatchingLibrary(
project: Project,
testModule: Module,
Expand Down