Skip to content

Commit

Permalink
Carry over previous baseline when reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Jun 9, 2024
1 parent 9f6c2c0 commit f28ae29
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
15 changes: 12 additions & 3 deletions Sources/Frontend/Commands/ScanBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,23 @@ final class ScanBehavior {
results = try block(project)

let interval = logger.beginInterval("result:output")
let filteredResults = try OutputDeclarationFilter().filter(results)
var baseline: Baseline?

if let baselinePath = configuration.baseline {
let data = try Data(contentsOf: baselinePath.url)
baseline = try JSONDecoder().decode(Baseline.self, from: data)
}

let filteredResults = try OutputDeclarationFilter().filter(results, with: baseline)

if configuration.autoRemove {
try ScanResultRemover().remove(results: filteredResults)
}

if !filteredResults.isEmpty, let baselinePath = configuration.writeBaseline {
let usrs = filteredResults.flatMapSet { $0.usrs }
if let baselinePath = configuration.writeBaseline {
let usrs = filteredResults
.flatMapSet { $0.usrs }
.union(baseline?.usrs ?? [])
let baseline = Baseline.v1(usrs: usrs.sorted())
let data = try JSONEncoder().encode(baseline)
try data.write(to: baselinePath.url)
Expand Down
2 changes: 1 addition & 1 deletion Sources/PeripheryKit/Results/Baseline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
public enum Baseline: Codable {
case v1(usrs: [String])

var usrs: Set<String> {
public var usrs: Set<String> {
switch self {
case .v1(let usrs):
return Set(usrs)
Expand Down
6 changes: 2 additions & 4 deletions Sources/PeripheryKit/Results/OutputDeclarationFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ public final class OutputDeclarationFilter {
self.contextualLogger = logger.contextualized(with: "report:filter")
}

public func filter(_ declarations: [ScanResult]) throws -> [ScanResult] {
public func filter(_ declarations: [ScanResult], with baseline: Baseline?) throws -> [ScanResult] {
var declarations = declarations

if let baselinePath = configuration.baseline {
let data = try Data(contentsOf: baselinePath.url)
let baseline = try JSONDecoder().decode(Baseline.self, from: data)
if let baseline {
var didFilterDeclaration = false
declarations = declarations.filter {
let isDisjoint = $0.usrs.isDisjoint(with: baseline.usrs)
Expand Down

0 comments on commit f28ae29

Please sign in to comment.