Skip to content

[Collections] refreshCollection should have correct source config #3314

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

Merged
merged 1 commit into from
Mar 1, 2021
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
2 changes: 1 addition & 1 deletion Sources/PackageCollections/Model/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension PackageCollectionsModel {
public let identifier: Identifier

/// Where the collection and its contents are obtained
public let source: Source
public internal(set) var source: Source

/// The name of the collection
public let name: String
Expand Down
15 changes: 13 additions & 2 deletions Sources/PackageCollections/PackageCollections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,17 @@ public struct PackageCollections: PackageCollectionsProtocol {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}

self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil, callback: callback)
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
guard let savedSource = sources.first(where: { $0 == source }) else {
return callback(.failure(NotFoundError("\(source)")))
}
self.refreshCollectionFromSource(source: savedSource, trustConfirmationProvider: nil, callback: callback)
}
}
}

public func addCollection(_ source: PackageCollectionsModel.CollectionSource,
Expand Down Expand Up @@ -389,7 +399,7 @@ public struct PackageCollections: PackageCollectionsProtocol {
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let collection):
case .success(var collection):
// If collection is signed and signature is valid, save to storage. `provider.get`
// would have failed if signature were invalid.
if collection.isSigned {
Expand Down Expand Up @@ -426,6 +436,7 @@ public struct PackageCollections: PackageCollectionsProtocol {
callback(.failure(error))
case .success:
if userTrusted {
collection.source = source
self.storage.collections.put(collection: collection, callback: callback)
} else {
// Try to remove the untrusted collection (if previously saved) from storage before calling back
Expand Down
38 changes: 38 additions & 0 deletions Tests/PackageCollectionsTests/PackageCollectionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,44 @@ final class PackageCollectionsTests: XCTestCase {
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
}

func testRefreshOneTrustedUnsigned() throws {
try skipIfUnsupportedPlatform()

let configuration = PackageCollections.Configuration()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }

let mockCollections = makeMockCollections(count: 1, signed: false)
let collectionProviders = [PackageCollectionsModel.CollectionSourceType.json: MockCollectionsProvider(mockCollections)]
let metadataProvider = MockMetadataProvider([:])
let packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: collectionProviders, metadataProvider: metadataProvider)

// User trusted
let collection = try tsc_await { callback in packageCollections.addCollection(mockCollections[0].source, order: nil, trustConfirmationProvider: { _, cb in cb(true) }, callback: callback) }
XCTAssertEqual(true, collection.source.isTrusted) // isTrusted is nil-able

// `isTrusted` should be true so refreshCollection should succeed
XCTAssertNoThrow(try tsc_await { callback in packageCollections.refreshCollection(collection.source, callback: callback) })
}

func testRefreshOneNotFound() throws {
try skipIfUnsupportedPlatform()

let configuration = PackageCollections.Configuration()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }

let mockCollections = makeMockCollections(count: 1, signed: false)
let collectionProviders = [PackageCollectionsModel.CollectionSourceType.json: MockCollectionsProvider(mockCollections)]
let metadataProvider = MockMetadataProvider([:])
let packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: collectionProviders, metadataProvider: metadataProvider)

// Don't add collection so it's not found in the config
XCTAssertThrowsError(try tsc_await { callback in packageCollections.refreshCollection(mockCollections[0].source, callback: callback) }, "expected error") { error in
XCTAssert(error is NotFoundError)
}
}

func testListTargets() throws {
try skipIfUnsupportedPlatform()

Expand Down