Skip to content

Commit abc231b

Browse files
test: Use non optional data for string to data (#5028)
Prefer using Data(foo.utf8) over using foo.data(using: .utf8)
1 parent 7afad57 commit abc231b

19 files changed

+97
-97
lines changed

Tests/SentryTests/Helper/SentryFileManagerTestExtension.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ extension SentryFileManager {
99
static func prepareInitError() {
1010
deleteInternalPath()
1111
do {
12-
let data = "hello".data(using: .utf8)
13-
try data?.write(to: getInternalPath())
12+
let data = Data("hello".utf8)
13+
try data.write(to: getInternalPath())
1414
} catch {
1515
XCTFail("Couldn't create file for init error of SentryFileManager.")
1616
}

Tests/SentryTests/Helper/SentryFileManagerTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class SentryFileManagerTests: XCTestCase {
182182

183183
let dsStoreFile = "\(sut.basePath)/.DS_Store"
184184

185-
let result = FileManager.default.createFile(atPath: dsStoreFile, contents: "some data".data(using: .utf8))
185+
let result = FileManager.default.createFile(atPath: dsStoreFile, contents: Data("some data".utf8))
186186
XCTAssertEqual(result, true)
187187

188188
sut.deleteOldEnvelopeItems()
@@ -205,7 +205,7 @@ class SentryFileManagerTests: XCTestCase {
205205

206206
let textFilePath = "\(sut.basePath)/something.txt"
207207

208-
let result = FileManager.default.createFile(atPath: textFilePath, contents: "some data".data(using: .utf8))
208+
let result = FileManager.default.createFile(atPath: textFilePath, contents: Data("some data".utf8))
209209
XCTAssertEqual(result, true)
210210

211211
sut.deleteOldEnvelopeItems()
@@ -785,8 +785,8 @@ class SentryFileManagerTests: XCTestCase {
785785
let fileManager = FileManager.default
786786
let appHangEventFilePath = try XCTUnwrap(Dynamic(sut).appHangEventFilePath.asString)
787787

788-
fileManager.createFile(atPath: appHangEventFilePath, contents: "garbage".data(using: .utf8)!, attributes: nil)
789-
788+
fileManager.createFile(atPath: appHangEventFilePath, contents: Data("garbage".utf8), attributes: nil)
789+
790790
// Act
791791
XCTAssertNil(sut.readAppHangEvent())
792792
}
@@ -822,8 +822,8 @@ class SentryFileManagerTests: XCTestCase {
822822
let fileManager = FileManager.default
823823
let appHangEventFilePath = try XCTUnwrap(Dynamic(sut).appHangEventFilePath.asString)
824824

825-
fileManager.createFile(atPath: appHangEventFilePath, contents: "garbage".data(using: .utf8)!, attributes: nil)
826-
825+
fileManager.createFile(atPath: appHangEventFilePath, contents: Data("garbage".utf8), attributes: nil)
826+
827827
// Act && Assert
828828
XCTAssertTrue(sut.appHangEventExists())
829829
}

Tests/SentryTests/Integrations/Performance/IO/DataSentryTracingIntegrationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class DataSentryTracingIntegrationTests: XCTestCase {
1111
return provider
1212
}()
1313

14-
let data = "SOME DATA".data(using: .utf8)!
15-
14+
let data = Data("SOME DATA".utf8)
15+
1616
var fileUrlToRead: URL!
1717
var fileUrlToWrite: URL!
1818
var ignoredFileUrlToRead: URL!

Tests/SentryTests/Integrations/Performance/IO/FileManagerTracingIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class FileManagerSentryTracingIntegrationTests: XCTestCase {
1212
return provider
1313
}()
1414

15-
let data = "SOME DATA".data(using: .utf8)!
15+
let data = Data("SOME DATA".utf8)
1616

1717
var fileSrcUrl: URL!
1818
var fileDestUrl: URL!

Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SentryFileIOTrackerTests: XCTestCase {
1111
let sentryPath = try! TestFileManager(options: Options()).sentryPath
1212
let sentryUrl = URL(fileURLWithPath: try! TestFileManager(options: Options()).sentryPath)
1313
let dateProvider = TestCurrentDateProvider()
14-
let data = "SOME DATA".data(using: .utf8)!
14+
let data = Data("SOME DATA".utf8)
1515
let threadInspector = TestThreadInspector.instance
1616
let imageProvider = TestDebugImageProvider()
1717

Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackingIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import XCTest
66
class SentryFileIOTrackingIntegrationTests: XCTestCase {
77

88
private class Fixture {
9-
let data = "SOME DATA".data(using: .utf8) ?? Data()
9+
let data = Data("SOME DATA".utf8)
1010
let filePath: String!
1111
let fileURL: URL!
1212
let fileDirectory: URL!

Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,13 +1011,13 @@ class SentryNetworkTrackerTests: XCTestCase {
10111011
var request = $0
10121012

10131013
request.httpMethod = "POST"
1014-
request.httpBody = """
1014+
request.httpBody = Data("""
10151015
{
10161016
"operationName": "someOperationName",
10171017
"variables": { "a": 1 },
10181018
"query": "query someOperationName { someField }"
10191019
}
1020-
""".data(using: .utf8)
1020+
""".utf8)
10211021
request.allHTTPHeaderFields = ["content-type": "application/json"]
10221022

10231023
return request

Tests/SentryTests/Integrations/SessionReplay/SentryOnDemandReplayTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class SentryOnDemandReplayTests: XCTestCase {
165165
let end = dateProvider.date()
166166

167167
//Creating a file where the replay would be written to cause an error in the writer
168-
try "tempFile".data(using: .utf8)?.write(to: outputPath.appendingPathComponent("0.0.mp4"))
168+
try Data("tempFile".utf8).write(to: outputPath.appendingPathComponent("0.0.mp4"))
169169

170170
XCTAssertThrowsError(try sut.createVideoWith(beginning: start, end: end))
171171
}

Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SentryViewHierarchyIntegrationTests: XCTestCase {
1111

1212
init() {
1313
let testViewHierarchy = TestSentryViewHierarchy()
14-
testViewHierarchy.result = "view hierarchy".data(using: .utf8) ?? Data()
14+
testViewHierarchy.result = Data("view hierarchy".utf8)
1515
viewHierarchy = testViewHierarchy
1616
}
1717

Tests/SentryTests/PrivateSentrySDKOnlyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ class PrivateSentrySDKOnlyTests: XCTestCase {
117117
}
118118

119119
func testEnvelopeWithData() throws {
120-
let itemData = "{}\n{\"length\":0,\"type\":\"attachment\"}\n".data(using: .utf8)!
120+
let itemData = Data("{}\n{\"length\":0,\"type\":\"attachment\"}\n".utf8)
121121
XCTAssertNotNil(PrivateSentrySDKOnly.envelope(with: itemData))
122122
}
123123

124124
func testEnvelopeWithDataLengthGtZero() throws {
125-
let itemData = "{}\n{\"length\":1,\"type\":\"attachment\"}\n".data(using: .utf8)!
125+
let itemData = Data("{}\n{\"length\":1,\"type\":\"attachment\"}\n".utf8)
126126
XCTAssertNil(PrivateSentrySDKOnly.envelope(with: itemData))
127127
}
128128

0 commit comments

Comments
 (0)