Skip to content

Commit da3e553

Browse files
authored
Go. Big update (#2206)
* Bug fix * Move to Kotlin UI DSL 2 * Move from utbot-java-fuzzing to utbot-fuzzing * Add restart worker * Add equals and hashcode methods to models * Add simple chan support * Fix array and slice value providers * Update generation of test files * Fix channel value provider * Add channel initialization * Fix channel conversion in Go code * Update samples * Add pointers support * Add pointers support for return types * Update samples * Add part of channels support for return types * First attempt to parallelize fuzzing * Delete unnecessary lock and change async to launch * Change traces to coverage * Add fuzzing mode * Add support for methods * Add support for methods. Fix floats. Try to minimize input parameters * Add support for interfaces (as function parameters) * Refactoring * Fix restarting the worker * Fix template of tests * Fix strings * Fix remarks * Fix bugs * Fix Path environment variable * Refactor code and fix bugs * Fix number of shuffles for array * Refactor modifyEnvironment function
1 parent c4b5aaa commit da3e553

File tree

64 files changed

+4442
-1555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4442
-1555
lines changed

utbot-cli-go/src/main/kotlin/org/utbot/cli/go/commands/GenerateGoTestsCommand.kt

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package org.utbot.cli.go.commands
22

33
import com.github.ajalt.clikt.core.CliktCommand
44
import com.github.ajalt.clikt.parameters.options.*
5+
import com.github.ajalt.clikt.parameters.types.int
56
import com.github.ajalt.clikt.parameters.types.long
67
import mu.KotlinLogging
78
import org.utbot.cli.go.logic.CliGoUtTestsGenerationController
89
import org.utbot.cli.go.util.durationInMillis
910
import org.utbot.cli.go.util.now
1011
import org.utbot.cli.go.util.toAbsolutePath
1112
import org.utbot.go.logic.GoUtTestsGenerationConfig
13+
import org.utbot.go.logic.TestsGenerationMode
1214
import java.nio.file.Files
1315
import java.nio.file.Paths
1416

@@ -26,21 +28,46 @@ class GenerateGoTestsCommand :
2628
it.endsWith(".go") && Files.exists(Paths.get(it))
2729
}
2830

29-
private val selectedFunctionsNames: List<String> by option(
31+
private val selectedFunctionNames: List<String> by option(
3032
"-f", "--function",
3133
help = StringBuilder()
3234
.append("Specifies function name to generate tests for. ")
3335
.append("Can be used multiple times to select multiple functions at the same time.")
3436
.toString()
3537
)
36-
.multiple(required = true)
38+
.multiple()
39+
40+
private val selectedMethodNames: List<String> by option(
41+
"-m", "--method",
42+
help = StringBuilder()
43+
.append("Specifies method name to generate tests for. ")
44+
.append("Can be used multiple times to select multiple methods at the same time.")
45+
.toString()
46+
)
47+
.multiple()
3748

3849
private val goExecutablePath: String by option(
39-
"-go", "--go-path",
50+
"-go",
4051
help = "Specifies path to Go executable. For example, it could be [/usr/local/go/bin/go] for some systems"
4152
)
4253
.required() // TODO: attempt to find it if not specified
4354

55+
private val gopath: String by option(
56+
"-gopath",
57+
help = buildString {
58+
appendLine("Specifies path the location of your workspace.")
59+
appendLine("It defaults to a directory named go inside your home directory, so \$HOME/go on Unix, \$home/go on Plan 9, and %USERPROFILE%\\go (usually C:\\Users\\YourName\\go) on Windows.")
60+
}
61+
).required() // TODO: attempt to find it if not specified
62+
63+
private val numberOfFuzzingProcesses: Int by option(
64+
"-parallel",
65+
help = "The number of fuzzing processes running at once, default 8."
66+
)
67+
.int()
68+
.default(8)
69+
.check("Must be positive") { it > 0 }
70+
4471
private val eachFunctionExecutionTimeoutMillis: Long by option(
4572
"-et", "--each-execution-timeout",
4673
help = StringBuilder()
@@ -77,9 +104,26 @@ class GenerateGoTestsCommand :
77104
)
78105
.flag(default = false)
79106

107+
private val fuzzingMode: Boolean by option(
108+
"-fm",
109+
"--fuzzing-mode",
110+
help = "Stop test generation when a panic or error occurs (only one test will be generated for one of these cases)"
111+
)
112+
.flag(default = false)
113+
80114
override fun run() {
115+
if (selectedFunctionNames.isEmpty() && selectedMethodNames.isEmpty()) {
116+
throw IllegalArgumentException("Functions or methods must be passed")
117+
}
118+
81119
val sourceFileAbsolutePath = sourceFile.toAbsolutePath()
82120
val goExecutableAbsolutePath = goExecutablePath.toAbsolutePath()
121+
val gopathAbsolutePath = gopath.toAbsolutePath()
122+
val mode = if (fuzzingMode) {
123+
TestsGenerationMode.FUZZING_MODE
124+
} else {
125+
TestsGenerationMode.DEFAULT
126+
}
83127

84128
val testsGenerationStarted = now()
85129
logger.info { "Test file generation for [$sourceFile] - started" }
@@ -88,12 +132,16 @@ class GenerateGoTestsCommand :
88132
printToStdOut = printToStdOut,
89133
overwriteTestFiles = overwriteTestFiles
90134
).generateTests(
91-
mapOf(sourceFileAbsolutePath to selectedFunctionsNames),
135+
mapOf(sourceFileAbsolutePath to selectedFunctionNames),
136+
mapOf(sourceFileAbsolutePath to selectedMethodNames),
92137
GoUtTestsGenerationConfig(
93138
goExecutableAbsolutePath,
139+
gopathAbsolutePath,
140+
numberOfFuzzingProcesses,
141+
mode,
94142
eachFunctionExecutionTimeoutMillis,
95143
allFunctionExecutionTimeoutMillis
96-
)
144+
),
97145
)
98146
} catch (t: Throwable) {
99147
logger.error { "An error has occurred while generating test for snippet $sourceFile: $t" }

utbot-cli-go/src/main/kotlin/org/utbot/cli/go/commands/RunGoTestsCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class RunGoTestsCommand : CliktCommand(name = "runGo", help = "Runs tests for th
111111

112112
try {
113113
val runGoTestCommand = mutableListOf(
114-
goExecutablePath.toAbsolutePath(),
114+
goExecutablePath.toAbsolutePath().toString(),
115115
"test",
116116
"./"
117117
)

utbot-cli-go/src/main/kotlin/org/utbot/cli/go/logic/CliGoUtTestsGenerationController.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.utbot.go.api.GoUtFuzzedFunctionTestCase
99
import org.utbot.go.gocodeanalyzer.GoSourceCodeAnalyzer
1010
import org.utbot.go.logic.AbstractGoUtTestsGenerationController
1111
import java.io.File
12+
import java.nio.file.Path
1213
import java.time.LocalDateTime
1314

1415
private val logger = KotlinLogging.logger {}
@@ -20,7 +21,10 @@ class CliGoUtTestsGenerationController(
2021

2122
private lateinit var currentStageStarted: LocalDateTime
2223

23-
override fun onSourceCodeAnalysisStart(targetFunctionsNamesBySourceFiles: Map<String, List<String>>): Boolean {
24+
override fun onSourceCodeAnalysisStart(
25+
targetFunctionNamesBySourceFiles: Map<Path, List<String>>,
26+
targetMethodNamesBySourceFiles: Map<Path, List<String>>
27+
): Boolean {
2428
currentStageStarted = now()
2529
logger.debug { "Source code analysis - started" }
2630

@@ -36,6 +40,20 @@ class CliGoUtTestsGenerationController(
3640
return handleMissingSelectedFunctions(analysisResults)
3741
}
3842

43+
override fun onPackageInstrumentationStart(): Boolean {
44+
currentStageStarted = now()
45+
logger.debug { "Package instrumentation - started" }
46+
47+
return true
48+
}
49+
50+
override fun onPackageInstrumentationFinished(): Boolean {
51+
val stageDuration = durationInMillis(currentStageStarted)
52+
logger.debug { "Package instrumentation - completed in [$stageDuration] (ms)" }
53+
54+
return true
55+
}
56+
3957
override fun onTestCasesGenerationForGoSourceFileFunctionsStart(
4058
sourceFile: GoUtFile,
4159
functions: List<GoUtFunction>

utbot-cli-go/src/main/kotlin/org/utbot/cli/go/util/FileUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.utbot.cli.go.util
22

33
import java.io.File
4+
import java.nio.file.Path
5+
import java.nio.file.Paths
46

5-
fun String.toAbsolutePath(): String = File(this).canonicalPath
7+
fun String.toAbsolutePath(): Path = Paths.get(this).toAbsolutePath()
68

79
fun createFile(filePath: String): File = createFile(File(filePath).canonicalFile)
810

utbot-go/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tasks {
2121
}
2222

2323
dependencies {
24-
api(project(":utbot-java-fuzzing"))
24+
api(project(":utbot-fuzzing"))
2525
api(project(":utbot-framework"))
2626
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
2727
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

utbot-go/go-samples/simple/samples.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,37 @@ func StringSearch(str string) bool {
118118
}
119119
return false
120120
}
121+
122+
func SumOfChanElements(c <-chan int) int {
123+
sum := 0
124+
for val := range c {
125+
sum += val
126+
}
127+
return sum
128+
}
129+
130+
type List struct {
131+
tail *List
132+
val int
133+
}
134+
135+
func LenOfList(l *List) int {
136+
if l == nil {
137+
return 0
138+
}
139+
length := 1
140+
for ; l.tail != nil; l = l.tail {
141+
length++
142+
}
143+
return length
144+
}
145+
146+
func GetLastNode(n *Node) *Node {
147+
if n == nil {
148+
return nil
149+
}
150+
for ; n.next != nil; n = n.next {
151+
152+
}
153+
return n
154+
}

0 commit comments

Comments
 (0)