Skip to content

Commit

Permalink
Remove unnecessary try block
Browse files Browse the repository at this point in the history
* Remove an unnecessary try block, the code in the block cannot throw.
* Move a defer closure earlier to make flow clearer.
  • Loading branch information
Steelskin committed Jun 28, 2024
1 parent 79541bb commit 44eacd8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
26 changes: 13 additions & 13 deletions Sources/WinAppDriver/WinAppDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<SECURITY_ATTRIBUTES>.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<WCHAR>(mutating: outputFile), DWORD(GENERIC_WRITE),
DWORD(FILE_SHARE_READ), &securityAttributes,
Expand All @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 6 additions & 9 deletions Tests/WinAppDriverTests/AppDriverOptionsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 44eacd8

Please sign in to comment.