Skip to content

Commit

Permalink
Add basic end-to-end CheckDependencies2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Dec 31, 2023
1 parent 8391deb commit 3a96203
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/ValidatorCore/Commands/CheckDependencies2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct CheckDependencies2: AsyncParsableCommand {

// fetch all dependencies
let api = SwiftPackageIndexAPI(baseURL: apiBaseURL, apiToken: spiApiToken)
let records = try await api.fetchDependencies()
let records = try await Current.fetchDependencies(api)
let allPackages = records.allPackages
print("Total packages:", allPackages.count)

Expand Down Expand Up @@ -115,6 +115,9 @@ public struct CheckDependencies2: AsyncParsableCommand {
print("Total:", merged.count)

#warning("load and merge package file")
if let path = output {
try Current.fileManager.saveList(merged, path: path)
}
}

public init() { }
Expand Down
3 changes: 3 additions & 0 deletions Sources/ValidatorCore/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Environment {
var decodeManifest: (_ url: Package.ManifestURL) throws -> Package
var fileManager: FileManager
var fetch: (_ client: HTTPClient, _ url: URL) -> EventLoopFuture<ByteBuffer>
var fetchDependencies: (_ api: SwiftPackageIndexAPI) async throws -> [SwiftPackageIndexAPI.PackageRecord]
var fetchRepository: (_ client: HTTPClient,
_ owner: String,
_ repository: String) -> EventLoopFuture<Github.Repository>
Expand All @@ -38,6 +39,7 @@ extension Environment {
decodeManifest: { url in try Package.decode(from: url) },
fileManager: .live,
fetch: Github.fetch(client:url:),
fetchDependencies: { try await $0.fetchDependencies() },
fetchRepository: Github.fetchRepository(client:owner:repository:),
fetchRepositoryAsync: Github.fetchRepository(client:url:),
githubToken: { ProcessInfo.processInfo.environment["GITHUB_TOKEN"] },
Expand All @@ -50,6 +52,7 @@ extension Environment {
decodeManifest: { _ in fatalError("not implemented") },
fileManager: .mock,
fetch: { client, _ in client.eventLoopGroup.next().makeFailedFuture(AppError.runtimeError("unimplemented")) },
fetchDependencies: { _ in [] },
fetchRepository: { client, _, _ in
client.eventLoopGroup.next().makeSucceededFuture(
Github.Repository(default_branch: "main", fork: false)) },
Expand Down
83 changes: 83 additions & 0 deletions Tests/ValidatorTests/CheckDependencies2Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import ValidatorCore

import CanonicalPackageURL


final class CheckDependencies2Tests: XCTestCase {

func test_basic() async throws {
// Input urls and api urls agree - we're up-to-date with reconciliation, i.e. the package list
// we process in validation is the same package list that has been reconciled when we make the
// dependencies API call.
Current = .mock
Current.fetchDependencies = { _ in [
.init(.p1, dependencies: []),
.init(.p2, dependencies: [.p3]),
]}
var saved: [PackageURL]? = nil
Current.fileManager.createFile = { _, data, _ in
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
}

var check = CheckDependencies2()
check.apiBaseURL = "unused"
check.limit = .max
check.spiApiToken = "unused"
check.output = "unused"
check.packageUrls = [.p1, .p2]

// MUT
try await check.run()

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

}


private extension PackageURL {
static let p1 = PackageURL(argument: "https://github.com/org/1.git")!
static let p2 = PackageURL(argument: "https://github.com/org/2.git")!
static let p3 = PackageURL(argument: "https://github.com/org/3.git")!
// static let p4 = PackageURL(argument: "https://github.com/org/4")!
}

private extension CanonicalPackageURL {
static let p1 = CanonicalPackageURL(prefix: .gitAt, hostname: "github.com", path: "org/1")
static let p2 = CanonicalPackageURL(prefix: .http, hostname: "github.com", path: "org/2")
static let p3 = CanonicalPackageURL(prefix: .https, hostname: "github.com", path: "org/3")
// static let p4 = CanonicalPackageURL(prefix: .https, hostname: "github.com", path: "org/4")
// static let p5 = CanonicalPackageURL(prefix: .https, hostname: "github.com", path: "org/5")
}

private extension SwiftPackageIndexAPI.PackageRecord {
init(_ url: CanonicalPackageURL, dependencies: [CanonicalPackageURL]) {
self.init(id: .init(), url: url, resolvedDependencies: dependencies)
}
}

0 comments on commit 3a96203

Please sign in to comment.