diff --git a/supacode/Domain/WorktreeProjectKind.swift b/supacode/Domain/WorktreeProjectKind.swift index 43419672..9f139348 100644 --- a/supacode/Domain/WorktreeProjectKind.swift +++ b/supacode/Domain/WorktreeProjectKind.swift @@ -96,12 +96,9 @@ enum WorktreeProjectKind: CaseIterable { /// when a repo carries a pathological manifest. nonisolated private static func markerFileContents(named name: String, in directory: URL) -> String? { let url = directory.appending(path: name, directoryHint: .notDirectory) - guard let handle = try? FileHandle(forReadingFrom: url), - let data = try? handle.read(upToCount: 128 * 1024) - else { - return nil - } - try? handle.close() + guard let handle = try? FileHandle(forReadingFrom: url) else { return nil } + defer { try? handle.close() } + guard let data = try? handle.read(upToCount: 128 * 1024) else { return nil } return String(data: data, encoding: .utf8) } diff --git a/supacodeTests/TemporaryProjectDirectory.swift b/supacodeTests/TemporaryProjectDirectory.swift index d538a3b1..399d0b47 100644 --- a/supacodeTests/TemporaryProjectDirectory.swift +++ b/supacodeTests/TemporaryProjectDirectory.swift @@ -31,23 +31,28 @@ private func makeTemporaryProjectDirectory( let directory = fileManager.temporaryDirectory .appending(path: "project-fixture-\(UUID().uuidString)") try fileManager.createDirectory(at: directory, withIntermediateDirectories: true) - for entry in entries { - if entry.hasSuffix("/") { + do { + for entry in entries { + if entry.hasSuffix("/") { + try fileManager.createDirectory( + at: directory.appending(path: String(entry.dropLast())), + withIntermediateDirectories: true + ) + } else { + try Data().write(to: directory.appending(path: entry)) + } + } + for (path, data) in contents { + let fileURL = directory.appending(path: path) try fileManager.createDirectory( - at: directory.appending(path: String(entry.dropLast())), + at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true ) - } else { - try Data().write(to: directory.appending(path: entry)) + try data.write(to: fileURL) } - } - for (path, data) in contents { - let fileURL = directory.appending(path: path) - try fileManager.createDirectory( - at: fileURL.deletingLastPathComponent(), - withIntermediateDirectories: true - ) - try data.write(to: fileURL) + } catch { + try? fileManager.removeItem(at: directory) + throw error } return directory }