1import Foundation
2
3/// A SwiftPM package, as far as exporting its coverage needs to know it.
4public 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`.
105 public init(describeJSON: Data) throws {
115 guard let described = try? JSONDecoder().decode(Description.self, from: describeJSON)
125 else { throw SwiftPMExportError.unreadableDescription }
134 name = described.name
1410 testTargets = described.targets.filter { $0.type == "test" }.map(\.name)
154 }
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.
28public 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.
341 public init(binPathOutput: String, codecovPathOutput: String) {
351 binFolder = binPathOutput.trimmingCharacters(in: .whitespacesAndNewlines)
361 let codecovPath = codecovPathOutput.trimmingCharacters(in: .whitespacesAndNewlines)
371 profilePath =
381 URL(fileURLWithPath: codecovPath).deletingLastPathComponent()
391 .appendingPathComponent("default.profdata").path
401 }
41}
42
43/// How to make the `llvm-cov export` of a package's test run (D2).
44public 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.
548 public init(package: SwiftPackage, build: SwiftPMBuild, binFolderEntries: [String]) throws {
5516 let found = Set(binFolderEntries.filter { $0.hasSuffix(Self.bundleSuffix) })
568 let selected = try Self.bundles(of: package, among: found, in: build.binFolder)
577 let objects = selected.flatMap { bundle in
587 let executable = bundle.dropLast(Self.bundleSuffix.count)
597 return ["-object", "\(build.binFolder)/\(bundle)/Contents/MacOS/\(executable)"]
607 }
614 xcrunArguments =
624 ["llvm-cov", "export"] + objects
634 + ["-instr-profile=\(build.profilePath)", "-skip-functions"]
644 ignoredBundles = found.subtracting(selected).sorted()
654 }
66
67 private static let bundleSuffix = ".xctest"
68
69 private static func bundles(
70 of package: SwiftPackage, among found: Set<String>, in binFolder: String
718 ) throws -> [String] {
728 let nativeName = package.name + "PackageTests"
738 // A test target may carry that name itself; its bundle then holds only its tests.
748 if found.contains(nativeName + bundleSuffix) && !package.testTargets.contains(nativeName) {
751 return [nativeName + bundleSuffix]
767 }
7712 let missing = package.testTargets.filter { !found.contains($0 + bundleSuffix) }
787 if missing.count == package.testTargets.count {
792 throw SwiftPMExportError.noTestBundles(binFolder: binFolder)
805 }
815 guard missing.isEmpty else {
822 throw SwiftPMExportError.missingTestBundles(
832 targets: missing.sorted(), binFolder: binFolder)
843 }
856 return package.testTargets.map { $0 + bundleSuffix }.sorted()
868 }
87}
88
89public 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
96extension SwiftPMExportError: CustomStringConvertible {
9720 public var description: String {
9820 let remedy = "Run the tests with --enable-code-coverage."
9920 return switch self {
10020 case .unreadableDescription:
1012 "Cannot read the output of `swift package describe --type json`."
10220 case .noProfile(let path): "There is no coverage profile at \(path). \(remedy)"
10320 case .noTestBundles(let binFolder):
1047 "No test bundle of this package is in \(binFolder). \(remedy)"
10520 case .missingTestBundles(let targets, let binFolder):
1067 "No test bundle for \(targets.joined(separator: ", ")) is in \(binFolder). \(remedy)"
10720 }
10820 }
109}