From d10dcc9a0eea35b6ebb89fe8169d76ad5d7fa074 Mon Sep 17 00:00:00 2001 From: Bas van Kuijck Date: Wed, 15 Jan 2020 20:29:24 +0100 Subject: [PATCH] Validate package version value (#755) * Validate package version value * Added CHANGELOG entry * Improved testcases for package versions --- CHANGELOG.md | 1 + Sources/ProjectSpec/SwiftPackage.swift | 22 +++++++++++++++++++ Tests/ProjectSpecTests/SpecLoadingTests.swift | 19 ++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b02ea8a..20c4498f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Don't add framework dependency's directory to `FRAMEWORK_SEARCH_PATHS` if it is implicit [#744](https://github.com/yonaskolb/XcodeGen/pull/744) @ikesyo @yutailang0119 - Fixed resolving relative path passed to `XcodeProj` [#751](https://github.com/yonaskolb/XcodeGen/pull/751) @PycKamil - Prefer configurations named "Debug" or "Release" for default scheme build configurations [#752](https://github.com/yonaskolb/XcodeGen/pull/752) @john-flanagan +- Added an extra check for package versions. [#755](https://github.com/yonaskolb/XcodeGen/pull/755) @basvankuijck #### Internal - Update to SwiftCLI 6.0 and use the new property wrappers [#749](https://github.com/yonaskolb/XcodeGen/pull/749) @yonaskolb diff --git a/Sources/ProjectSpec/SwiftPackage.swift b/Sources/ProjectSpec/SwiftPackage.swift index 6f1be5c1..7aee8575 100644 --- a/Sources/ProjectSpec/SwiftPackage.swift +++ b/Sources/ProjectSpec/SwiftPackage.swift @@ -20,6 +20,28 @@ extension SwiftPackage: JSONObjectConvertible { public init(jsonDictionary: JSONDictionary) throws { url = try jsonDictionary.json(atKeyPath: "url") versionRequirement = try VersionRequirement(jsonDictionary: jsonDictionary) + try validateVersion() + } + + private func validateVersion() throws { + switch versionRequirement { + + case .upToNextMajorVersion(let version): + try _ = Version(version) + + case .upToNextMinorVersion(let version): + try _ = Version(version) + + case .range(let from, let to): + try _ = Version(from) + try _ = Version(to) + + case .exact(let version): + try _ = Version(version) + + default: + break + } } } diff --git a/Tests/ProjectSpecTests/SpecLoadingTests.swift b/Tests/ProjectSpecTests/SpecLoadingTests.swift index c2bcb202..723bae25 100644 --- a/Tests/ProjectSpecTests/SpecLoadingTests.swift +++ b/Tests/ProjectSpecTests/SpecLoadingTests.swift @@ -1119,6 +1119,25 @@ class SpecLoadingTests: XCTestCase { } } + func testPackagesVersion() { + describe { + let invalidPackages = [ + [ "url": "package.git", "majorVersion": "master" ], + [ "url": "package.git", "from": "develop" ], + [ "url": "package.git", "minVersion": "feature/swift5.2", "maxVersion": "9.1.0" ], + [ "url": "package.git", "minorVersion": "x.1.2" ], + [ "url": "package.git", "exactVersion": "1.2.3.1" ], + [ "url": "package.git", "version": "foo-bar" ] + ] + + $0.it("is an invalid package version") { + for dictionary in invalidPackages { + try expect { _ = try SwiftPackage(jsonDictionary: dictionary) }.toThrow() + } + } + } + } + func testDecoding() throws { describe { $0.it("decodes dots in dictionary keys") {