Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary fix #59

Merged
merged 4 commits into from
Jan 13, 2024
Merged
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
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 }
#warning("This is a temporary fix!")
let packageList = try inputSource.packageURLs()
let server = serverPackages.map(\.packageURL)
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