Skip to content

Commit 2d05414

Browse files
weissiJohannes Weiss
andauthored
Improve API usages around String/Data/ByteBuffer (#109)
Co-authored-by: Johannes Weiss <[email protected]>
1 parent fb0914a commit 2d05414

File tree

7 files changed

+14
-19
lines changed

7 files changed

+14
-19
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PackageDescription
44

55
let package = Package(
66
name: "swiftly",
7+
platforms: [.macOS(.v13)],
78
products: [
89
.executable(
910
name: "swiftly",

Sources/Swiftly/List.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct List: SwiftlyCommand {
7070

7171
let message = "Installed \(modifier) toolchains"
7272
SwiftlyCore.print(message)
73-
SwiftlyCore.print(String(repeating: "-", count: message.utf8.count))
73+
SwiftlyCore.print(String(repeating: "-", count: message.count))
7474
for toolchain in toolchains {
7575
printToolchain(toolchain)
7676
}

Sources/Swiftly/ListAvailable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct ListAvailable: SwiftlyCommand {
9191

9292
let message = "Available \(modifier) toolchains"
9393
SwiftlyCore.print(message)
94-
SwiftlyCore.print(String(repeating: "-", count: message.utf8.count))
94+
SwiftlyCore.print(String(repeating: "-", count: message.count))
9595
for toolchain in toolchains {
9696
printToolchain(toolchain)
9797
}

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ public struct SwiftlyHTTPClient {
6565

6666
guard case .ok = response.status else {
6767
var message = "received status \"\(response.status)\" when reaching \(url)"
68-
if let json = response.buffer.getString(at: 0, length: response.buffer.readableBytes) {
69-
message += ": \(json)"
70-
}
68+
let json = String(buffer: response.buffer)
69+
message += ": \(json)"
7170
throw Error(message: message)
7271
}
7372

@@ -186,9 +185,5 @@ public struct SwiftlyHTTPClient {
186185
}
187186

188187
private class HTTPClientWrapper {
189-
fileprivate let inner = HTTPClient(eventLoopGroupProvider: .singleton)
190-
191-
deinit {
192-
try? self.inner.syncShutdown()
193-
}
188+
fileprivate let inner = HTTPClient.shared
194189
}

Tests/SwiftlyTests/SelfUpdateTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ final class SelfUpdateTests: SwiftlyTests {
3131
try buffer.writeJSONEncodable(nextRelease)
3232
return HTTPClientResponse(body: .bytes(buffer))
3333
case "github.com":
34-
var buffer = ByteBuffer()
35-
buffer.writeString(latestVersion)
34+
let buffer = ByteBuffer(string: latestVersion)
3635
return HTTPClientResponse(body: .bytes(buffer))
3736
default:
3837
throw SwiftlyTestError(message: "unknown url host: \(String(describing: url.host))")
@@ -43,7 +42,7 @@ final class SelfUpdateTests: SwiftlyTests {
4342
func runSelfUpdateTest(latestVersion: String, shouldUpdate: Bool = true) async throws {
4443
try await self.withTestHome {
4544
let swiftlyURL = Swiftly.currentPlatform.swiftlyBinDir.appendingPathComponent("swiftly", isDirectory: false)
46-
try "old".data(using: .utf8)!.write(to: swiftlyURL)
45+
try Data("old".utf8).write(to: swiftlyURL)
4746

4847
var update = try self.parseCommand(SelfUpdate.self, ["self-update"])
4948
update.httpClient = Self.makeMockHTTPClient(latestVersion: latestVersion)
@@ -52,9 +51,9 @@ final class SelfUpdateTests: SwiftlyTests {
5251
let swiftly = try Data(contentsOf: swiftlyURL)
5352

5453
if shouldUpdate {
55-
XCTAssertEqual(String(data: swiftly, encoding: .utf8), latestVersion)
54+
XCTAssertEqual(String(decoding: swiftly, as: UTF8.self), latestVersion)
5655
} else {
57-
XCTAssertEqual(String(data: swiftly, encoding: .utf8), "old")
56+
XCTAssertEqual(String(decoding: swiftly, as: UTF8.self), "old")
5857
}
5958
}
6059
}
@@ -74,7 +73,7 @@ final class SelfUpdateTests: SwiftlyTests {
7473
func testSelfUpdateIntegration() async throws {
7574
try await self.withTestHome {
7675
let swiftlyURL = Swiftly.currentPlatform.swiftlyBinDir.appendingPathComponent("swiftly", isDirectory: false)
77-
try "old".data(using: .utf8)!.write(to: swiftlyURL)
76+
try Data("old".utf8).write(to: swiftlyURL)
7877

7978
var update = try self.parseCommand(SelfUpdate.self, ["self-update"])
8079
try await update.run()

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public struct MockToolchainDownloader: HTTPRequestExecutor {
443443
echo '\(toolchain.name)'
444444
"""
445445

446-
let data = script.data(using: .utf8)!
446+
let data = Data(script.utf8)
447447
try data.write(to: executablePath)
448448

449449
// make the file executable

Tests/SwiftlyTests/UseTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ final class UseTests: SwiftlyTests {
230230
// Add an unrelated executable to the binary directory.
231231
let existingFileName = "existing"
232232
let existingExecutableURL = Swiftly.currentPlatform.swiftlyBinDir.appendingPathComponent(existingFileName)
233-
let data = "hello world\n".data(using: .utf8)!
233+
let data = Data("hello world\n".utf8)
234234
try data.write(to: existingExecutableURL)
235235

236236
for (toolchain, files) in spec {
@@ -264,7 +264,7 @@ final class UseTests: SwiftlyTests {
264264
let existingText = "existing"
265265
for fileName in existingExecutables {
266266
let existingExecutableURL = Swiftly.currentPlatform.swiftlyBinDir.appendingPathComponent(fileName)
267-
let data = existingText.data(using: .utf8)!
267+
let data = Data(existingText.utf8)
268268
try data.write(to: existingExecutableURL)
269269
}
270270

0 commit comments

Comments
 (0)