Skip to content
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

[SwiftLint] Remove force_try from disabled_rules #582

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
disabled_rules:
- todo
- force_try

included:
- Sources
Expand Down
33 changes: 30 additions & 3 deletions Sources/Nimble/Adapters/AssertionRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,48 @@ public class AssertionRecorder: AssertionHandler {
}
}

extension NMBExceptionCapture {
internal func tryBlockThrows(_ unsafeBlock: () throws -> Void) throws {
var catchedError: Error?
tryBlock {
do {
try unsafeBlock()
} catch {
catchedError = error
}
}
if let error = catchedError {
throw error
}
}
}

/// Allows you to temporarily replace the current Nimble assertion handler with
/// the one provided for the scope of the closure.
///
/// Once the closure finishes, then the original Nimble assertion handler is restored.
///
/// @see AssertionHandler
public func withAssertionHandler(_ tempAssertionHandler: AssertionHandler, closure: () throws -> Void) {
public func withAssertionHandler(_ tempAssertionHandler: AssertionHandler,
file: FileString = #file,
line: UInt = #line,
closure: () throws -> Void) {
let environment = NimbleEnvironment.activeInstance
let oldRecorder = environment.assertionHandler
let capturer = NMBExceptionCapture(handler: nil, finally: ({
environment.assertionHandler = oldRecorder
}))
environment.assertionHandler = tempAssertionHandler
capturer.tryBlock {
try! closure()

do {
try capturer.tryBlockThrows {
try closure()
}
} catch {
let failureMessage = FailureMessage()
failureMessage.stringValue = "unexpected error thrown: <\(error)>"
let location = SourceLocation(file: file, line: line)
tempAssertionHandler.assert(false, message: failureMessage, location: location)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Nimble/Adapters/NMBExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ internal struct ObjCMatcherWrapper: Matcher {

func matches(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.matches(
// swiftlint:disable:next force_try
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
}

func doesNotMatch(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.doesNotMatch(
// swiftlint:disable:next force_try
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
Expand Down
1 change: 1 addition & 0 deletions Sources/Nimble/Matchers/AllPass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extension NMBObjCMatcher {
} else {
let failureMessage = FailureMessage()
let result = matcher.matches(
// swiftlint:disable:next force_try
({ try! expr.evaluate() }),
failureMessage: failureMessage,
location: expr.location
Expand Down
10 changes: 7 additions & 3 deletions Sources/Nimble/Matchers/RaisesException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public func raiseException(
exception = e
}), finally: nil)

capture.tryBlock {
_ = try! actualExpression.evaluate()
return
do {
try capture.tryBlockThrows {
_ = try actualExpression.evaluate()
}
} catch {
failureMessage.stringValue = "unexpected error thrown: <\(error)>"
return false
}

setFailureMessageForException(
Expand Down
8 changes: 6 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAllOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ extension NMBObjCMatcher {
return predicate.satisfies({ try expression.evaluate() }, location: actualExpression.location).toSwift()
} else {
let failureMessage = FailureMessage()
// swiftlint:disable:next line_length
let success = matcher.matches({ try! expression.evaluate() }, failureMessage: failureMessage, location: actualExpression.location)
let success = matcher.matches(
// swiftlint:disable:next force_try
{ try! expression.evaluate() },
failureMessage: failureMessage,
location: actualExpression.location
)
return PredicateResult(bool: success, message: failureMessage.toExpectationMessage())
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAnyOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ extension NMBObjCMatcher {
return predicate.satisfies({ try expression.evaluate() }, location: actualExpression.location).toSwift()
} else {
let failureMessage = FailureMessage()
// swiftlint:disable:next line_length
let success = matcher.matches({ try! expression.evaluate() }, failureMessage: failureMessage, location: actualExpression.location)
let success = matcher.matches(
// swiftlint:disable:next force_try
{ try! expression.evaluate() },
failureMessage: failureMessage,
location: actualExpression.location
)
return PredicateResult(bool: success, message: failureMessage.toExpectationMessage())
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NimbleTests/Helpers/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func failsWithErrorMessage(_ messages: [String], file: FileString = #file, line:
var lineNumber = line

let recorder = AssertionRecorder()
withAssertionHandler(recorder, closure: closure)
withAssertionHandler(recorder, file: file, line: line, closure: closure)

for msg in messages {
var lastFailure: AssertionRecord?
Expand Down