From 2a79a7d41317da64a1d88fed52514a06d773a808 Mon Sep 17 00:00:00 2001 From: Maksim Pelevin Date: Thu, 10 Aug 2023 12:19:30 +0300 Subject: [PATCH 1/2] Improve CLI: 1. Fix error with classloader 2. Add an option to control fuzzer ration --- .../utbot/cli/GenerateTestsAbstractCommand.kt | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt b/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt index 4d39fd2c1c..c9a71eb262 100644 --- a/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt +++ b/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt @@ -7,6 +7,7 @@ import com.github.ajalt.clikt.parameters.options.multiple import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.unique import com.github.ajalt.clikt.parameters.types.choice +import com.github.ajalt.clikt.parameters.types.double import com.github.ajalt.clikt.parameters.types.long import mu.KotlinLogging import org.utbot.common.PathUtil.classFqnToPath @@ -25,14 +26,7 @@ import org.utbot.framework.codegen.domain.testFrameworkByName import org.utbot.framework.codegen.generator.CodeGenerator import org.utbot.framework.codegen.generator.CodeGeneratorParams import org.utbot.framework.codegen.services.language.CgLanguageAssistant -import org.utbot.framework.plugin.api.ClassId -import org.utbot.framework.plugin.api.CodegenLanguage -import org.utbot.framework.plugin.api.ExecutableId -import org.utbot.framework.plugin.api.MethodId -import org.utbot.framework.plugin.api.MockStrategyApi -import org.utbot.framework.plugin.api.TestCaseGenerator -import org.utbot.framework.plugin.api.TreatOverflowAsError -import org.utbot.framework.plugin.api.UtMethodTestSet +import org.utbot.framework.plugin.api.* import org.utbot.framework.plugin.services.JdkInfoDefaultProvider import org.utbot.summary.summarizeAll import java.io.File @@ -131,6 +125,12 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) : .long() .default(LONG_GENERATION_TIMEOUT) + private val fuzzingRation by option( + "--fuzzing-ratio", + help = "Specify the ratio between symbolic engine and fuzzing" + ) + .double() + protected open val classLoader: URLClassLoader by lazy { val urls = classPath!! .split(File.pathSeparator) @@ -138,7 +138,7 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) : uri.toPath().toURL() } .toTypedArray() - URLClassLoader(urls) + URLClassLoader(urls, null) } abstract override fun run() @@ -162,7 +162,15 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) : targetMethods, mockStrategy, chosenClassesToMockAlways, - generationTimeout + generationTimeout, + generate = fuzzingRation?.let { rat -> + testFlow { + generationTimeout = this@GenerateTestsAbstractCommand.generationTimeout + isSymbolicEngineEnabled = rat < 1.0 + isFuzzingEnabled = rat > 0.0 + fuzzingValue = rat + } + } ?: defaultTestFlow(generationTimeout) ).let { if (sourceCodeFile != null) it.summarizeAll(searchDirectory, sourceCodeFile.toFile()) else it } From 57555e4e07b369ca9493ca98717a96c48c752d11 Mon Sep 17 00:00:00 2001 From: Maksim Pelevin Date: Thu, 10 Aug 2023 13:18:31 +0300 Subject: [PATCH 2/2] We can ignore --source option --- .../src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt b/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt index a6837ca80f..94e0dfcfe2 100644 --- a/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt +++ b/utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt @@ -50,7 +50,6 @@ class GenerateTestsCommand : "-s", "--source", help = "Specifies source code file for a generated test" ) - .required() .check("Must exist and end with .java or .kt suffix") { (it.endsWith(".java") || it.endsWith(".kt")) && Files.exists(Paths.get(it)) } @@ -113,7 +112,7 @@ class GenerateTestsCommand : val testSets = generateTestSets( testCaseGenerator, targetMethods, - Paths.get(sourceCodeFile), + sourceCodeFile?.let(Paths::get), searchDirectory = workingDirectory, chosenClassesToMockAlways = (Mocker.defaultSuperClassesToMockAlwaysNames + classesToMockAlways) .mapTo(mutableSetOf()) { ClassId(it) } @@ -149,9 +148,12 @@ class GenerateTestsCommand : projectRootPath == null -> { println("The path to the project root is required to generate a report. Please, specify \"--project-root\" option.") } + sourceCodeFile == null -> { + println("The source file is not found. Please, specify \"--source\" option.") + } else -> { val sourceFinding = - SourceFindingStrategyDefault(classFqn, sourceCodeFile, testsFilePath, projectRootPath) + SourceFindingStrategyDefault(classFqn, sourceCodeFile!!, testsFilePath, projectRootPath) val report = SarifReport(testSets, testClassBody, sourceFinding).createReport().toJson() saveToFile(report, sarifReport) println("The report was saved to \"$sarifReport\".")