Skip to content

Fixed issue with string force unwrap #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public struct ShellExecutor: ShellExecuting {

task.waitUntilExit()

return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
let result = String(data: data, encoding: .utf8) ?? ""
return result.trimmingCharacters(in: .whitespacesAndNewlines)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
let result = String(data: data, encoding: .utf8) ?? ""
return result.trimmingCharacters(in: .whitespacesAndNewlines)
let result = String(data: data, encoding: .utf8).map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } ?? ""
return result

So you do the trimming only if not nil

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

} catch {
return error.localizedDescription
}
Expand Down Expand Up @@ -109,7 +110,7 @@ public struct ShellExecutor: ShellExecuting {
outputQueue.async(group: group, qos: .userInitiated) {
// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
stdoutString = String(data: stdoutData, encoding: .utf8)!
stdoutString = String(data: stdoutData, encoding: .utf8) ?? ""
}

outputQueue.async(group: group, qos: .userInitiated) {
Expand Down