From 44eacd80d4411c506b210ca9b938b6a6a7510dd0 Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Fri, 28 Jun 2024 12:37:41 -0700 Subject: [PATCH] Remove unnecessary try block * Remove an unnecessary try block, the code in the block cannot throw. * Move a defer closure earlier to make flow clearer. --- Sources/WinAppDriver/WinAppDriver.swift | 26 +++++++++---------- .../AppDriverOptionsTest.swift | 15 +++++------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Sources/WinAppDriver/WinAppDriver.swift b/Sources/WinAppDriver/WinAppDriver.swift index e3eb090..31bf80b 100644 --- a/Sources/WinAppDriver/WinAppDriver.swift +++ b/Sources/WinAppDriver/WinAppDriver.swift @@ -46,13 +46,24 @@ public class WinAppDriver: WebDriver { var childStdinHandle: HANDLE? = nil do { var launchOptions = ProcessLaunchOptions() + + // Close our handles when the process has launched. The child process keeps a copy. + defer { + if let handle = launchOptions.stdoutHandle { + CloseHandle(handle) + } + if let handle = launchOptions.stdinHandle { + CloseHandle(handle) + } + } + if let outputFile = outputFile { // Open the output file for writing to the child stdout. var securityAttributes = SECURITY_ATTRIBUTES() securityAttributes.nLength = DWORD(MemoryLayout.size) securityAttributes.bInheritHandle = true - launchOptions.stdoutHandle = try outputFile.withCString(encodedAs: UTF16.self) { - outputFile throws in + launchOptions.stdoutHandle = outputFile.withCString(encodedAs: UTF16.self) { + outputFile in CreateFileW( UnsafeMutablePointer(mutating: outputFile), DWORD(GENERIC_WRITE), DWORD(FILE_SHARE_READ), &securityAttributes, @@ -70,7 +81,6 @@ public class WinAppDriver: WebDriver { // pipe here to keep stdin open until the child process is closed. var childReadInputHandle: HANDLE? if !CreatePipe(&childReadInputHandle, &childStdinHandle, &securityAttributes, 0) { - CloseHandle(launchOptions.stdoutHandle) throw Win32Error.getLastError(apiName: "CreatePipe") } launchOptions.stdinHandle = childReadInputHandle @@ -79,16 +89,6 @@ public class WinAppDriver: WebDriver { launchOptions.spawnNewConsole = false } - // Close our handles when the process has launched. The child process keeps a copy. - defer { - if let handle = launchOptions.stdoutHandle { - CloseHandle(handle) - } - if let handle = launchOptions.stdinHandle { - CloseHandle(handle) - } - } - processTree = try Win32ProcessTree( path: executablePath, args: [ip, String(port)], options: launchOptions) } catch let error as Win32Error { diff --git a/Tests/WinAppDriverTests/AppDriverOptionsTest.swift b/Tests/WinAppDriverTests/AppDriverOptionsTest.swift index e7272c1..96a52c6 100644 --- a/Tests/WinAppDriverTests/AppDriverOptionsTest.swift +++ b/Tests/WinAppDriverTests/AppDriverOptionsTest.swift @@ -13,15 +13,12 @@ class AppDriverOptionsTest: XCTestCase { /// Tests that redirecting stdout to a file works. func testStdoutRedirectToFile() throws { - let outputFile = try { - // Start a new instance of msinfo32 and write the output to a file. - let outputFile = tempFileName() - let _ = try MSInfo32App( - winAppDriver: WinAppDriver.start( - outputFile: outputFile - )) - return outputFile - }() + // Start a new instance of msinfo32 and write the output to a file. + let outputFile = tempFileName() + let _ = try MSInfo32App( + winAppDriver: WinAppDriver.start( + outputFile: outputFile + )) // Read the output file. let output = try String(contentsOfFile: outputFile, encoding: .utf16LittleEndian)