| 1 | | import Foundation |
| 2 | | |
| 3 | | /// A SwiftPM package, as far as exporting its coverage needs to know it. |
| 4 | | public struct SwiftPackage: Sendable, Equatable { |
| 5 | | public let name: String |
| 6 | | public let testTargets: [String] |
| 7 | | |
| 8 | | /// - Throws: `SwiftPMExportError.unreadableDescription` unless given the output of |
| 9 | | /// `swift package describe --type json`. |
| 10 | 5 | public init(describeJSON: Data) throws { |
| 11 | 5 | guard let described = try? JSONDecoder().decode(Description.self, from: describeJSON) |
| 12 | 5 | else { throw SwiftPMExportError.unreadableDescription } |
| 13 | 4 | name = described.name |
| 14 | 10 | testTargets = described.targets.filter { $0.type == "test" }.map(\.name) |
| 15 | 4 | } |
| 16 | | |
| 17 | | private struct Description: Decodable { |
| 18 | | struct Target: Decodable { |
| 19 | | let name: String |
| 20 | | let type: String |
| 21 | | } |
| 22 | | let name: String |
| 23 | | let targets: [Target] |
| 24 | | } |
| 25 | | } |
| 26 | | |
| 27 | | /// Where SwiftPM puts what a test run with coverage leaves. |
| 28 | | public struct SwiftPMBuild: Sendable, Equatable { |
| 29 | | public let binFolder: String |
| 30 | | public let profilePath: String |
| 31 | | |
| 32 | | /// Takes what `swift build --show-bin-path` and `swift test --show-codecov-path` |
| 33 | | /// print. |
| 34 | 1 | public init(binPathOutput: String, codecovPathOutput: String) { |
| 35 | 1 | binFolder = binPathOutput.trimmingCharacters(in: .whitespacesAndNewlines) |
| 36 | 1 | let codecovPath = codecovPathOutput.trimmingCharacters(in: .whitespacesAndNewlines) |
| 37 | 1 | profilePath = |
| 38 | 1 | URL(fileURLWithPath: codecovPath).deletingLastPathComponent() |
| 39 | 1 | .appendingPathComponent("default.profdata").path |
| 40 | 1 | } |
| 41 | | } |
| 42 | | |
| 43 | | /// How to make the `llvm-cov export` of a package's test run (D2). |
| 44 | | public struct SwiftPMExport: Sendable, Equatable { |
| 45 | | /// The arguments `xcrun` takes to write the export on standard output. |
| 46 | | public let xcrunArguments: [String] |
| 47 | | /// Bundles in the bin folder that the package's test targets don't build, by name. |
| 48 | | public let ignoredBundles: [String] |
| 49 | | |
| 50 | | /// Takes the declared test targets' bundles, or under SwiftPM's native build system |
| 51 | | /// the one `<package>PackageTests.xctest` that holds them all. |
| 52 | | /// - Throws: `SwiftPMExportError.noTestBundles` when neither is there, or |
| 53 | | /// `missingTestBundles` when only some declared targets have a bundle. |
| 54 | 8 | public init(package: SwiftPackage, build: SwiftPMBuild, binFolderEntries: [String]) throws { |
| 55 | 16 | let found = Set(binFolderEntries.filter { $0.hasSuffix(Self.bundleSuffix) }) |
| 56 | 8 | let selected = try Self.bundles(of: package, among: found, in: build.binFolder) |
| 57 | 7 | let objects = selected.flatMap { bundle in |
| 58 | 7 | let executable = bundle.dropLast(Self.bundleSuffix.count) |
| 59 | 7 | return ["-object", "\(build.binFolder)/\(bundle)/Contents/MacOS/\(executable)"] |
| 60 | 7 | } |
| 61 | 4 | xcrunArguments = |
| 62 | 4 | ["llvm-cov", "export"] + objects |
| 63 | 4 | + ["-instr-profile=\(build.profilePath)", "-skip-functions"] |
| 64 | 4 | ignoredBundles = found.subtracting(selected).sorted() |
| 65 | 4 | } |
| 66 | | |
| 67 | | private static let bundleSuffix = ".xctest" |
| 68 | | |
| 69 | | private static func bundles( |
| 70 | | of package: SwiftPackage, among found: Set<String>, in binFolder: String |
| 71 | 8 | ) throws -> [String] { |
| 72 | 8 | let nativeName = package.name + "PackageTests" |
| 73 | 8 | // A test target may carry that name itself; its bundle then holds only its tests. |
| 74 | 8 | if found.contains(nativeName + bundleSuffix) && !package.testTargets.contains(nativeName) { |
| 75 | 1 | return [nativeName + bundleSuffix] |
| 76 | 7 | } |
| 77 | 12 | let missing = package.testTargets.filter { !found.contains($0 + bundleSuffix) } |
| 78 | 7 | if missing.count == package.testTargets.count { |
| 79 | 2 | throw SwiftPMExportError.noTestBundles(binFolder: binFolder) |
| 80 | 5 | } |
| 81 | 5 | guard missing.isEmpty else { |
| 82 | 2 | throw SwiftPMExportError.missingTestBundles( |
| 83 | 2 | targets: missing.sorted(), binFolder: binFolder) |
| 84 | 3 | } |
| 85 | 6 | return package.testTargets.map { $0 + bundleSuffix }.sorted() |
| 86 | 8 | } |
| 87 | | } |
| 88 | | |
| 89 | | public enum SwiftPMExportError: Error, Equatable { |
| 90 | | case unreadableDescription |
| 91 | | case noProfile(path: String) |
| 92 | | case noTestBundles(binFolder: String) |
| 93 | | case missingTestBundles(targets: [String], binFolder: String) |
| 94 | | } |
| 95 | | |
| 96 | | extension SwiftPMExportError: CustomStringConvertible { |
| 97 | 20 | public var description: String { |
| 98 | 20 | let remedy = "Run the tests with --enable-code-coverage." |
| 99 | 20 | return switch self { |
| 100 | 20 | case .unreadableDescription: |
| 101 | 2 | "Cannot read the output of `swift package describe --type json`." |
| 102 | 20 | case .noProfile(let path): "There is no coverage profile at \(path). \(remedy)" |
| 103 | 20 | case .noTestBundles(let binFolder): |
| 104 | 7 | "No test bundle of this package is in \(binFolder). \(remedy)" |
| 105 | 20 | case .missingTestBundles(let targets, let binFolder): |
| 106 | 7 | "No test bundle for \(targets.joined(separator: ", ")) is in \(binFolder). \(remedy)" |
| 107 | 20 | } |
| 108 | 20 | } |
| 109 | | } |