This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
SPM Prep - Use XCTUnwrap instead of force-unwrapping for fixture paths in tests #756
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,14 +54,20 @@ extension RemoteTestCase { | |
| contentType: ResponseContentType, | ||
| status: Int32 = 200 | ||
| ) { | ||
| // There are hundreds of usages of the various `stubRemoteResponse` overloads. | ||
| // The pattern here should be to XCTUnwrap and throw. | ||
| // In the interest of moving along with the work, let's fail the tests at this level if the file is not found. | ||
| guard let stubPath = OHPathForFile(filename, type(of: self)) else { | ||
| return XCTFail("Could not find file at path '\(filename)'.") | ||
| } | ||
|
|
||
| stub(condition: condition) { _ in | ||
| let stubPath = OHPathForFile(filename, type(of: self)) | ||
| var headers: [NSObject: AnyObject]? | ||
|
|
||
| if contentType != .NoContentType { | ||
| headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject] | ||
| } | ||
| return OHHTTPStubs.fixture(filePath: stubPath!, status: status, headers: headers) | ||
| return OHHTTPStubs.fixture(filePath: stubPath, status: status, headers: headers) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -74,16 +80,22 @@ extension RemoteTestCase { | |
| /// - status: The status code to use for the response. Defaults to 200. | ||
| /// | ||
| func stubRemoteResponse(_ endpoint: String, filename: String, contentType: ResponseContentType, status: Int32 = 200) { | ||
| // There are hundreds of usages of the various `stubRemoteResponse` overloads. | ||
| // The pattern here should be to XCTUnwrap and throw. | ||
| // In the interest of moving along with the work, let's fail the tests at this level if the file is not found. | ||
| guard let stubPath = OHPathForFile(filename, type(of: self)) else { | ||
| return XCTFail("Could not find file at path '\(filename)'.") | ||
| } | ||
|
|
||
| stub(condition: { request in | ||
| return request.url?.absoluteString.range(of: endpoint) != nil | ||
| }) { _ in | ||
| let stubPath = OHPathForFile(filename, type(of: self)) | ||
| var headers: [NSObject: AnyObject]? | ||
|
|
||
| if contentType != .NoContentType { | ||
| headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject] | ||
| } | ||
| return fixture(filePath: stubPath!, status: status, headers: headers) | ||
| return fixture(filePath: stubPath, status: status, headers: headers) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -134,15 +146,20 @@ extension RemoteTestCase { | |
| return HTTPStubsResponse(error: notConnectedError) | ||
| } | ||
|
|
||
| let stubPath = OHPathForFile(files[callCounter], type(of: self)) | ||
| guard let stubPath = OHPathForFile(files[callCounter], type(of: self)) else { | ||
| XCTFail("Could not find file at path '\(files[callCounter])'.") | ||
| let error = NSError(domain: "RemoteTestCase", code: 0, userInfo: nil) | ||
| return HTTPStubsResponse(error: error) | ||
| } | ||
|
Comment on lines
+149
to
+153
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to fail in this way, also returning an error because we're inside a closure and the file path is computed based on the |
||
|
|
||
| callCounter += 1 | ||
|
|
||
| var headers: [NSObject: AnyObject]? | ||
| if contentType != .NoContentType { | ||
| headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject] | ||
| } | ||
|
|
||
| return fixture(filePath: stubPath!, status: status, headers: headers) | ||
| return fixture(filePath: stubPath, status: status, headers: headers) | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header dictionary for content type JSON is repeated all over the place.
It could be DRYed in some form. I decided to keep it out because this PR is already noisy. Besides, the focus of this stream of work is to prepare for SPM. DRYing this definition would be useful, but doesn't bring us closer to that goal.