From 42a9e2a4582a1dedfefb2f5870f10332423b03b2 Mon Sep 17 00:00:00 2001 From: mtj0928 Date: Sat, 14 Sep 2024 12:37:22 +0900 Subject: [PATCH] Suppress swiftlint warnings --- Sources/ScipioKit/Executor.swift | 23 ++++++++++++--------- Tests/ScipioKitTests/CacheSystemTests.swift | 2 +- Tests/ScipioKitTests/RunnerTests.swift | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Sources/ScipioKit/Executor.swift b/Sources/ScipioKit/Executor.swift index 4d55e05f..f4b8344e 100644 --- a/Sources/ScipioKit/Executor.swift +++ b/Sources/ScipioKit/Executor.swift @@ -11,6 +11,11 @@ protocol ErrorDecoder { func decode(_ result: ExecutorResult) throws -> String? } +enum ExecutorError: Error { + /// A case representing the encoding of the output is invalid. + case invalidEncoding +} + protocol ExecutorResult { var arguments: [String] { get } /// The environment with which the process was launched. @@ -163,21 +168,19 @@ struct ProcessExecutor: Executor { extension ExecutorResult { func unwrapOutput() throws -> String { - switch output { - case .success(let data): - return String(decoding: Data(data), as: UTF8.self) - case .failure(let error): - throw error + let data = try output.get() + guard let string = String(data: Data(data), encoding: .utf8) else { + throw ExecutorError.invalidEncoding } + return string } func unwrapStdErrOutput() throws -> String { - switch stderrOutput { - case .success(let data): - return String(decoding: Data(data), as: UTF8.self) - case .failure(let error): - throw error + let data = try stderrOutput.get() + guard let string = String(data: Data(data), encoding: .utf8) else { + throw ExecutorError.invalidEncoding } + return string } } diff --git a/Tests/ScipioKitTests/CacheSystemTests.swift b/Tests/ScipioKitTests/CacheSystemTests.swift index dfdcb660..1f8c2e0d 100644 --- a/Tests/ScipioKitTests/CacheSystemTests.swift +++ b/Tests/ScipioKitTests/CacheSystemTests.swift @@ -44,7 +44,7 @@ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys, .prettyPrinted] let data = try encoder.encode(cacheKey) - let rawString = try XCTUnwrap(String(decoding: data, as: UTF8.self)) + let rawString = try XCTUnwrap(String(data: data, encoding: .utf8)) let expected = """ { "buildOptions" : { diff --git a/Tests/ScipioKitTests/RunnerTests.swift b/Tests/ScipioKitTests/RunnerTests.swift index 4bf9b4b1..60b72269 100644 --- a/Tests/ScipioKitTests/RunnerTests.swift +++ b/Tests/ScipioKitTests/RunnerTests.swift @@ -136,7 +136,7 @@ final class RunnerTests: XCTestCase { fileManager.fileExists(atPath: moduleMapPath), "Should exist a modulemap" ) - let moduleMapContents = try XCTUnwrap(fileManager.contents(atPath: moduleMapPath).flatMap { String(decoding: $0, as: UTF8.self) }) + let moduleMapContents = try XCTUnwrap(fileManager.contents(atPath: moduleMapPath).flatMap { String(data: $0, encoding: .utf8) }) XCTAssertEqual( moduleMapContents, """ @@ -186,7 +186,7 @@ final class RunnerTests: XCTestCase { fileManager.fileExists(atPath: moduleMapPath), "Should exist a modulemap" ) - let moduleMapContents = try XCTUnwrap(fileManager.contents(atPath: moduleMapPath).flatMap { String(decoding: $0, as: UTF8.self) }) + let moduleMapContents = try XCTUnwrap(fileManager.contents(atPath: moduleMapPath).flatMap { String(data: $0, encoding: .utf8) }) XCTAssertEqual( moduleMapContents, """ @@ -669,7 +669,7 @@ final class RunnerTests: XCTestCase { XCTAssertTrue(fileManager.fileExists(atPath: moduleMapPath.path)) let generatedModuleMapData = try XCTUnwrap(fileManager.contents(atPath: moduleMapPath.path)) - let generatedModuleMapContents = String(decoding: generatedModuleMapData, as: UTF8.self) + let generatedModuleMapContents = try XCTUnwrap(String(data: generatedModuleMapData, encoding: .utf8)) let expectedModuleMap = """ framework module MyTarget {