diff --git a/Sources/XcodeGenKit/Spec/SpecError.swift b/Sources/XcodeGenKit/Spec/SpecError.swift index 7175df68..d201ba6c 100644 --- a/Sources/XcodeGenKit/Spec/SpecError.swift +++ b/Sources/XcodeGenKit/Spec/SpecError.swift @@ -8,8 +8,16 @@ import Foundation -public enum SpecError: Error { +public enum SpecError: Error, CustomStringConvertible { case unknownTargetType(String) case unknownTargetPlatform(String) case invalidDependency([String: Any]) + + public var description: String { + switch self { + case let .unknownTargetType(type): return "Unknown Target type: \(type)" + case let .unknownTargetPlatform(platform): return "Unknown Target platform: \(platform)" + case let .invalidDependency(dependency): return "Unknown Target dependency: \(dependency)" + } + } } diff --git a/Sources/XcodeGenKit/Spec/Target.swift b/Sources/XcodeGenKit/Spec/Target.swift index 29797e0a..fcd35680 100644 --- a/Sources/XcodeGenKit/Spec/Target.swift +++ b/Sources/XcodeGenKit/Spec/Target.swift @@ -68,7 +68,11 @@ extension Target: JSONObjectConvertible { sources = jsonDictionary.json(atKeyPath: "sources") ?? [] } sourceExludes = jsonDictionary.json(atKeyPath: "sourceExludes") ?? [] - dependencies = jsonDictionary.json(atKeyPath: "dependencies") ?? [] + if jsonDictionary["dependencies"] == nil { + dependencies = [] + } else { + dependencies = try jsonDictionary.json(atKeyPath: "dependencies", invalidItemBehaviour: .fail) + } prebuildScripts = jsonDictionary.json(atKeyPath: "prebuildScripts") ?? [] postbuildScripts = jsonDictionary.json(atKeyPath: "postbuildScripts") ?? [] } diff --git a/Tests/XcodeGenKitTests/FixtureTests.swift b/Tests/XcodeGenKitTests/FixtureTests.swift index 82c4fd0b..293978d1 100644 --- a/Tests/XcodeGenKitTests/FixtureTests.swift +++ b/Tests/XcodeGenKitTests/FixtureTests.swift @@ -1,4 +1,4 @@ -import XCTest +import Spectre import PathKit import XcodeGenKit import xcodeproj @@ -14,17 +14,16 @@ func generate(specPath: Path, projectPath: Path) throws { try project.write(path: projectPath, override: true) _ = try XcodeProj(path: projectPath) } else { - XCTFail("Spec has errors:\n\(lintedSpec.errors.map { $0.description}.joined(separator: "\n"))") + throw failure("Spec has errors:\n\(lintedSpec.errors.map { $0.description}.joined(separator: "\n"))") } } -class FixtureTests: XCTestCase { +func fixtureTests() { - func testGeneration() throws { - try generate(specPath: fixturePath + "TestProject/spec.yml", projectPath: fixturePath + "TestProject/GeneratedProject.xcodeproj") + + describe("Test Project") { + $0.it("generates") { + try generate(specPath: fixturePath + "TestProject/spec.yml", projectPath: fixturePath + "TestProject/GeneratedProject.xcodeproj") + } } - - static var allTests = [ - ("testGeneration", testGeneration), - ] } diff --git a/Tests/XcodeGenKitTests/GeneratorTests.swift b/Tests/XcodeGenKitTests/GeneratorTests.swift index cbbeb74c..e425cffc 100644 --- a/Tests/XcodeGenKitTests/GeneratorTests.swift +++ b/Tests/XcodeGenKitTests/GeneratorTests.swift @@ -1,9 +1,9 @@ -import XCTest +import Spectre import XcodeGenKit import xcodeproj import PathKit -class GeneratorTests: XCTestCase { +func generatorTests() { func getProject(_ spec: Spec) throws -> XcodeProj { let lintedSpec = SpecLinter.lint(spec) @@ -11,12 +11,13 @@ class GeneratorTests: XCTestCase { return try generator.generate() } - func testGeneratorGeneratesDefaultConfigs() throws { - let project = try getProject(Spec(name: "test")) - XCTAssert(project.pbxproj.objects.buildConfigurations.count == 2) - } + describe("Generator") { - static var allTests = [ - ("testGeneratorGeneratesDefaultConfigs", testGeneratorGeneratesDefaultConfigs), - ] + $0.it("provide defaults") { + let project = try getProject(Spec(name: "test")) + try expect(project.pbxproj.objects.buildConfigurations.count) == 2 + + } + } } + diff --git a/Tests/XcodeGenKitTests/SpecLoadingTests.swift b/Tests/XcodeGenKitTests/SpecLoadingTests.swift index 5d8a11b4..69bdb4d7 100644 --- a/Tests/XcodeGenKitTests/SpecLoadingTests.swift +++ b/Tests/XcodeGenKitTests/SpecLoadingTests.swift @@ -1,9 +1,9 @@ -import XCTest +import Spectre import PathKit import XcodeGenKit import xcodeproj -class SpecLoadingTests: XCTestCase { +func specLoadingTests() { @discardableResult func getSpec(_ spec: [String: Any]) throws -> Spec { @@ -14,22 +14,34 @@ class SpecLoadingTests: XCTestCase { return try Spec(jsonDictionary: specDictionary) } - func expectSpecFailure(_ expectedError: SpecError, _ spec: [String: Any]) { - expectError(expectedError) { + func expectSpecFailure(_ expectedError: SpecError, _ spec: [String: Any]) throws { + try expectError(expectedError) { try getSpec(spec) } } - func testIncorrectTargetPlatform() throws { - expectSpecFailure(.unknownTargetPlatform("invalid"), ["targets": [["name": "test", "type": "application", "platform": "invalid"]]]) + let validTarget: [String: Any] = ["name": "test", "type": "application", "platform": "iOS"] + let invalidName = "invalid" + + describe("The project spec") { + + $0.it("fails with incorrect platform") { + var target = validTarget + target["platform"] = invalidName + try expectSpecFailure(.unknownTargetPlatform("invalid"), ["targets": [target]]) + } + + $0.it("fails with incorrect product type") { + var target = validTarget + target["type"] = invalidName + try expectSpecFailure(.unknownTargetType("invalid"), ["targets": [target]]) + } + + $0.it("fails with invalid target dependency") { + var target = validTarget + target["dependencies"] = [["invalid": "target"]] + try expectSpecFailure(.invalidDependency(["invalid": "target"]), ["targets": [target]]) + } } - func testIncorrectTargetProductType() throws { - expectSpecFailure(.unknownTargetType("invalid"), ["targets": [["name": "test", "type": "invalid", "platform": "iOS"]]]) - } - - static var allTests = [ - ("testIncorrectTargetPlatform", testIncorrectTargetPlatform), - ("testIncorrectTargetProductType", testIncorrectTargetProductType), - ] } diff --git a/Tests/XcodeGenKitTests/TestHelpers.swift b/Tests/XcodeGenKitTests/TestHelpers.swift index 69f73b95..b74adf91 100644 --- a/Tests/XcodeGenKitTests/TestHelpers.swift +++ b/Tests/XcodeGenKitTests/TestHelpers.swift @@ -1,12 +1,14 @@ import Foundation -import XCTest +import Spectre -func expectError(_ expectedError: Error, closure: () throws -> ()) { +func expectError(_ expectedError: T, closure: () throws -> ()) throws where T: CustomStringConvertible { do { try closure() - XCTFail("Supposed to fail with \"\(expectedError)\"") + } catch let error as T { + try expect(error.description) == expectedError.description + return } catch { - XCTAssert(error.localizedDescription == expectedError.localizedDescription, - "Expected error \"\(expectedError.localizedDescription)\" but got \"\(error.localizedDescription)\"") + throw failure("Supposed to fail with \"\(expectedError)\"") } + throw failure("Supposed to fail with \"\(expectedError)\"") } diff --git a/Tests/XcodeGenKitTests/XCTest.swift b/Tests/XcodeGenKitTests/XCTest.swift new file mode 100644 index 00000000..fe24cf1e --- /dev/null +++ b/Tests/XcodeGenKitTests/XCTest.swift @@ -0,0 +1,11 @@ +import Foundation +import XCTest + +class XCodeGenKitTests: XCTestCase { + + func testXcodeGenKit() { + generatorTests() + specLoadingTests() + fixtureTests() + } +}