Skip to content

Commit

Permalink
We create temporary files a lot, we have to clean it either (UnitTest…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vassiliy-Kudryashov authored and AbdullinAM committed Oct 17, 2022
1 parent 2a981af commit 58b4755
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
17 changes: 10 additions & 7 deletions utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ object FileUtil {
* Deletes all the files and folders from the java unit-test temp directory that are older than [daysLimit].
*/
fun clearTempDirectory(daysLimit: Int) {
val currentTimeInMillis = System.currentTimeMillis()

val files = utBotTempDirectory.toFile().listFiles() ?: return
(utBotTempDirectory.toFile().listFiles() ?: return).filter { isOld(it, daysLimit) }
.forEach { it.deleteRecursively() }
}

files.filter {
val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime()
TimeUnit.MILLISECONDS.toDays(currentTimeInMillis - creationTime.toMillis()) > daysLimit
}.forEach { it.deleteRecursively() }
private fun isOld(it: File, daysLimit: Int): Boolean {
val creationTime = Files.readAttributes(it.toPath(), BasicFileAttributes::class.java).creationTime()
return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - creationTime.toMillis()) > daysLimit
}

fun createTempDirectory(prefix: String): Path {
return createTempDirectory(utBotTempDirectory, prefix)
}

fun createTempFile(prefix: String, suffix: String) : Path {
return Files.createTempFile(utBotTempDirectory, prefix, suffix)
}

/**
* Copy the class file for given [classes] to temporary folder.
* It can be used for Soot analysis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import java.io.File
import java.lang.reflect.Modifier
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import org.utbot.common.FileUtil

const val SYMBOLIC_NULL_ADDR: Int = 0

Expand Down Expand Up @@ -1384,7 +1385,7 @@ enum class CodegenLanguage(

// https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#commandlineargfile
fun isolateCommandLineArgumentsToArgumentFile(arguments: List<String>): String {
val argumentFile = File.createTempFile("cmd-args", "")
val argumentFile = FileUtil.createTempFile("cmd-args", "").toFile()
argumentFile.writeText(
arguments.joinToString(" ") {
// If a filename contains embedded spaces, put the whole filename in double quotes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.awt.datatransfer.StringSelection
import java.io.FileWriter
import java.nio.file.Files
import java.nio.file.Paths
import org.utbot.common.FileUtil

private val logger = KotlinLogging.logger {}

Expand All @@ -29,7 +30,7 @@ class GraphViz(
) : TraverseGraphStatistics(globalGraph) {

// Files
private val graphVisDirectory = Files.createTempDirectory("Graph-vis")
private val graphVisDirectory = FileUtil.createTempDirectory("Graph-vis")
private val graphVisPathString = graphVisDirectory.toString()
private val graphJs = Paths.get(graphVisPathString, "graph.js").toFile()

Expand Down

0 comments on commit 58b4755

Please sign in to comment.