Skip to content

Commit

Permalink
Temporary fix
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Jan 13, 2024
1 parent 01f5dcd commit 4019b85
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Sources/ValidatorCore/Commands/CheckDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public struct CheckDependencies: AsyncParsableCommand {
// fetch all dependencies
let api = SwiftPackageIndexAPI(baseURL: apiBaseURL, apiToken: spiApiToken)
let records = try await Current.fetchDependencies(api)
let allPackages = records.allPackages
print("Total packages:", allPackages.count)
let serverPackages = records.allPackages
print("Total packages (server):", serverPackages.count)

let allDependencies = records.allDependencies
let missing = allDependencies.subtracting(allPackages)
let missing = allDependencies.subtracting(serverPackages)
print("Not indexed:", missing.count)

let client = HTTPClient(eventLoopGroupProvider: .singleton,
Expand Down Expand Up @@ -80,7 +80,7 @@ public struct CheckDependencies: AsyncParsableCommand {
print(" ... redirected to:", resolved)
}

if allPackages.contains(resolved.canonicalPackageURL) {
if serverPackages.contains(resolved.canonicalPackageURL) {
print(" ... ⛔ already indexed")
continue
}
Expand Down Expand Up @@ -111,10 +111,14 @@ public struct CheckDependencies: AsyncParsableCommand {
}

// merge with existing and sort result
let input = allPackages.map { $0.packageURL }
let packageList = try inputSource.packageURLs()
let server = serverPackages.map(\.packageURL)
#warning("This is a temporary fix!")
let deleted = Set(server).subtracting(packageList)
let merged = Array(newPackages.map(\.value.packageURL))
.mergingWithExisting(urls: input)
.mergingWithExisting(urls: try inputSource.packageURLs())
.mergingWithExisting(urls: server)
.mergingWithExisting(urls: packageList)
.filter { !deleted.contains($0) }
.sorted(by: { $0.lowercased() < $1.lowercased() })

print("Total:", merged.count)
Expand Down
38 changes: 38 additions & 0 deletions Tests/ValidatorTests/CheckDependenciesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,44 @@ final class CheckDependenciesTests: XCTestCase {
XCTAssertEqual(saved, [.p1, .p2])
}

func test_issue_2828() async throws {
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2828
// The input list coming out of RedirectCheck has removed packages. Ensure they are
// not being put back via the API dependency call's package list.
Current = .mock
Current.fetchDependencies = { _ in [
// p1 is still on the server and is being returned by the dependencies API call
.init(.p1, dependencies: []),
.init(.p2, dependencies: []),
]}
var saved: [PackageURL]? = nil
Current.fileManager.createFile = { path, data, _ in
guard path.hasSuffix("package.json") else { return false }
guard let data = data else {
XCTFail("data must not be nil")
return false
}
guard let list = try? JSONDecoder().decode([PackageURL].self, from: data) else {
XCTFail("decoding of output failed")
return false
}
saved = list
return true
}
Current.fetchRepository = { _, url in throw Error.unexpectedCall }
Current.fetch = { client, url in
client.eventLoopGroup.next().makeFailedFuture(Error.unexpectedCall)
}
check.packageUrls = [.p2] // p1 not in input list - it's been removed by CheckRedirect
check.output = "package.json"

// MUT
try await check.run()

// validate
XCTAssertEqual(saved, [.p2])
}

}


Expand Down

0 comments on commit 4019b85

Please sign in to comment.