-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
processUtils.kt
51 lines (46 loc) · 1.78 KB
/
processUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package org.jetbrains.compose.test.utils
import java.io.File
data class ProcessRunResult(val exitCode: Int, val out: String, val err: String)
fun runProcess(
tool: File,
args: Collection<String>,
checkExitCodeIsNormal: Boolean = true
): ProcessRunResult {
val outFile = File.createTempFile("run-process-compose-tests-out.txt", null).apply { deleteOnExit() }
val errFile = File.createTempFile("run-process-compose-tests-err.txt", null).apply { deleteOnExit() }
return try {
val cmd = arrayOf(tool.absolutePath, *args.toTypedArray())
val process = ProcessBuilder().run {
redirectError(errFile)
redirectOutput(outFile)
command(*cmd)
start()
}
val exitCode = process.waitFor()
if (checkExitCodeIsNormal) {
check(exitCode == 0) {
buildString {
appendLine("Non-zero exit code: $exitCode")
appendLine("Command: ${cmd.joinToString(", ")}")
appendLine("Out:")
outFile.forEachLine { line ->
appendLine(" >$line")
}
appendLine("Err:")
errFile.forEachLine { line ->
appendLine(" >$line")
}
}
}
}
ProcessRunResult(exitCode = exitCode, out = outFile.readText(), err = errFile.readText())
} finally {
outFile.delete()
errFile.delete()
}
}
val isWindows = System.getProperty("os.name").contains("windows", ignoreCase = true)