Skip to content
Open
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
12 changes: 11 additions & 1 deletion Sources/ProjectSpec/SpecLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ public class SpecLoader {
private extension Dictionary where Key == String, Value: Any {

func validateWarnings() throws {
let errors: [SpecValidationError.ValidationError] = []
var errors: [SpecValidationError.ValidationError] = []

if let targets = self["targets"] as? [String: Any] {
for (targetName, targetValue) in targets {
guard let targetDict = targetValue as? JSONDictionary else { continue }
let unknownKeys = Set(targetDict.keys).subtracting(Target.validYAMLKeys)
for key in unknownKeys.sorted() {
errors.append(.unknownTargetKey(target: targetName, key: key))
}
}
}

if !errors.isEmpty {
throw SpecValidationError(errors: errors)
Expand Down
3 changes: 3 additions & 0 deletions Sources/ProjectSpec/SpecValidationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public struct SpecValidationError: Error, CustomStringConvertible {
case invalidPluginPackageReference(plugin: String, package: String)
case emptySourcePath(target: String)
case invalidProjectFormat(String)
case unknownTargetKey(target: String, key: String)

public var description: String {
switch self {
Expand Down Expand Up @@ -116,6 +117,8 @@ public struct SpecValidationError: Error, CustomStringConvertible {
case let .invalidProjectFormat(projectFormat):
let validValues = ProjectFormat.allCases.map(\.rawValue).joined(separator: ", ")
return "Invalid projectFormat \(projectFormat.quoted). Valid values are: \(validValues)"
case let .unknownTargetKey(target, key):
return "Target \(target.quoted) has an unrecognized key \(key.quoted). This key will be ignored. Check the XcodeGen documentation for valid target keys."
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Sources/ProjectSpec/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ extension LegacyTarget: JSONEncodable {

extension Target: NamedJSONDictionaryConvertible {

/// All YAML keys recognized at the top level of a target definition.
public static let validYAMLKeys: Set<String> = [
"name", "productName", "type", "platform", "supportedDestinations",
"isMultiPlatformTarget", "deploymentTarget", "settings", "configFiles",
"sources", "dependencies", "buildToolPlugins", "info", "entitlements",
"transitivelyLinkDependencies", "directlyEmbedCarthageDependencies",
"requiresObjCLinking", "preBuildScripts", "prebuildScripts",
"postCompileScripts", "postBuildScripts", "postbuildScripts",
"buildRules", "scheme", "legacy", "attributes",
"onlyCopyFilesOnInstall", "putResourcesBeforeSourcesBuildPhase",
// Template-related keys consumed before Target.init is called.
"templates", "templateAttributes",
]

public init(name: String, jsonDictionary: JSONDictionary) throws {
let resolvedName: String = jsonDictionary.json(atKeyPath: "name") ?? name
self.name = resolvedName
Expand Down
30 changes: 30 additions & 0 deletions Tests/ProjectSpecTests/SpecLoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,36 @@ class SpecLoadingTests: XCTestCase {
throw failure("Expected to not throw a validation error. Got: \(error)")
}
}

$0.it("warns about unrecognized target keys") {
let dictionary: [String: Any] = [
"name": "TestUnknownTargetKey",
"targets": [
"App": [
"type": "application",
"platform": "iOS",
"sources": ["Sources/App"],
"resources": ["Resources"],
],
],
]
try? Yams.dump(object: dictionary).write(toFile: path.string, atomically: true, encoding: .utf8)
let specLoader = SpecLoader(version: "1.1.0")
do {
_ = try specLoader.loadProject(path: path)
} catch {
throw failure("loadProject should not throw for unknown keys. Got: \(error)")
}
do {
try specLoader.validateProjectDictionaryWarnings()
throw failure("Expected a validation error for the unknown 'resources' key")
} catch let error as SpecValidationError {
let expected = SpecValidationError.ValidationError.unknownTargetKey(target: "App", key: "resources")
try expect(error.errors.contains(expected)) == true
} catch {
throw failure("Unexpected error type: \(error)")
}
}
}
}

Expand Down
Loading