Skip to content

Improve package collection validation error message #4166

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
Feb 24, 2022
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
15 changes: 12 additions & 3 deletions Sources/PackageCollections/PackageCollections+Validation.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
Copyright (c) 2020-2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -80,7 +80,7 @@ extension PackageCollectionModel.V1 {

// TODO: validate package url?
private func validate(package: Collection.Package, messages: inout [ValidationMessage]) {
let packageID = PackageIdentity(url: package.url).description
let packageID = "\(PackageIdentity(url: package.url).description) (\(package.url.absoluteString))"

guard !package.versions.isEmpty else {
messages.append(.error("Package \(packageID) does not have any versions.", property: "package.versions"))
Expand Down Expand Up @@ -145,7 +145,7 @@ extension PackageCollectionModel.V1 {

version.manifests.forEach { toolsVersion, manifest in
if toolsVersion != manifest.toolsVersion {
messages.append(.error("Manifest tools version \(manifest.toolsVersion) does not match \(toolsVersion)", property: "version.manifest"))
messages.append(.error("Package \(packageID) manifest tools version \(manifest.toolsVersion) does not match \(toolsVersion)", property: "version.manifest"))
}

if manifest.products.isEmpty {
Expand Down Expand Up @@ -231,6 +231,15 @@ extension Array where Element == ValidationMessage {
public enum ValidationError: Error, Equatable, CustomStringConvertible {
case property(name: String, message: String)
case other(message: String)

public var message: String {
switch self {
case .property(_, let message):
return message
case .other(let message):
return message
}
}

public var description: String {
switch self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
Copyright (c) 2020-2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -195,7 +195,7 @@ struct JSONPackageCollectionProvider: PackageCollectionProvider {
private func makeCollection(from collection: JSONModel.Collection, source: Model.CollectionSource, signature: Model.SignatureData?) -> Result<Model.Collection, Error> {
do {
if let errors = self.validator.validate(collection: collection)?.errors() {
throw JSONPackageCollectionProviderError.invalidCollection("\(errors)")
throw JSONPackageCollectionProviderError.invalidCollection("\(errors.map { $0.message }.joined(separator: " "))")
}

var serializationOkay = true
Expand Down