|
| 1 | +// |
| 2 | +// EntitlementsEnvironmentParityTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | +// Guards the CloudKit environment pin. The Mac and iOS apps must both target |
| 6 | +// the Production environment, or a development iOS build talks to the |
| 7 | +// Development database while the Mac talks to Production and sync silently |
| 8 | +// moves nothing across devices. |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import Testing |
| 13 | + |
| 14 | +@Suite("CloudKit environment entitlement parity") |
| 15 | +struct EntitlementsEnvironmentParityTests { |
| 16 | + private static let environmentKey = "com.apple.developer.icloud-container-environment" |
| 17 | + private static let macEntitlements = "TablePro/TablePro.entitlements" |
| 18 | + private static let iosEntitlements = "TableProMobile/TableProMobile/TableProMobileRelease.entitlements" |
| 19 | + |
| 20 | + @Test("Mac app pins the Production CloudKit environment") |
| 21 | + func macTargetsProduction() throws { |
| 22 | + try #expect(environment(in: Self.macEntitlements) == "Production") |
| 23 | + } |
| 24 | + |
| 25 | + @Test("iOS app pins the Production CloudKit environment") |
| 26 | + func iosTargetsProduction() throws { |
| 27 | + try #expect(environment(in: Self.iosEntitlements) == "Production") |
| 28 | + } |
| 29 | + |
| 30 | + private func environment(in relativePath: String) throws -> String? { |
| 31 | + let url = try repoRoot().appendingPathComponent(relativePath) |
| 32 | + let data = try Data(contentsOf: url) |
| 33 | + let plist = try PropertyListSerialization.propertyList(from: data, format: nil) |
| 34 | + let entitlements = try #require(plist as? [String: Any], "Entitlements at \(relativePath) is not a dictionary") |
| 35 | + return entitlements[Self.environmentKey] as? String |
| 36 | + } |
| 37 | + |
| 38 | + private func repoRoot(file: StaticString = #filePath) throws -> URL { |
| 39 | + var directory = URL(fileURLWithPath: "\(file)").deletingLastPathComponent() |
| 40 | + while directory.path != "/" { |
| 41 | + let marker = directory.appendingPathComponent("TablePro.xcodeproj") |
| 42 | + if FileManager.default.fileExists(atPath: marker.path) { |
| 43 | + return directory |
| 44 | + } |
| 45 | + directory = directory.deletingLastPathComponent() |
| 46 | + } |
| 47 | + throw EntitlementsParityError.repoRootNotFound |
| 48 | + } |
| 49 | + |
| 50 | + private enum EntitlementsParityError: Error { |
| 51 | + case repoRootNotFound |
| 52 | + } |
| 53 | +} |
0 commit comments