Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension PackageDescription.Target {
static let prototype = TargetDefinition(name: "Prototype", path: "Sources/Command/Prototype")
static let report = TargetDefinition(name: "Report", path: "Sources/Command/Report")
static let shared = TargetDefinition(name: "Shared", path: "Sources/Shared")
static let trend = TargetDefinition(name: "Trend", path: "Sources/Command/Trend")
}

@MainActor
Expand All @@ -28,10 +29,6 @@ enum ExternalDependencies {
static let yams = Dependency(package: .package(url: "https://github.com/jpsim/Yams.git", from: "6.2.0"),
target: .product(name: "Yams", package: "yams"))

// needs to be replaced by https://github.com/davbeck/swift-glob in the future
static let globPattern = Dependency(package: .package(url: "https://github.com/ChimeHQ/GlobPattern.git", from: "0.1.1"),
target: .product(name: "GlobPattern", package: "GlobPattern"))

static let asyncAlgorithms = Dependency(package: .package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
target: .product(name: "AsyncAlgorithms", package: "swift-async-algorithms"))

Expand All @@ -40,6 +37,9 @@ enum ExternalDependencies {

static let sqlDriver = Dependency(package: .package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.6.0"),
target: .product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"))

static let swiftGlob = Dependency(package: .package(url: "https://github.com/davbeck/swift-glob", from: "1.0.0"),
target: .product(name: "Glob", package: "swift-glob"))
}

typealias MyPackage = PackageDescription.Target
Expand All @@ -57,7 +57,7 @@ let package = Package(
ExternalDependencies.asyncAlgorithms.package,
ExternalDependencies.fluent.package,
ExternalDependencies.sqlDriver.package,
ExternalDependencies.globPattern.package,
ExternalDependencies.swiftGlob.package,
ExternalDependencies.swiftHTMLParser.package,
ExternalDependencies.yams.package,
],
Expand All @@ -75,6 +75,7 @@ let package = Package(
.build(),
.config(),
.report(),
.trend(),
],
path: MyPackage.app.path
),
Expand Down Expand Up @@ -142,6 +143,12 @@ let package = Package(
.helper(),
.shared(),
]),
MyPackage.trend.toTarget(dependencies: [
ExternalDependencies.argumentParser.target,
.dependencyInjection(),
.helper(),
.shared(),
]),

// MARK: HELPER
MyPackage.helper.toTarget(
Expand All @@ -150,7 +157,7 @@ let package = Package(
ExternalDependencies.asyncAlgorithms.target,
ExternalDependencies.fluent.target,
ExternalDependencies.sqlDriver.target,
ExternalDependencies.globPattern.target,
ExternalDependencies.swiftGlob.target,
ExternalDependencies.swiftHTMLParser.target,
ExternalDependencies.yams.target,
.dependencyInjection(),
Expand Down Expand Up @@ -271,6 +278,11 @@ extension PackageDescription.Target.Dependency {
static func migrate() -> Target.Dependency {
Target.Dependency.target(name: "Migrate")
}

/// SubCommand: `trend`: command to analyze coverage trends
static func trend() -> Target.Dependency {
Target.Dependency.target(name: "Trend")
}
}

struct TargetDefinition {
Expand Down
3 changes: 3 additions & 0 deletions Sources/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
import Migrate
import Prototype
import Report
import Trend

@main
struct App: AsyncParsableCommand {
Expand All @@ -26,6 +27,7 @@ struct App: AsyncParsableCommand {
ReportCommand.self,
CompareCommand.self,
MigrateCommand.self,
TrendCommand.self,
]
#else
return [
Expand All @@ -34,6 +36,7 @@ struct App: AsyncParsableCommand {
ConfigCommand.self,
MigrateCommand.self,
ReportCommand.self,
TrendCommand.self,
]
#endif
}
Expand Down
54 changes: 53 additions & 1 deletion Sources/Command/Coverage/CoverageCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public final class CoverageCommand: DerivedDataCommand, QuietErrorHandling {
@Option(name: [.customShort("f"), .customLong("format")], help: "Output format (json, csv, summary)")
public var format: String?

@Option(name: .long, help: "Minimum coverage threshold percentage (overrides config)")
public var minCoverage: Double?

@Option(name: .long, help: "Maximum coverage drop percentage (overrides config)")
public var maxDrop: Double?

enum CodingKeys: CodingKey {
case verbose, quiet, configFilePath, customGitRootpath, format
case verbose, quiet, configFilePath, customGitRootpath, format, minCoverage, maxDrop
}

public required init() {}
Expand Down Expand Up @@ -77,6 +83,9 @@ public final class CoverageCommand: DerivedDataCommand, QuietErrorHandling {
throw CoverageError.internalError
}

// Load and merge threshold settings
let thresholdSettings = try loadThresholdSettings(from: config)

// Create and run coverage tool
let coverageTool = CoverageTool(
fileHandler: fileHandler,
Expand All @@ -94,6 +103,7 @@ public final class CoverageCommand: DerivedDataCommand, QuietErrorHandling {
locationCurrentReport: reportUrl,
archiveLocation: archiveLocation,
format: format,
thresholdSettings: thresholdSettings,
verbose: verbose,
quiet: quiet
)
Expand Down Expand Up @@ -134,4 +144,46 @@ public final class CoverageCommand: DerivedDataCommand, QuietErrorHandling {
throw error
}
}

private func loadThresholdSettings(from config: Config) throws -> ThresholdSettings? {
// Load threshold settings from config
var configSettings: ThresholdSettings?
do {
configSettings = try config.settings(.threshold) as? ThresholdSettings
} catch {
// Threshold settings not configured in config file, which is fine
configSettings = nil
}

// If we have CLI overrides, merge them with config settings
if minCoverage != nil || maxDrop != nil {
let configMinCoverage = configSettings?.minCoverage
let configMaxDrop = configSettings?.maxDrop
let configPerTargetThresholds = configSettings?.perTargetThresholds ?? [:]

// CLI flags override config values
let finalMinCoverage = minCoverage ?? configMinCoverage
let finalMaxDrop = maxDrop ?? configMaxDrop

// Create merged settings
var mergedDict: [String: String] = [:]
if let min = finalMinCoverage {
mergedDict["min_coverage"] = "\(min)"
}
if let max = finalMaxDrop {
mergedDict["max_drop"] = "\(max)"
}
if !configPerTargetThresholds.isEmpty {
let jsonData = try SingleEncoder.shared.encode(configPerTargetThresholds)
if let json = String(data: jsonData, encoding: .utf8) {
mergedDict["per_target_thresholds"] = json
}
}

return try ThresholdSettings(values: mergedDict)
}

// No CLI overrides, return config settings as-is
return configSettings
}
}
9 changes: 9 additions & 0 deletions Sources/Command/Coverage/CoverageError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ enum CoverageError: LocalizedError, CustomStringConvertible {
case noResultsToWorkWith
case noResultFilesToConvert
case internalError
case thresholdFailedAbsolute(expected: Double, actual: Double)
case thresholdFailedRelative(maxDrop: Double, actualDrop: Double)
case thresholdFailedPerTarget(target: String, expected: Double, actual: Double)

/// Retrieve the localized description for this error.
var localizedDescription: String {
Expand All @@ -42,6 +45,12 @@ enum CoverageError: LocalizedError, CustomStringConvertible {
return "There are no xcresult files to work with"
case .missingDatabasePath:
return "No database path provided"
case let .thresholdFailedAbsolute(expected: expected, actual: actual):
return "Coverage threshold failed: Expected minimum \(String(format: "%.2f", expected))%, but actual coverage is \(String(format: "%.2f", actual))%"
case let .thresholdFailedRelative(maxDrop: maxDrop, actualDrop: actualDrop):
return "Coverage drop threshold exceeded: Maximum allowed drop is \(String(format: "%.2f", maxDrop))%, but coverage dropped by \(String(format: "%.2f", actualDrop))%"
case let .thresholdFailedPerTarget(target: target, expected: expected, actual: actual):
return "Target '\(target)' coverage threshold failed: Expected minimum \(String(format: "%.2f", expected))%, but actual coverage is \(String(format: "%.2f", actual))%"
}
}

Expand Down
Loading