From ae21f1d5f44ce8fa9c21f06429bcc01a077289cd Mon Sep 17 00:00:00 2001 From: Eray Felek Date: Fri, 6 Dec 2024 17:19:20 +0100 Subject: [PATCH] Execute powershell from file --- .cirrus.yml | 4 +- .cirrus/foreground.ps1 | 67 +++++++++ .../intellij/its/tests/StandaloneIdeaTests.kt | 39 ++++++ .../intellij/its/tests/foreground.ps1 | 128 ++++++++++++++++++ 4 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 .cirrus/foreground.ps1 create mode 100644 its/src/test/kotlin/org/sonarlint/intellij/its/tests/foreground.ps1 diff --git a/.cirrus.yml b/.cirrus.yml index 5b4da0bc6..b49fd13b6 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -244,7 +244,6 @@ qa_task: # echo ${CIRRUS_WORKING_DIR} # echo ls ${CIRRUS_WORKING_DIR}/staged-plugin mkdir -p its/build/idea-sandbox/config-uiTest/ - powershell -command "Get-Process | Select-Object Id, ProcessName, MainWindowTitle | Where-Object { $_.MainWindowTitle -ne '' }" gradle :its:runIdeForUiTests --stacktrace -i -PijVersion=${IDEA_VERSION} -PslPluginDirectory=${CIRRUS_WORKING_DIR}/staged-plugin > ${CIRRUS_WORKING_DIR}/runIdeGradle.log & wait_ide_script: | echo "Wait for IDE to start" @@ -255,6 +254,7 @@ qa_task: run_its_script: | echo "Run ITs on ${IDEA_VERSION}" source .cirrus/use-gradle-wrapper.sh + # powershell -ExecutionPolicy Bypass -File ".cirrus/foreground.ps1" gradle :its:check --stacktrace -i -PijVersion=${IDEA_VERSION} -PslPluginDirectory=${CIRRUS_WORKING_DIR}/staged-plugin <<: *CLEANUP_GRADLE_CACHE_SCRIPT always: @@ -262,6 +262,8 @@ qa_task: - cat ${CIRRUS_WORKING_DIR}/runIdeGradle.log log_artifacts: path: "its/build/idea-sandbox/system/log" + image_artifacts: + path: "${CIRRUS_WORKING_DIR}/image_${IDEA_VERSION}.png" on_failure: reports_artifacts: path: "**/reports/**/*" diff --git a/.cirrus/foreground.ps1 b/.cirrus/foreground.ps1 new file mode 100644 index 000000000..0080f3884 --- /dev/null +++ b/.cirrus/foreground.ps1 @@ -0,0 +1,67 @@ +Add-Type @" +using System; +using System.Runtime.InteropServices; + +public class CustomUser32 { + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount); + + public static string GetActiveWindowTitle() { + IntPtr handle = GetForegroundWindow(); + System.Text.StringBuilder sb = new System.Text.StringBuilder(256); + if (GetWindowText(handle, sb, sb.Capacity) > 0) { + return sb.ToString(); + } + return null; + } +} +"@ + +function Print-AllProcessesWithMainWindowTitle { + $processes = Get-Process | Select-Object Id, ProcessName, MainWindowTitle + + # Write the output to the console + $processes | ForEach-Object { Write-Output "Id: $_.Id, ProcessName: $_.ProcessName, MainWindowTitle: $_.MainWindowTitle" } +} + +function Bring-WindowToForegroundByTitle { + param ( + [string]$windowTitle, + [int]$maxRetries = 15, + [int]$retryDelay = 1000 + ) + + Write-Output "Starting to try bring window to foreground" + + for ($i = 0; $i -lt $maxRetries; $i++) { + $process = Get-Process | Where-Object { $_.MainWindowTitle -eq $windowTitle } | Select-Object -First 1 + + if ($null -ne $process) { + if ([CustomUser32]::SetForegroundWindow($process.MainWindowHandle)) { + Write-Output "Brought window with title '$windowTitle' to the foreground." + return + } else { + Start-Sleep -Milliseconds $retryDelay + } + } else { + Write-Output "No window with the title '$windowTitle' found." + return + } + } + + Write-Output "Failed to bring window with title '$windowTitle' to the foreground after $maxRetries attempts." +} + +Write-Output "Starting to print process" + +# Print all processes with main window titles +Print-AllProcessesWithMainWindowTitle + +# Example call to bring the window with title "Welcome to IntelliJ IDEA" to the foreground +Bring-WindowToForegroundByTitle -windowTitle "Welcome to IntelliJ IDEA" diff --git a/its/src/test/kotlin/org/sonarlint/intellij/its/tests/StandaloneIdeaTests.kt b/its/src/test/kotlin/org/sonarlint/intellij/its/tests/StandaloneIdeaTests.kt index ada967384..760433f46 100644 --- a/its/src/test/kotlin/org/sonarlint/intellij/its/tests/StandaloneIdeaTests.kt +++ b/its/src/test/kotlin/org/sonarlint/intellij/its/tests/StandaloneIdeaTests.kt @@ -19,6 +19,11 @@ */ package org.sonarlint.intellij.its.tests +import java.io.BufferedReader +import java.io.File +import java.io.InputStreamReader +import java.nio.file.Paths +import javax.imageio.ImageIO import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.EnabledIf @@ -59,6 +64,12 @@ class StandaloneIdeaTests : BaseUiTest() { @Test fun should_exclude_file_and_analyze_file_and_no_issues_found() = uiTest { + remoteRobot.getScreenshot() + val screenshot = remoteRobot.getScreenshot() + val cirrusWorkingDir = System.getenv("CIRRUS_WORKING_DIR") + val ideaVersion = System.getenv("IDEA_VERSION") + val outputFile = File("$cirrusWorkingDir/image_$ideaVersion.jpg") + ImageIO.write(screenshot, "png", outputFile) openExistingProject("sample-java-issues") excludeFile("src/main/java/foo/Foo.java") openFile("src/main/java/foo/Foo.java", "Foo.java") @@ -72,4 +83,32 @@ class StandaloneIdeaTests : BaseUiTest() { openFile("templates/memory_limit_pod2.yml", "memory_limit_pod2.yml") verifyCurrentFileTabContainsMessages("Bind this resource's automounted service account to RBAC or disable automounting.") } + + fun executePS() { + try { + // Specify the PowerShell script to execute, located in the same directory as this Java file + val scriptPath = Paths.get(System.getProperty("user.dir"), "src", "test", "kotlin", "org", "sonarlint", "intellij", "its", "tests", "foreground.ps1").toString() + //val scriptPath = Paths.get(System.getProperty("user.dir"), "foreground.ps1").toString() + + // Create a ProcessBuilder to execute the PowerShell script + val processBuilder = ProcessBuilder("powershell.exe", "-File", scriptPath) + processBuilder.redirectErrorStream(true) + + // Start the process + val process = processBuilder.start() + + // Read the output from the PowerShell script + val reader = BufferedReader(InputStreamReader(process.inputStream)) + var line: String? + while ((reader.readLine().also { line = it }) != null) { + println(line) + } + + // Wait for the process to complete and get the exit value + val exitCode = process.waitFor() + println("Exited with code: $exitCode") + } catch (e: Exception) { + e.printStackTrace() + } + } } diff --git a/its/src/test/kotlin/org/sonarlint/intellij/its/tests/foreground.ps1 b/its/src/test/kotlin/org/sonarlint/intellij/its/tests/foreground.ps1 new file mode 100644 index 000000000..1e768dfad --- /dev/null +++ b/its/src/test/kotlin/org/sonarlint/intellij/its/tests/foreground.ps1 @@ -0,0 +1,128 @@ +Add-Type @" +using System; +using System.Runtime.InteropServices; + +public class CustomUser32 { + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount); + + public static string GetActiveWindowTitle() { + IntPtr handle = GetForegroundWindow(); + System.Text.StringBuilder sb = new System.Text.StringBuilder(256); + if (GetWindowText(handle, sb, sb.Capacity) > 0) { + return sb.ToString(); + } + return null; + } +} +"@ + +function Print-AllProcessesWithMainWindowTitle { + $processes = Get-Process | Select-Object Id, ProcessName, MainWindowTitle + + # Write the output to the console + $processes | ForEach-Object { Write-Output "Id: $_.Id, ProcessName: $_.ProcessName, MainWindowTitle: $_.MainWindowTitle" } +} + +function Bring-WindowToForegroundByTitle { + param ( + [string]$windowTitle, + [int]$maxRetries = 15, + [int]$retryDelay = 1000 + ) + + Write-Output "Starting to try bring window to foreground" + + for ($i = 0; $i -lt $maxRetries; $i++) { + $process = Get-Process | Where-Object { $_.MainWindowTitle -eq $windowTitle } | Select-Object -First 1 + + if ($null -ne $process) { + if ([CustomUser32]::SetForegroundWindow($process.MainWindowHandle)) { + Write-Output "Brought window with title '$windowTitle' to the foreground." + return + } else { + Start-Sleep -Milliseconds $retryDelay + } + } else { + Write-Output "No window with the title '$windowTitle' found." + return + } + } + + Write-Output "Failed to bring window with title '$windowTitle' to the foreground after $maxRetries attempts." +} + +function Get-FocusedProcess { + Add-Type @" + using System; + using System.Runtime.InteropServices; + using System.Diagnostics; + public class User32 { + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + [DllImport("user32.dll")] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + } +"@ + + # Initialize the processId variable + [uint32]$processId = 0 + + $hWnd = [User32]::GetForegroundWindow() + $null = [User32]::GetWindowThreadProcessId($hWnd, [ref]$processId) + $process = Get-Process -Id $processId + return $process +} + + +function Set-ForegroundWindow { + param ( + [int]$hWnd + ) + Add-Type @" + using System; + using System.Runtime.InteropServices; + public class Eray32 { + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetForegroundWindow(IntPtr hWnd); + } +"@ + [Eray32]::SetForegroundWindow([IntPtr]::new($hWnd)) +} + +function Bring-JavaProcessToForeground { + $javaProcesses = Get-Process -Name java + foreach ($process in $javaProcesses) { + $hWnd = $process.MainWindowHandle + if ($hWnd -ne [IntPtr]::Zero) { + Set-ForegroundWindow -hWnd $hWnd + Write-Output "Brought Java process with PID $($process.Id) to the foreground." + return + } + } + Write-Output "No Java process with a main window found." +} + +# Example usage +Bring-JavaProcessToForeground + +# Example usage +$focusedProcess = Get-FocusedProcess +Write-Output "Focused Process ID: $($focusedProcess.Id)" +Write-Output "Focused Process Name: $($focusedProcess.ProcessName)" + + +Write-Output "Starting to print process" + +# Print all processes with main window titles +Print-AllProcessesWithMainWindowTitle + +# Example call to bring the window with title "Welcome to IntelliJ IDEA" to the foreground +Bring-WindowToForegroundByTitle -windowTitle "Welcome to IntelliJ IDEA"