Skip to content

Commit

Permalink
[build_tools_tests]Added exception handling for screenshots
Browse files Browse the repository at this point in the history
GitOrigin-RevId: c431669a018cccb585790bd9850913d3dc7072a1
  • Loading branch information
nikitaBarkov authored and intellij-monorepo-bot committed Nov 18, 2024
1 parent 70bb462 commit 97fe82d
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.intellij.ide.starter.utils

import com.intellij.ide.starter.path.GlobalPaths
import com.intellij.ide.starter.process.exec.ExecOutputRedirect
import com.intellij.ide.starter.process.exec.ProcessExecutor
import com.intellij.ide.starter.runner.IDERunContext
import com.intellij.openapi.util.SystemInfo
Expand Down Expand Up @@ -50,7 +51,7 @@ fun takeScreenshot(logsDir: Path) {
takeScreenshot(logsDir, "screenshot_beforeKill.jpg")
}

fun takeScreenshot(logsDir: Path, screenshotName: String) {
fun takeScreenshot(logsDir: Path, screenshotName: String, ignoreExceptions: Boolean = false) {
val toolsDir = GlobalPaths.instance.getCacheDirectoryFor("tools")
val toolName = "TakeScreenshot"
val screenshotTool = toolsDir / toolName / "$toolName.jar"
Expand All @@ -62,14 +63,24 @@ fun takeScreenshot(logsDir: Path, screenshotName: String) {
val screenshotFile = logsDir.resolve(screenshotName)

val javaPath = ProcessHandle.current().info().command().orElseThrow().toString()
ProcessExecutor(
presentableName = "take-screenshot",
workDir = toolsDir,
timeout = 15.seconds,
args = mutableListOf(javaPath, "-jar", screenshotTool.absolutePathString(), screenshotFile.toString()),
environmentVariables = mapOf("DISPLAY" to ":88"),
onlyEnrichExistedEnvVariables = true
).start()
val stdOut = ExecOutputRedirect.ToStdOut("[take-screenshot-out]")
val stdErr = ExecOutputRedirect.ToStdOut("[take-screenshot-err]")
try {
ProcessExecutor(
presentableName = "take-screenshot",
workDir = toolsDir,
timeout = 15.seconds,
stdoutRedirect = stdOut,
stderrRedirect = stdErr,
args = mutableListOf(javaPath, "-jar", screenshotTool.absolutePathString(), screenshotFile.toString()),
environmentVariables = mapOf("DISPLAY" to ":88"),
onlyEnrichExistedEnvVariables = true
).start()
}
catch (e: Exception) {
if (!ignoreExceptions) throw e
logOutput("Exception while taking screenshot: ${e.message}")
}

if (screenshotFile.exists()) {
logOutput("Screenshot saved in $screenshotFile")
Expand Down Expand Up @@ -148,5 +159,5 @@ private fun downloadAsyncProfilerIfNeeded(profiler: Path, toolsDir: Path) {

fun pathInsideJarFile(
jarFile: Path,
pathInsideJar: String
pathInsideJar: String,
): String = jarFile.toAbsolutePath().toString().trimEnd('/') + "!/" + pathInsideJar

0 comments on commit 97fe82d

Please sign in to comment.