diff --git a/Sources/ProjectSpec/SpecLoader.swift b/Sources/ProjectSpec/SpecLoader.swift index 56c31a5c..bf155d57 100644 --- a/Sources/ProjectSpec/SpecLoader.swift +++ b/Sources/ProjectSpec/SpecLoader.swift @@ -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, value) in targets.sorted(by: { $0.key < $1.key }) { + if let targetDict = value as? [String: Any] { + for key in targetDict.keys.sorted() where !Target.recognizedKeys.contains(key) { + errors.append(.unrecognizedTargetKey(target: targetName, key: key)) + } + } + } + } if !errors.isEmpty { throw SpecValidationError(errors: errors) diff --git a/Sources/ProjectSpec/SpecValidationError.swift b/Sources/ProjectSpec/SpecValidationError.swift index 7f16d2ad..1fce6a6a 100644 --- a/Sources/ProjectSpec/SpecValidationError.swift +++ b/Sources/ProjectSpec/SpecValidationError.swift @@ -44,6 +44,7 @@ public struct SpecValidationError: Error, CustomStringConvertible { case invalidPluginPackageReference(plugin: String, package: String) case emptySourcePath(target: String) case invalidProjectFormat(String) + case unrecognizedTargetKey(target: String, key: String) public var description: String { switch self { @@ -116,6 +117,9 @@ 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 .unrecognizedTargetKey(target, key): + let validKeys = Target.recognizedKeys.sorted().joined(separator: ", ") + return "Target \(target.quoted) has unrecognized key \(key.quoted). Valid target keys are: \(validKeys)" } } } diff --git a/Sources/ProjectSpec/Target.swift b/Sources/ProjectSpec/Target.swift index 566efe5e..c1b6f066 100644 --- a/Sources/ProjectSpec/Target.swift +++ b/Sources/ProjectSpec/Target.swift @@ -277,6 +277,23 @@ extension LegacyTarget: JSONEncodable { } } +extension Target { + + public static let recognizedKeys: Set = [ + "name", "productName", "type", "supportedDestinations", + "isMultiPlatformTarget", "platform", "deploymentTarget", + "settings", "configFiles", "sources", "dependencies", + "buildToolPlugins", "info", "entitlements", + "transitivelyLinkDependencies", "directlyEmbedCarthageDependencies", + "requiresObjCLinking", "preBuildScripts", "prebuildScripts", + "postCompileScripts", "postBuildScripts", "postbuildScripts", + "buildRules", "scheme", "legacy", "attributes", + "onlyCopyFilesOnInstall", "putResourcesBeforeSourcesBuildPhase", + "templates", "templateAttributes", + "platformPrefix", "platformSuffix", + ] +} + extension Target: NamedJSONDictionaryConvertible { public init(name: String, jsonDictionary: JSONDictionary) throws { diff --git a/Tests/ProjectSpecTests/SpecLoadingTests.swift b/Tests/ProjectSpecTests/SpecLoadingTests.swift index 3ff0e4e2..a9f2aa94 100644 --- a/Tests/ProjectSpecTests/SpecLoadingTests.swift +++ b/Tests/ProjectSpecTests/SpecLoadingTests.swift @@ -423,6 +423,58 @@ class SpecLoadingTests: XCTestCase { throw failure("Expected to not throw a validation error. Got: \(error)") } } + + $0.it("warns on unrecognized target keys") { + let dictionary: [String: Any] = [ + "name": "TestUnrecognizedKey", + "targets": [ + "MyApp": [ + "type": "application", + "platform": "iOS", + "resources": ["Resources/**"], + ], + ], + ] + try? Yams.dump(object: dictionary).write(toFile: path.string, atomically: true, encoding: .utf8) + let specLoader = SpecLoader(version: "1.1.0") + _ = try specLoader.loadProject(path: path) + do { + try specLoader.validateProjectDictionaryWarnings() + throw failure("Expected a warning for unrecognized key \"resources\"") + } catch let error as SpecValidationError { + let expected = SpecValidationError.ValidationError.unrecognizedTargetKey(target: "MyApp", key: "resources") + if !error.errors.contains(expected) { + throw failure("Expected warning for unrecognized key \"resources\", got: \(error)") + } + let message = expected.description + if !message.contains("\"resources\"") || !message.contains("Valid target keys are:") { + throw failure("Expected message to name the key and list valid keys, got: \(message)") + } + } + } + + $0.it("does not warn on recognized target keys") { + let dictionary: [String: Any] = [ + "name": "TestRecognizedKeys", + "targets": [ + "MyApp": [ + "type": "application", + "platform": "iOS", + "sources": ["Sources"], + "dependencies": [], + "settings": ["SWIFT_VERSION": "5"], + ], + ], + ] + try? Yams.dump(object: dictionary).write(toFile: path.string, atomically: true, encoding: .utf8) + let specLoader = SpecLoader(version: "1.1.0") + _ = try specLoader.loadProject(path: path) + do { + try specLoader.validateProjectDictionaryWarnings() + } catch { + throw failure("Unexpected warning for recognized keys: \(error)") + } + } } }